It's easier to show the code, than talk about it. I also searched the inter-webs for an answer, but could not find any, so here's my code:
I also added an output, which is on lines 50-70. Output from 61-70 is the "right" one, the one I want.
Am I using fill() method wrong, or what? Can't wrap my head around this...
Is there really a difference?
ClassB[][] classB_2Array = new ClassB[10][10];
Between this:
for (ClassB[] classB_1Array : classB_2Array) {
Arrays.fill(classB_1Array, new ClassB());
}
to this:
for (int i = 0; i < classB_2Array.length; i++) {
for (int j = 0; j < classB_2Array[0].length; j++) {
classB_2Array[i][j] = new ClassB();
}
}
Anyways, just check out my code and thank you all for your answers!
Answer to your question: Yes, there is a difference (see JavaDoc).
Your first version puts one object instance into every single array element of a row. So a change to this instance is visible in every element in the same row of the array. You'll have i
ClassB
instances in total.
The second version puts its own instance into each array element. You'll have i*j
ClassB
instances in total.
Your first version of the code is equivalent to
for (ClassB[] classB_1Array : classB_2Array) {
ClassB instance = new ClassB();
Arrays.fill(classB_1Array, instance);
}
Hope this information helps you, I did not look at your pastebin code.
EDIT:
To clarify your misunderstanding, closely look at the output of this programm:
import java.util.Arrays;
public class ArrayFiller {
public static void main(String[] args) {
// your first version:
Person[][] yourFirstVersion = new Person[2][2];
for (Person[] array : yourFirstVersion) {
Arrays.fill(array, new Person("Mike"));
}
System.out.println(Arrays.deepToString(yourFirstVersion));
yourFirstVersion[0][1].setName("Paul");
System.out.println(Arrays.deepToString(yourFirstVersion));
System.out.println("-----");
// equivalent: my version:
Person[][] myVersion = new Person[2][2];
for (Person[] array : myVersion) {
Person person = new Person("John");
Arrays.fill(array, person);
}
System.out.println(Arrays.deepToString(myVersion));
myVersion[0][1].setName("Thomas");
System.out.println(Arrays.deepToString(myVersion));
System.out.println("-----");
// your second version
Person[][] yourSecondVersion = new Person[2][2];
for (int i = 0; i < yourSecondVersion.length; i++) {
for (int j = 0; j < yourSecondVersion[i].length; j++) {
yourSecondVersion[i][j] = new Person("Max");
}
}
System.out.println(Arrays.deepToString(yourSecondVersion));
yourSecondVersion[0][1].setName("Chris");
System.out.println(Arrays.deepToString(yourSecondVersion));
}
private static class Person {
private String name;
public Person(String name) {
System.out.println("Constructor called for " + name);
this.name = name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
}
Here's the output:
Constructor called for Mike
Constructor called for Mike
[[Mike, Mike], [Mike, Mike]]
[[Paul, Paul], [Mike, Mike]]
-----
Constructor called for John
Constructor called for John
[[John, John], [John, John]]
[[Thomas, Thomas], [John, John]]
-----
Constructor called for Max
Constructor called for Max
Constructor called for Max
Constructor called for Max
[[Max, Max], [Max, Max]]
[[Max, Chris], [Max, Max]]