尝试使用带有ArrayList的for循环时出现.length错误

时间:2014-04-12 15:37:53

标签: java arrays

首先,我很抱歉这个标题我想不出更好的方式来说出来。实际错误在选项3中(每当我尝试将选项1中的所有销售加在一起时)。当我尝试使用salesList.length跟踪数组的大小时,我得到cannot find symbol- variable length我对使用数组列表非常新,并且该方法在早期数组中对我有用,但该数组不是动态的。是否有特定的方法来跟踪动态数组列表的长度?

import java.util.*;
public class CustomerTest
{
public static void main(String[] args)
        {
            double totalSales = 0;
            ArrayList<String> nameList;
            nameList = new ArrayList<String>();
            ArrayList<Double> salesList;
            salesList = new ArrayList<Double>();
            Scanner myScanner = new Scanner(System.in);
            boolean done = true;     
            do
            {

                System.out.println("1) Add a new customer \n 2) Print all customers \n 3) Compute and print the total sales \n 4) Quit");
                int choice = Integer.parseInt(myScanner.nextLine());
                if (choice == 1)
                {
                    System.out.print("Add a new customer ");
                    String answer = myScanner.nextLine();
                    nameList.add(answer);
                    System.out.print("Enter their sales ");
                    String answer2 = myScanner.nextLine();
                    double answer3 = Double.parseDouble(answer2);
                    salesList.add(answer3);
                }
                else if(choice == 2)
                {
                    System.out.println("Customers: " + nameList);
                    System.out.println("Sales: " + salesList);
                }
                else if(choice == 3)
                {
                    for(int i = 0; i < salesList.length; i++)
                    {
                     totalSales = totalSales + salesList[i];   
                    } 
                    System.out.println(totalSales);
                }
                else if(choice == 4)
                {
                    System.out.println("Goodbye   *Bows gracefully*"); 
                    done = false;
                }
                else
                    System.out.println("Invalid Choice");     
            }
            while (done);
           System.exit(0);
        }
}

5 个答案:

答案 0 :(得分:1)

将其更改为salesList.size();。与数组不同,ArrayList的长度不是可直接访问的字段。

答案 1 :(得分:1)

数组包含length字段

ArrayList没有长度字段类型使用size()

答案 2 :(得分:1)

使用salesList.size()。与数组不同,您无法使用salesList.lenght

答案 3 :(得分:1)

else if (choice == 3) {
        for (int i = 0; i < salesList.size(); i++) {
            totalSales += salesList.get(i);
        }
        System.out.println(totalSales);
        }

用这个替换选择3,它应该有用。

答案 4 :(得分:0)

您的代码存在错误:将其他if(choice==3) {}条件部分更改为以下内容。您无法使用salesList.length,可以使用salesList.size()并请求更改salesList[i] to salesList.get(i).

 else if(choice == 3)
                {
                    for(int i = 0; i < salesList.size(); i++)
                    {
                     totalSales += salesList.get(i);   
                    } 
                    System.out.println(totalSales);
                }