【发布时间】:2012-12-27 20:34:35
【问题描述】:
我想知道是否可以将对象存储到用户想要的数组列表中。对于我的程序,它通过“帐号”将用户数据存储到他们选择的单元格中,但是每次我输入新帐号时,它都会说数组基本上不够大。这是我的代码。如果有人可以提供帮助,将不胜感激。
ArrayList <Account> account = new ArrayList<Account>();
int accountNumber;
String nCity;
String nState;
String nZipCode;
String nLastName;
String nAddress;
String firstName;
String nAccount;
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?");
String num = JOptionPane.showInputDialog("What do you want your account number to be?");
accountNumber = Integer.parseInt(num);
account.add(accountNumber, a);
【问题讨论】:
-
在添加或使用
Map结构之前使列表足够大。 -
@jlordo 如何让它足够大,我似乎无法找到在哪里放置数组列表应该有多大的值?
-
@Lightning.. 你确定你能编译那个代码吗?就目前而言,您的代码甚至无法编译。你是如何运行它的?
-
@RohitJain 它为我编译并运行,但随后它说“java.lang.IndexOutOfBoundsException:索引:1,大小:0(在 java.util.ArrayList 中)。我需要初始大小数量庞大,因此可以在创建新帐户时添加它们。
-
@Lightning
while (list.size() < needed_size) list.add(null);但Map会更适合您的用例。