关于2维数组

时间:2015-11-22 11:35:17

标签: java arrays

我们有关于二维数组的预先分配。问题是这样的。将接受姓名和分数的计划。将数据存储在二维数组上。然后显示学生列表及其分数,并获得最高分。

由于我们的教练教我们基于阵列的基础我不太了解这个2D阵列我已经做了很多研究,但仍然无法得到我需要的确切代码。

以下是我未完成的代码。到目前为止我的问题是我无法打印所有的名字和分数。它只打印输入的最后数据。

任何帮助都会做tnx。

batchSize

}

2 个答案:

答案 0 :(得分:0)

创建像

这样的二维数组

String[][] arr2d = new String[students][2];

在你的循环中,你可以使用

arr2d[i][0] = name;

arr2d[i][1] = fscore;

要检查最高分,请迭代数组并比较分数字段(arr2d[i][1])。

答案 1 :(得分:0)

二维数组在Java中被称为数组数组,因为Java中没有二维数组。为什么?因为如果是2D数组则应将它们存储在连续的内存位置。阅读2D阵列。阅读here有关2D数组或数组数组如何工作的信息。

String[][] studentList = new String[10][2];

第一个字段表示行数,第二个字段表示您的案例中的列数。因此,上面将采用10个学生阵列,其中每个学生阵列都有两个元素[0,1]。

我已经开发了一个示例代码示例,以帮助您理解数组,如何使用try和catch,异常和循环。

public static void main(String[] args) {
        //your 2D array or array of arrays def
        String[][] studentList = null;

        Scanner input = new Scanner(System.in);

        //temporary students count 
        int students = 1;

        // i is used to take number of students, -1 to initialize do while loop
        int i = -1; 

        do {
            try {
                //executed only once 
                if (i == -1) {
                    System.out.println("Enter the number of students : ");

                    // if you provide anything but integer, this throws 
                    // InputMisMatchException so validate if
                    // Also validate against negative integer input 
                    students = input.nextInt();

                    // move the input cursor to beginging to line
                    // this is because of using input.nextInt() before
                    input.nextLine();

                    //Initialize 2D array, make sure students is +ve
                    studentList = new String[students][2];

                    //increment i to 1 so above if condition fail
                    i++;
                } else {
                    System.out.println("Enter Name for Student " + i + " :");
                    studentList[i][0] = input.nextLine();

                    System.out.println("Enter Score for " + studentList[i][0] + " : ");
                    studentList[i][1] = input.nextLine();
                    i++;

                    //all above inputs can reside inside one try/catch block
                    //no need for extra try/catch
                }

            } catch (Exception e) { 
                // all exceptions caught here, any type because Exception is
                // parent of all others i.e. InputMisMatchException etc.
                System.out.println("Error enter name!");
            }
        //loop breaks after n student is inputed 
        } while (i < students);

        // close the input stream
        input.close();

        //making sure that student has been initialized and has student data
        if (studentList != null && studentList.length > 0) {
            System.out.println("The students are as follow: ");
            // To display students yo need to iterate through them using loop  
            for (int j = 0; j < students; j++) {
                System.out.println(studentList[j][0] + " : " + studentList[j][1]);
            }
        }
    }
相关问题