I've successfully created a 2D Arraylist but I still am having issues storing my values into the Arraylist. My error message is as follows, "cannot find symbol - method add(int,java.lang.String). I know its probably a simple fix but I couldn't find it online or anywhere in my textbook. And I also am wondering if there is a simpler way to create a 2D arraylist. Thanks.
Here is where I declare the 2D array:
ArrayList <ArrayList<String>> account = new ArrayList<ArrayList<String>>()
;
Here is my code:
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);
Better way is to use object instead of Strings:
ArrayList <Account> account = new ArrayList<Account>();
And this is the Account class:
public class Account{
public String firstName;
public String nLastName;
public String nAddress;
public String nCity;
public String nState;
public String nZipCode;
}
And addition to list:
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);
}
You can change visibility and use it with setter/getter. This is just to explain you the example.
您应该首先创建一个ArrayList<String>
并将字符串添加到该ArrayList
然后将该ArrayList
添加到您的account
ArrayList
。
You dont want an array list of strings, you want an array list of Account Information, like so:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.