在 java 如何让用户在参差不齐的数组中输入列数,但对于每一行,列数应该不同

In java how to make the user enter number of columns in a ragged array but for each row the number of columns should be different

在 java 中,我想创建并打印一个参差不齐的数组,用户首先必须输入行数,然后在每一行中,用户必须输入数字列,然后输入任何内容他在每一列中想要的数字,直到他达到他为每一行输入的数字, 例如

4 11 22 33 44
2 51 8
6 92 1 3 5 3 99

在第一行中,用户输入了 4,因此他可以输入 四个数字

在第二行中,用户输入了 2,因此他可以输入 两个数字

在第三行用户输入了 6 所以他可以输入 四个数字

之后它应该打印用户为每一行输入的数字以及他输入的任何内容(它应该打印上面的例子)

但出于某种原因使用我的代码

int rows , col, m=0 , z=0 ;

System.out.println("enter number of rows");
rows=input.nextInt();
        
while(z<rows){
System.out.println("in each row enter number of coloms and then enter whatever number you want in there");
col=input.nextInt();
z++;
for(int i =0 ; i<col ; i++) {
m=input.nextInt();}
}
            
int [][] test1 = new int [rows][m];

for(int i =0 ; i<test1.length ; i++) {
for( int j =0 ; j<test1[i].length ; j++)
System.out.print(test1[i][j]+ " ");
System.out.println();}      


所有输出都是零,但用户首先输入的行数是正确的,所以我对此没有问题

所以不要像这样输出

enter number of rows
3
in each row enter number of coloms and then enter whatever number you want in there
4 11 22 33 44 // for example the user will enter these numbers and it will be printed the way he typed it
2 51 8
6 92 1 3 5 3 99

但我得到了这个输出

enter number of rows
3
4 11 22 33 44  // if the user have entered these numbers it will print all of the array zeros depending on the first number in the last row 
2 51 8
6 92 1 3 5 3 99

//  this is what I get
0 0 0 0 0 0 
0 0 0 0 0 0 
0 0 0 0 0 0 

我整天都在寻找解决方案,但我什么也没找到,有人知道如何解决这个问题吗?

很抱歉让您阅读所有内容

通过使用 ArrayList 而不是 int "array" 解决:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("How many rows?");
    int rowsSize = input.nextInt();
    List<List<Integer>> rows = new ArrayList<>();
    for (int i = 0; i < rowsSize; i++) {
        System.out.println("How many columns?");
        int colSize = input.nextInt();
        List<Integer> cols = new ArrayList<>();
        for (int j = 0; j < colSize; j++) {
            System.out.println("Write a number");
            cols.add(input.nextInt());
        }
        rows.add(cols);
    }
    for (List<Integer> row : rows) {
        for (Integer number : row) {
            System.out.print(number);
        }
        System.out.println("");
    }
}

正如 Alex 所提到的,数组不适合这种动态调整大小。 大多数时候应该首选 ArrayList 而不是数组,除非 memory/speed 非常 关键。

编辑: 通过使用二维数组为您的问题编写了一个解决方案,但我的建议是更喜欢列表: public class WhosebugQuestion {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("How many classes?");
    int numberOfClasses = input.nextInt();
    int largestNumberOfMarksInAClass = numberOfClasses;
    int[][] classesAndMarks = new int[numberOfClasses][largestNumberOfMarksInAClass];
    for (int i = 0; i < numberOfClasses; i++) {
        System.out.println("How many marks in class  " + (i + 1) + "?");
        int numberOfMarks = input.nextInt();
        if (numberOfMarks > largestNumberOfMarksInAClass) { //If the new class has an higher number of marks,
            largestNumberOfMarksInAClass = numberOfMarks;   //we have to increase the size of the 2d array
            classesAndMarks = getResizedArray(numberOfClasses, largestNumberOfMarksInAClass, classesAndMarks);
        }
        for (int j = 0; j < numberOfMarks; j++) {
            System.out.println("Please write a mark");
            classesAndMarks[i][j] = input.nextInt();
        }
    }

    int highestAverageIndex = 0;
    double[] averageMarks = averageAllClassesMarks(classesAndMarks);
    System.out.println("Class average");
    for (int i = 0; i < averageMarks.length; i++) {
        System.out.println((i + 1) + " " + averageMarks[i]);
        if (averageMarks[i] > averageMarks[highestAverageIndex]) {
            highestAverageIndex = i;
        }
    }
    System.out.println("The highest average is " + averageMarks[highestAverageIndex] +
            " and it is for class " + (highestAverageIndex + 1));
}

private static int[][] getResizedArray(int numberOfClasses, int largestNumberOfMarksInAClass, int[][] classesAndMarks) {
    int[][] resizedArray = new int[numberOfClasses][largestNumberOfMarksInAClass];
    for (int i = 0; i < classesAndMarks.length; i++) {
        int[] mark = classesAndMarks[i];
        for (int j = 0; j < mark.length; j++) {
            resizedArray[i][j] = mark[j];
        }
    }
    return resizedArray;
}

private static double[] averageAllClassesMarks(int[][] classesAndMarks) {
    double[] averageAllClasses = new double[classesAndMarks.length];
    for (int i = 0; i < classesAndMarks.length; i++) {
        averageAllClasses[i] = averageClassMarks(classesAndMarks[i]);
    }
    return averageAllClasses;
}

private static double averageClassMarks(int[] marks) {
    double sum = 0;
    double numberOfMarks = 0;
    for (int mark : marks) {
        sum += mark;
        if (mark != 0) {
            numberOfMarks++;
        }

    }
    return Math.round((sum / numberOfMarks) * 100.0) / 100.0;
}

}