【问题标题】:Trying to manipulate objects in an array using an ArrayList尝试使用 ArrayList 操作数组中的对象
【发布时间】:2015-05-04 22:23:07
【问题描述】:

我正在阅读两个不同的文本文件,一个包含有关 Salesperson 的数据,另一个包含每个人的 SalesStats。但是,当我尝试为每个人设置SalesStats 时,我得到一个索引越界错误,我不知道为什么。我已经用 cmets 记录了这个错误出现的地方。

问题:如何修复索引越界错误并让每个Salesperson 都被赋予正确的SalesStats

文本文件:

PEOPLE:
        1,Skippy,Jones
        2,Rod,Stewart
        3,Betty,Velveta
        4,Gina,Ginger
        5,Paul,Funyun

STATS:
    1   120
    1   130
    1   140
    1   150
    1   160
    1   170
    1   180
    1   190
    1   200
    1   210
    1   220
    1   230
    2   240
    2   250
    2   260
    ETC....

错误信息:

 Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
        at java.util.ArrayList.rangeCheck(ArrayList.java:653)
        at java.util.ArrayList.set(ArrayList.java:444)
        at SalespersonDriver.main(SalespersonDriver.java:72)

代码:

public class SalespersonDriver {

public static void main(String[] args) throws FileNotFoundException {

double monthSales = 0;
                int currentLine = 0;
                int currentLine2 = 0;
                int personNumber = 0;
                int personNumber2 = 0;
                String firstName = "";
                String lastName = "";
                Salesperson[] SalesPeople = new Salesperson[5];
                ArrayList<Double> SalesStats1 = new ArrayList<Double>();
                ArrayList<Double> SalesStats2= new ArrayList<Double>();
                ArrayList<Double> SalesStats3 = new ArrayList<Double>();
                ArrayList<Double> SalesStats4 = new ArrayList<Double>();
                ArrayList<Double> SalesStats5 = new ArrayList<Double>();



                // reads in NewPeople.txt
                        File NewPeople = new File("./src/NewPeople.txt");
                        Scanner fileScanner = new Scanner(NewPeople);

                        // while there is a new line in the data, goes to the next one
                        while (fileScanner.hasNextLine()) {
                            String line = fileScanner.nextLine();
                            Scanner lineScanner = new Scanner(line);
                            lineScanner.useDelimiter("\\,");

                            // while there is a new attribute to read in on a given line, reads data
                            while (lineScanner.hasNext()) {
                                personNumber = lineScanner.nextInt();
                                firstName = lineScanner.next();
                                lastName = lineScanner.next();

                                SalesPeople[currentLine] = new Salesperson(personNumber, firstName, lastName);
                                currentLine++;
                            }

                    // reads in SalesFigures.txt
                            File SalesFigures = new File("./src/SalesFigures.txt");
                            Scanner fileScanner2 = new Scanner(SalesFigures);

                            // while there is a new line in the data, goes to the next one
                            while (fileScanner2.hasNextLine()) {
                                String line2 = fileScanner2.nextLine();
                                Scanner lineScanner2 = new Scanner(line2);
                                lineScanner2.useDelimiter(" ");

                                // while there is a new attribute to read in on a given line, reads data
                                while (lineScanner2.hasNext()) {

                                    personNumber2 = lineScanner2.nextInt();     
                                    monthSales = lineScanner2.nextDouble();     

                                    if(personNumber2 == 1)
                                    {
                                        SalesStats1.set(currentLine2, monthSales); //Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
                                    }
                                    else if(personNumber2 == 2)
                                    {
                                        SalesStats2.set(currentLine2, monthSales);
                                    }
                                    else if(personNumber2 == 3)
                                    {
                                        SalesStats3.set(currentLine2, monthSales);
                                    }
                                    else if(personNumber2 == 4)
                                    {
                                        SalesStats4.set(currentLine2, monthSales);
                                    }
                                    else if(personNumber2 == 5)
                                    {
                                        SalesStats5.set(currentLine2, monthSales);
                                    }

                                    currentLine2++;
                                }

                                SalesPeople[0].setSalesStats(SalesStats1);
                                SalesPeople[1].setSalesStats(SalesStats2);
                                SalesPeople[2].setSalesStats(SalesStats3);
                                SalesPeople[3].setSalesStats(SalesStats4);
                                SalesPeople[4].setSalesStats(SalesStats5);

【问题讨论】:

  • 请编辑您的问题以包含完整的堆栈跟踪。
  • 请注明你的SalespersonDriver.java文件的第72行。

标签: java arrays list file loops


【解决方案1】:

ArrayList.set(int index, E element) 方法将抛出该异常,如果

IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())

您可能应该使用 ArrayList.add(int index, E element) 方法,因为如果

IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size())

一大于一大于等于

如果还没有 0,则不能设置(0,元素)

【讨论】:

    【解决方案2】:

    调用ArrayListset 方法时您的代码失败。

    您实例化SalesStats1-5,然后在它们上调用set,而无需添加任何数据。

    列表为空,您调用 set 方法是不合逻辑的,因为 set 旨在用于覆盖指定索引处的数据,但列表实际上是空的。改用add 方法,它会添加到下一个索引,还可以让你摆脱无用的currentLine2 微不足道的内存持有者。

    文档指定set

    将此列表中指定位置的元素替换为 指定元素。

    会抛出

    IndexOutOfBoundsException - 如果索引超出范围(索引 = 大小())

    在您的情况下,给定的索引为 0,大小为 0,进入子句 index &gt;= size(),因此抛出异常。

    【讨论】:

    • 所以我可以只做 SalesStats1.add(monthSales);它会在循环时更新每个连续的索引?
    • @Jordan 是的,它会一直添加到下一个索引,试试看。
    • 除了 SalesPeople[0] 之外,数组中 SalesPeople 中的所有对象都出现空指针异常。您认为我创建 Salesperson 对象的方式有问题吗?
    • 打印您将 SalesPerson 添加到数组的位置,以查看它是否添加了超过一个人。
    猜你喜欢
    • 2019-01-14
    • 2016-03-05
    • 1970-01-01
    • 1970-01-01
    • 2022-08-08
    • 2014-08-29
    • 2020-06-30
    • 2021-04-11
    • 1970-01-01
    相关资源
    最近更新 更多