文本:
78 87
78 78
54 78
现在在前面加!!!这句,会报空指针异常,不知道为什么
public class mysort {
public static void main(String[] args) throws FileNotFoundException,IOException{
int[][] aryBefore=getAry("myStudentScore.txt");
plusScore(aryBefore);
// ouputScore(aryBefore,"myStudentScore.txt");
}
private static void ouputScore(int[][] aryBefore, String string) {
}
private static void plusScore(int[][] aryBefore){
// System.out.println(aryBefore[0][0]);
int[] sum=new int[aryBefore.length];
for (int i = 0; i < aryBefore.length; i++) {
for (int j = 0; j < aryBefore[i].length; j++) {
sum[j]+=aryBefore[i][j];
}
}
System.out.println(Arrays.toString(sum));
}
private static int[][] getAry(String string) throws FileNotFoundException,IOException {
BufferedReader bufR=new BufferedReader(new FileReader(string));
String lineSrc;
int row=0;
int[][] getAry=new int[0][0];
while ((lineSrc=bufR.readLine())!=null) {
getAry=Arrays.copyOf(getAry, getAry.length+1);
String[] everScore=lineSrc.split(" ");
for (int i = 0; i < everScore.length; i++) {
!!!! getAry[row]=Arrays.copyOf(getAry[row], getAry[row].length+1);
getAry[row][i]=Integer.parseInt(everScore[i]);
// System.out.println(getAry[row][i]+"+"+row);
row++;
}
lineSrc=bufR.readLine();
}
return getAry;
}
}
但是,这不是吧原来的数组长度+1吗,
追答数组的长度是不可变的,你要长度+1,要重新定义一个数组。
这样:int getAry2[row+1]=Arrays.copyOf(getAry[row], getAry[row].length+1);