gpt4 book ai didi

使用 ArrayList 时出现 java.lang.IndexOutOfBoundsException : Index: 0, 大小:0

转载 作者:行者123 更新时间:2023-12-01 09:00:57 28 4
gpt4 key购买 nike

我正在尝试存储所有这些元素,但收到类似 java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 的错误,并且我不确定我的错误是什么。

public List<AddUserInformation> insertData(AddUserDetail user) {
List<AddUserInformation> inserted = null;
try{
List<AddUserInformation> addUserInformation = new ArrayList<AddUserInformation>() ;
ArrayList accName = new ArrayList();

for (int i = 0; i < user.getListofaccounts().toArray().length ; i++) {
accName.add(user.getListofaccounts().get(i).getAccountName());
}

List<AddUserInformation> addUserInformation1 = new ArrayList<AddUserInformation>();
for (int i = 0; i< accName.toArray().length; i++) {
addUserInformation1.get(i).setUserID(user.getUserId());
addUserInformation1.get(i).setUsername(user.getUsername());
addUserInformation1.get(i).setPassword(user.getPassword());
addUserInformation1.get(i).setEmailid(user.getEmailid());
List<AddUserAccountDetails> adduseraccountdetails = new ArrayList<>();
addUserInformation1.get(i).setAccountName(user.getListofaccounts().get(i).getAccountName());
addUserInformation1.get(i).setPermission_id(user.getListofaccounts().get(i).getPermission_id());
}
inserted = adduserInformationDao.saveAddUserInfo(addUserInformation1);

} catch (Exception e) {
LOGGER.error("Exception in adding of account to a user " + user.getUsername(), e);
}
return inserted;
}

问题是 accName 包含帐户数量,可以将“n”表示为列表,并在第二个“for”循环中最终替换新值并显示最后的列表值。

最佳答案

声明:

List<AddUserInformation> addUserInformation1 = new ArrayList<AddUserInformation>();

创建一个新的空ArrayList ,所以当你写addUserInformation1.get(i)时在 for 循环中,您正在尝试访问索引 0 处的元素但列表仍然为空( 0 处没有元素),因此您会遇到异常。

在尝试 add(...) 之前,您必须向列表中添加一些内容(例如,调用 get(...) 方法)元素。

关于使用 ArrayList 时出现 java.lang.IndexOutOfBoundsException : Index: 0, 大小:0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41670710/

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