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

时间:2015-03-04 02:47:38

标签: 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)

CODE:

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);

2 个答案:

答案 0 :(得分:1)

调用set的{​​{1}}方法时,您的代码失败。

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

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

文档指定currentLine2

  

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

并将抛出

  

IndexOutOfBoundsException - 如果索引超出范围(索引&lt; 0 ||   index&gt; = size())

在您的情况下,给定的索引为0,大小为0,进入子句set,因此抛出异常。

答案 1 :(得分:0)

ArrayList.set(int index,        如果

,E元素)方法将抛出该异常
IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())

你应该使用ArrayList.add(int index,        E元素)方法,因为如果

将抛出异常
IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size())

一个大于,一个大于或等于

如果没有0

你就不能设置(0,元素)