简体   繁体   English

在arraylists的arraylist中出界

[英]Out of bounds in an arraylist of arraylists

Whenever i try to add an element to the 1st internal array it gives an out of bounds exception. 每当我尝试向第一个内部数组添加一个元素时,它就会产生一个超出范围的异常。 I'm trying to create a 2d arraylist to store a name in the 1st slot then 30 grades. 我正在尝试创建一个2d arraylist来存储第一个插槽中的名称,然后是30个等级。 I cannot figure out how to add anything to the internal arraylist. 我无法弄清楚如何向内部arraylist添加任何东西。 The error is being thrown ((ArrayList)matrix.get(i)).set(0, nameIn); 抛出错误((ArrayList)matrix.get(i))。set(0,nameIn); on that line. 在那条线上。

import java.util.*;
/**
 * 
 * @author mitchell castaldini 
 * @version 5/5/2016
 */
public class gradebookTester
{
    public static void main(String args[])
    {
        Scanner in = new Scanner(System.in);
        /*List<List<String>> lists = new ArrayList<List<String>>();
        for(int i = 0; i < numNames;i++)
        {
            lists.add(new ArrayList<String>());
        }*/

        ArrayList matrix = new ArrayList();

        //matrix.add(row1);
        matrix.add(new ArrayList());
        matrix.add(new ArrayList());
        matrix.add(new ArrayList());
        matrix.add(new ArrayList());
        matrix.add(new ArrayList());
        matrix.add(new ArrayList());
        matrix.add(new ArrayList());
        matrix.add(new ArrayList());
        matrix.add(new ArrayList());
        matrix.add(new ArrayList());
        matrix.add(new ArrayList());
        matrix.add(new ArrayList());
        matrix.add(new ArrayList());
        matrix.add(new ArrayList());
        matrix.add(new ArrayList());
        matrix.add(new ArrayList());
        matrix.add(new ArrayList());
        matrix.add(new ArrayList());
        matrix.add(new ArrayList());
        matrix.add(new ArrayList());
        matrix.add(new ArrayList());
        matrix.add(new ArrayList());
        matrix.add(new ArrayList());
        matrix.add(new ArrayList());
        matrix.add(new ArrayList());
        matrix.add(new ArrayList());
        matrix.add(new ArrayList());
        matrix.add(new ArrayList());
        makeArrays(matrix);
    }
    private static void makeArrays(ArrayList matrix)
    {
        ArrayList row1 = new ArrayList();
        matrix.add(row1);
        Scanner in = new Scanner(System.in);
        int a = 2;

        for(int i = 0; i < a; i++)
        {
            System.out.println("Add name");
            String nameIn = in.next();
            row1.set(0, nameIn);
            //((ArrayList)matrix.get(i)).set(0, nameIn);
            for(int d = 0; d < 10;d++)
            {
                System.out.println("Add the " + (d+1) + " grade");
                int gradeIn = in.nextInt();

                ((ArrayList)matrix.get(i)).set(d, gradeIn);
            }
        }
    }
}

Take a look at the documentation for the ArrayList.set method. 看一下ArrayList.set方法的文档。 You will see that it replaces the element at the given position. 您将看到它替换给定位置的元素。 Because your lists are empty it will throw an error at this point. 因为您的列表是空的,所以此时会抛出错误。

You could use add to add the name to the list at this point. 此时您可以使用add将名称add到列表中。

There are serious flaws in your code that you would be wise to address. 您的代码中存在严重缺陷,您应该明智地解决这些缺陷。 You intend to store strings (name) and integers (grades) in a single list. 您打算将字符串(名称)和整数(成绩)存储在单个列表中。 That's almost always a sign of the design flaw of storing multiple domains of data in a single collection. 这几乎总是表明在单个集合中存储多个数据域的设计缺陷。

Two options to address this are: 解决这个问题的两个选择是:

  • use a Map<String,List<Integer>> grades to store your grades. 使用Map<String,List<Integer>> grades来存储您的成绩。 Then use grades.put(name, new ArrayList<>()) and grades.get(name).add(grade) . 然后使用grades.put(name, new ArrayList<>())grades.get(name).add(grade)

  • Better would be to define a StudentGrade class that stores the name and list of grades. 更好的方法是定义一个存储名称和成绩列表的StudentGrade类。 You could then have a List<StudentGrade> or Map<String,StudentGrade>> to store the list of student grades. 然后,您可以使用List<StudentGrade>Map<String,StudentGrade>>来存储学生成绩列表。 This would make your code much clearer. 这将使您的代码更清晰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM