java对象类引用相同的双数组,而不是它们自己的双数组

ssm49v7z  于 2021-07-06  发布在  Java
关注(0)|答案(3)|浏览(309)

这个问题在这里已经有答案了

为什么我的arraylist包含添加到列表中的最后一项的n个副本(5个答案)
上个月关门了。
我从一个文件中读取,其中首先包含一个字符串,每行包含50个不同的双精度值,大约10行。我没有问题阅读和存储信息的类。我创建了一个类来保存一些 String word 和一个 double[] weight 保存50个值。然后我创建了一个包含10个单词的对象类型的数组来容纳这10行,我遇到的问题是它们都引用了相同的数组,但是单词被正确地容纳了。
例如,该文件类似于以下文件:
或-1.433 0.384 2.6587

cotxawn7

cotxawn71#

----总共50个双倍值
前-3.576 1.1511 3.000

q5lcpyga

q5lcpyga2#

每个 i 内部循环的迭代,您将重写 tempVec 数组:

for (int i = 1; i <= 10; i ++) {
        tempWord = scan.next();

        for (int j = 0; j < 50; j ++) {
            tempnum = scan.nextDouble();
            tempVec[j] = tempnum; // <-- overriding the same positions.
        }
        this.vertexes[i] = new Word(tempWord, tempVec);
    }

可以将数组视为一个连续的内存块,并将该块传递给 new Word(tempWord, tempVec); 类构造函数。稍后,当您更改数组的内容时,您正在更改这些内存位置的内容。因此,因为要传递相同的数组,所以要(重新)写入相同的内存位置。
您需要创建一个新数组,并将其传递给构造函数。改为:

for (int i = 1; i <= 10; i ++) {
        tempWord = scan.next();
        double[] tempVec = new double[50]; // <-- create a new array per word
        for (int j = 0; j < 50; j ++) {
            tempnum = scan.nextDouble();
            tempVec[j] = tempnum;
        }
        this.vertexes[i] = new Word(tempWord, tempVec);
    }
omtl5h9j

omtl5h9j3#

--共50个双倍值
像这样有10行。
我的程序持有正确的字/字符串,但所有的双数组持有相同的值。
主要代码是:

/* Instance Variables */
File file;
Scanner scan;
Word[] vertexes = new Word[11]; // 1 based indexing

public void readFile() {
    String tempWord;
    double tempnum;
    double[] tempVec = new double[50];

    for (int i = 1; i <= 10; i ++) {
        tempWord = scan.next();

        for (int j = 0; j < 50; j ++) {
            tempnum = scan.nextDouble();

            tempVec[j] = tempnum;

        }

        this.vertexes[i] = new Word(tempWord, tempVec);

    }

}

词类是:

/* Instance variables */
String word;
double[] vertex = new double[50];

public Word(String word) {
    this.word = word;

}

public Word(String word, double[] vertex) {
    this.word = word;
    this.vertex = vertex;

}

public void setVertexs(double[] vertexs) {
    this.vertex = vertexs;

}

public String getWord() {
    return (this.word);

}

public double getVertex(int index) {
    return (this.vertex[index]);

}

public double[] getVertexs() {
    return (this.vertex);

}

输出示例:
0.26836-0.4585-0.23311-0.52822-1.355 7 0.16098 0.0 0 0.2698 0 0 0.26822-1.355 7 0 0 0.16098 0.0 0.37691 0 0.37691-0.0 0 0 0.37691 0.0 0 0 0 0.26680 0 0.6768680 0 0.0 0 0 0.52822-0 0 0.52822-1.355 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.2698 0 0 0 0 0 0.2698 0 0 0 0 0 0 0 0 0 0 0 0 0.37691 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.528 8 8 8 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.5294 0.69956 0.14885 0.0294531.4888 0.52361 0.099354 1.2515 0.099381 -0.079261 -0.30862 0.30893 0.11023
0.167680 0.16768 1.21551 0.49515 0.49515 0.26836 0.26836-0.4585-0.23311-0.52822-1.355 7 0 0.16098 0.2698 0.0 0.37691 0.0 0 0 0.67680 0 0.167680 0 0 0 0 0.26836-0 0.4585-0 0 0.23311-0.52822 0 0 0 0 0 0.52822-1 1.355 7 7 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.52982 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.37288 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.525294 0.69956 0.14885 0.0294531.4888 0.52361 0.099354 1.2515 0.099381 -0.079261 -0.30862 0.30893 0.11023
什么时候应该是:
0.15272 0.15272 0.361810 0.36181-0.221680 0.680 0 0.066051 0.13029 0.37075-0.75874-0.44722 0.2253 0.0 0 0.52520 0.5272 0.0 0.15272 0.0 0.15272 0.0 0 0 0 0 0 0 0 0 0 0 0 0 0.13029 0 0.07575 0 0 0 0 0 0 0 0.75874-0 0 0 0 0.75874-0.44722 0 0 0 0 0 0 0 0 0 0.44727272 0 0 0 0 0 0 0 0 0 0 0 0 0 0.74747 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.090909090909090909090 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3024至0.343220.020339 0.2142 0.044097 0.14003 -0.20079 0.074794 -0.36076 0.43382 -0.084617 0.1214
0.167680 0.16768 1.21551 0.49515 0.49515 0.26836 0.26836-0.4585-0.23311-0.52822-1.355 7 0 0.16098 0.2698 0.0 0.37691 0.0 0 0 0.67680 0 0.167680 0 0 0 0 0.26836-0 0.4585-0 0 0.23311-0.52822 0 0 0 0 0 0.52822-1 1.355 7 7 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.52982 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.37288 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.525294 0.69956 0.14885 0.0294531.4888 0.52361 0.099354 1.2515 0.099381 -0.079261 -0.30862 0.30893 0.11023
所以它得到了正确的单词,但是用从文件中读取的最后50个值覆盖了所有10的double[]数组。我不确定我做错了什么。

相关问题