gpt4 book ai didi

java - 如何正确地将值赋给对象数组?

转载 作者:行者123 更新时间:2023-12-01 17:02:13 24 4
gpt4 key购买 nike

我不知道为什么在运行代码后,数组 scoreMatrix 总是将最后一个赋值给整个数组。

        String input, seq="ACGT";
PairScore[] scoreMatrix = new PairScore[16];
for(int x = 0; x<16 ; x++){
scoreMatrix[x] = new PairScore();
}
Scanner user_input = new Scanner( System.in );
for(int i = 0; i <4;i++){
input=user_input.nextLine();
String[] inputSplite = input.split("\\s+");
for(int j=0; j<4;j++){
scoreTable[i][j] = Integer.parseInt(inputSplite[j]);
}
}
for(int x = 0; x<4 ; x++){
for(int y=0; y<4; y++){
scoreMatrix[x*4 +y].fString = Character.toString(seq.charAt(x));
scoreMatrix[x*4 +y].sString = Character.toString(seq.charAt(y));
scoreMatrix[x*4 +y].score = scoreTable[x][y];
}
}// Fix the score pair problems;
for(int x1 = 0; x1<4 ; x1++){
for(int y1=0; y1<4; y1++){
System.out.println(scoreMatrix[x1*4 +y1].fString+scoreMatrix[x1*4 +y1].sString+scoreMatrix[x1*4 +y1].score);
}
}

这是 PairScore 类的代码

public class PairScore {
public static String fString;
public static String sString;
public static int score;

}

当我输入时

1 -1 -1 -1
-1 1 -1 -1
-1 -1 1 -1
-1 -1 -1 1

我得到了以下结果:

TT1TT1TT1TT1.....TT1

但我希望从代码中得到以下结果:

AA1AC-1AG-1AT-1CA-1CC1...TT1

为什么它采用值 TT1 并将其分配给数组的其余部分?

最佳答案

确保PairScore 的成员(fStringsStringscore)不是静态的。

如果它们是静态的,PairScore 类的所有实例将共享这三个成员的单个值,因此您只能看到分配给这些变量的最后一个值。

关于java - 如何正确地将值赋给对象数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26956545/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com