如何用fill()方法填充Java中特定对象的多维数组?

时间:2023-02-09 20:31:45

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:

展示代码比谈论它更容易。我也在互联网上寻找答案,但是没有找到,所以我的代码是:

http://pastebin.com/M606mXzR

http://pastebin.com/M606mXzR

I also added an output, which is on lines 50-70. Output from 61-70 is the "right" one, the one I want.

我还添加了一个输出,在第50-70行。61-70的输出是“正确”的,我想要的。

Am I using fill() method wrong, or what? Can't wrap my head around this...

我使用fill()方法是错误的,还是什么?我想不起来……

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!

不管怎样,只要看看我的代码,谢谢你们的回答!

1 个解决方案

#1


4  

Answer to your question: Yes, there is a difference (see JavaDoc).

回答你的问题:是的,有区别(参见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.

您的第一个版本将一个对象实例放入一行的每个数组元素中。因此,对这个实例的更改在数组的同一行中的每个元素中都是可见的。你总共有b类实例。

The second version puts its own instance into each array element. You'll have i*j ClassB instances in total.

第二个版本将自己的实例放入每个数组元素中。您将总共拥有i*j类b实例。

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.

希望这些信息对您有所帮助,我没有看您的pastebin代码。

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]]

#1


4  

Answer to your question: Yes, there is a difference (see JavaDoc).

回答你的问题:是的,有区别(参见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.

您的第一个版本将一个对象实例放入一行的每个数组元素中。因此,对这个实例的更改在数组的同一行中的每个元素中都是可见的。你总共有b类实例。

The second version puts its own instance into each array element. You'll have i*j ClassB instances in total.

第二个版本将自己的实例放入每个数组元素中。您将总共拥有i*j类b实例。

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.

希望这些信息对您有所帮助,我没有看您的pastebin代码。

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]]