我不知道为什么在运行代码之后,数组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);
}
}
Run Code Online (Sandbox Code Playgroud)
以下是PairScore类的代码
public class PairScore {
public static String fString;
public static String sString;
public static int score;
Run Code Online (Sandbox Code Playgroud)
}
当我输入
1 -1 -1 -1
-1 1 -1 -1
-1 -1 1 -1
-1 -1 -1 1
Run Code Online (Sandbox Code Playgroud)
我得到了以下结果:
TT1TT1TT1TT1.....TT1
Run Code Online (Sandbox Code Playgroud)
但我希望从代码中得到以下结果:
AA1AC-1AG-1AT-1CA-1CC1...TT1
Run Code Online (Sandbox Code Playgroud)
为什么它取值TT1并将其分配给数组的其余部分?
确保PairScore(fString,sString和score)的成员不是静态的.
如果它们是静态的,则PairScore类的所有实例将共享这三个成员的单个值,因此您只能看到分配给这些变量的最后一个值.