如何正确地将值分配给对象数组?

时间:2014-11-16 11:45:22

标签: java arrays

我不知道为什么在我运行代码之后,数组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并将其分配给数组的其余部分?

1 个答案:

答案 0 :(得分:5)

确保PairScorefStringsStringscore)的成员不是静态成员。

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

相关问题