gpt4 book ai didi

java - 2D 数组列表错误

转载 作者:太空宇宙 更新时间:2023-11-04 07:53:11 25 4
gpt4 key购买 nike

我已经成功创建了一个 2D Arraylist,但在将值存储到 Arraylist 时仍然遇到问题。我的错误消息如下:“找不到符号 - 方法 add(int,java.lang.String)。我知道这可能是一个简单的修复,但我无法在网上或教科书中的任何地方找到它。而且我也想知道是否有更简单的方法来创建 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);

最佳答案

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

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 一起使用。这只是为了向您解释示例。

关于java - 2D 数组列表错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14059554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com