java - 尝试使用 ArrayList 操作数组中的对象

标签 java arrays list file loops

我正在读取两个不同的文本文件,一个包含关于 Salesperson 的数据,另一个包含每个人的 SalesStats。但是,当我尝试为每个人设置 SalesStats 时,出现索引越界错误,我不确定原因。我在评论中指出了弹出此错误的位置。

问题:如何修复索引越界错误并让每个 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);

最佳答案

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

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

列表为空,您调用了 set 方法,这是不合逻辑的,因为 set 旨在用于覆盖指定索引处的数据,但列表实际上是空的。使用 add方法代替,它将添加到下一个索引,并且还允许您摆脱无用的 currentLine2 微不足道的内存持有者。

文档指定设置

Replaces the element at the specified position in this list with the specified element.

会抛出

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

在您的例子中,给定的索引为 0,大小为 0,这进入子句 index >= size(),因此抛出异常。

关于java - 尝试使用 ArrayList 操作数组中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28845839/

相关文章:

java - 比较通用项目?

java - cronjob 运行java程序

java - 迭代器堆栈 hasNext() 不返回 true

java - 如何在公共(public)类中获取HQL输出

Java - 迭代两个列表,比较然后添加到另一个列表

java网络服务;未生成方法

java - 使用 SharedPreferences 的许多用户登录

php - 在 PHP 中使用 foreach 构建多维数组

c - 动态二维数组崩溃

java - 对于 Java 中的静态帮助器类,使用泛型还是对象类型更好?