2D Arraylist错误

时间:2012-12-27 18:40:17

标签: java arraylist compiler-errors bluej

我已经成功创建了一个2D Arraylist,但我仍然无法将我的值存储到Arraylist中。我的错误信息如下,“找不到符号 - 方法添加(int,java.lang.String)。我知道它可能是一个简单的修复,但我无法在线或在我的教科书中的任何地方找到它。而且我也想知道如果有一种更简单的方法来创建2D arraylist。谢谢。

这是我声明2D数组的地方:

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

3 个答案:

答案 0 :(得分:1)

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

ArrayList <Account> account = new ArrayList<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一起使用。这只是为了解释你的例子。

答案 1 :(得分:0)

您应首先创建ArrayList<String>并将字符串添加到此ArrayList。然后将此ArrayList添加到account ArrayList

答案 2 :(得分:0)

你不想要一个字符串的arraylist,你想要一个帐户信息的数组列表,如下所示:

 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