【问题标题】:2D Arraylist Error二维数组列表错误
【发布时间】:2012-12-13 03:08:53
【问题描述】:

我已经成功创建了一个 2D Arraylist,但我仍然无法将我的值存储到 Arraylist 中。我的错误消息如下,“找不到符号 - 方法 add(int,java.lang.String)。我知道这可能是一个简单的修复,但我在网上或教科书中的任何地方都找不到它。我也想知道如果有更简单的方法来创建二维数组列表。谢谢。

这里是我声明二维数组的地方:

ArrayList <ArrayList<String>> account = new ArrayList<ArrayList<String>>();

这是我的代码:

public void newAccount()
{
    firstName = JOptionPane.showInputDialog("What's your first name?");
    nLastName = JOptionPane.showInputDialog("What's your last name?");
    nAddress = JOptionPane.showInputDialog("What's your current address?");
    nCity= JOptionPane.showInputDialog("What's your current city?");
    nState = JOptionPane.showInputDialog("What's your current State?");
    nZipCode = JOptionPane.showInputDialog("What's your current Zip Code?");
    account.add( accountNumber, firstName);
    account.add( accountNumber, nLastName);
    account.add( accountNumber, nAddress);
    account.add( accountNumber, nCity);
    account.add( accountNumber, nState);
    account.add(accountNumber, nZipCode);

【问题讨论】:

    标签: java arraylist compiler-errors bluej


    【解决方案1】:

    更好的方法是使用对象而不是字符串:

    ArrayList <Account> account = new ArrayList<Account>();
    

    这是 Account 类:

    public class Account{
        public String firstName;
        public String nLastName;
        public String nAddress;
        public String nCity;
        public String nState;
        public String nZipCode; 
    }
    

    并添加到列表中:

    public void newAccount()
    {
        Account a = new Account();
    
        a.firstName = JOptionPane.showInputDialog("What's your first name?");
        a.nLastName = JOptionPane.showInputDialog("What's your last name?");
        a.nAddress = JOptionPane.showInputDialog("What's your current address?");
        a.nCity= JOptionPane.showInputDialog("What's your current city?");
        a.nState = JOptionPane.showInputDialog("What's your current State?");
        a.nZipCode = JOptionPane.showInputDialog("What's your current Zip Code?");
    
        account.add(a);
    }
    

    您可以更改可见性并将其与 setter/getter 一起使用。这只是为了向您解释示例。

    【讨论】:

    • 我有一个循环,因此我可以创建任意数量的帐户,此代码会与之前的帐户重叠吗?
    • 没有。每次向account 添加一个新的。 a 也只在被调用的方法中有效。如果你多次调用它,那么你有多个对象,它们将被添加到account。试试看:)
    • 非常感谢,您可能又为我节省了 4 个小时的上网时间:)
    【解决方案2】:

    您应该首先创建一个ArrayList&lt;String&gt; 并将您的字符串添加到此ArrayList。然后将此ArrayList 添加到您的account ArrayList

    【讨论】:

      【解决方案3】:

      你不想要一个字符串的数组列表,你想要一个帐户信息的数组列表,像这样:

       class AccountEntry{
          public String firstName;
          public String lastName;
          public int address, city, state, zipCode;
       }
      
       ArrayList<AccountEntry> accounts = new ArrayList<AccountEntry>();
      
       AccountEntry entry = new AccountEntry();
       entry.firstName = JOptionPane.showInputDialog("What's your first name?");
       //..and so on
       accounts.add( entry ); //add the account
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-09-26
        • 1970-01-01
        • 2015-11-29
        • 1970-01-01
        • 1970-01-01
        • 2017-02-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多