This question already has answers here:
What does this for(:) mean in Java?
(2 answers)
Closed 6 years ago.
I am reading an example in my textbook (finding the path of a file recursively) and came across this format for a loop
for (File folderItem : dir.listFiles()){}
The only for loop I'm used to is
for (int i = 0; i < 10; i++){}
How does this loop work, and what is the logic behind it?
This is a new type of for loop (introduced in Java 5). It is used to iterate over some types of collections. It's basically identical to
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++)
{
File folderItem = files[i];
// Code
}
Related
This question already has answers here:
The best way to print a Java 2D array? [closed]
(14 answers)
Closed 2 years ago.
I am new to Java and was wondering what I did wrong. I am building a simple board game, but for some reason the array prints as an array of "Ljava.lang.String;#4bec1f0c". I've read some similar questions to this problem but for some reason, the solutions that I have found does not work in my code.
Help is appreciated.
for(int row = 0; row < gameBoard.length; row++)
{
for(int col = 0; col < gameBoard[row].length; col++)
{
gameBoard[row][col] = "- ";
}
}
System.out.print(Arrays.toString(gameBoard));
if you want to every object be visible in a 2-dimension array, use this method:
System.out.print(Arrays.deepToString(gameBoard));
Arrays.toString is working in single dimension arrays.
more data and examples:
https://www.geeksforgeeks.org/arrays-deeptostring-in-java-with-example/
Arrays.toString does the right thing for one-dimensional arrays, but won't help much on two-dimensional arrays.
Use Arrays.deepToString instead.
This question already has answers here:
NullPointerException when Creating an Array of objects [duplicate]
(9 answers)
Closed 2 years ago.
I'm using an Array of ArrayLists of Integer in Java but it can't run. Every time I get the message
"Note: Goal.java uses unchecked or unsafe operations." and
"Note: Recompile with -Xlint:unchecked for details." after compile and when I want to run it message "Exception in thread "main" java.lang.NullPointerException" appears.
the code is (All is in main method) :
int v = 8;
ArrayList<Integer>[] adj = new ArrayList[11];
adj[0].add(21);
and the error belongs to the last line.
searched a lot but nothing ! plz help, stuck for hours now.
According to the comment section, people are right about the null elements in your array of arraylists. To resolve the NullPointerException you should initialize elements of the array right after its declaration and instantiation.
Here's the modified code below that works:
ArrayList<Integer>[] adj = new ArrayList[11];
for (int i = 0; i < 11; i++)
adj[i] = new ArrayList<Integer>();
adj[0].add(21);
You should first initialize all indices of the array:
int v = 8;
ArrayList<Integer>[] adj = new ArrayList[11];
for(int i=0; i<adj.length; i++) {
adj[i] = new ArrayList<>();
}
adj[0].add(21);
This question already has answers here:
Dynamic variable names Java
(3 answers)
Closed 7 years ago.
I have for example:
String invslot_ = "invslot_";
int i = 0;
Now I have a while(i < 44) loop and I want it
every time it loops to "add" invslot_ and i.
(Yes I could do invslot_ + i !)
But I want that infslot_ + 1 is recognized as a variable I have defined earlier!
I hope you understand what I mean, please answer if you have an idea.
Map<String, Number> vars = new HashMap<>();
vars.put("invslot_1", 3267);
for (int i = 0; i < 44; i++)
if (!vars.contains("invslot_" + i)) // put only if "var" is not already "assigned"
vars.put("invslot_" + i, i);
System.out.println(vars.get("invslot_1")); // 3267
System.out.println(vars.get("invslot_11")); // 11
This question already has answers here:
Java: Different outputs when add/remove short and integer elements in a Set
(3 answers)
Closed 8 years ago.
Set<Short> set = new HashSet<Short>();
short i = 0;
for (i = 0; i < 100; i++) {
set.add(i);
set.remove(i-1);
}
System.out.println(set.size());
When we run above program output comes 100.
I understood it that As we are inserting short values in set and trying to remove integer value. So It's not removing from set.
But if we modify remove statement as below
set.remove(i);
Output coming 0. Any idea?
While you are removing it from the set, i-1 is treated as object and need to be typecast to shore to remove correct value from the set. set.remove((short)(i - 1))
Set<Short> set = new HashSet<Short>();
short i = 0;
for (i = 0; i < 100; i++) {
set.add(i);
set.remove((short)(i - 1));
}
System.out.println(set.size());
Try this code its giving output as 1.
This question already has answers here:
Arrays.asList() not working as it should?
(12 answers)
Closed 9 years ago.
So I'm just learning about arrays in Java (interesting stuff) but I'm having some problems getting my head around the contains() method.
I tried:
Random rn = new Random();
int first = 12;
int[] tab = new int[first];
for (int i = 0; i <= first - 1; i++) {
tab[i] = rn.nextInt(10);
Which seemed to work fine for filling in my Array, but then I tried a:
System.out.println(Arrays.asList(tab).contains(9));
And no matter what, even if I fill the array manually with 9's, it'll still only print up "false".
What am I doing wrong?
Try this
Integer[] tab = new Integer[]{9};
System.out.println(java.util.Arrays.asList(tab).contains(9));
I get
true