英文:
Populating a list inside a for loop with an if condition Java
问题
我正在尝试填充与数据库中的账户相关的整数列表。然而,当我填充并返回该列表时,列表只显示一个元素。我认为这与 ownList.add(owner.getId())
处于 if
的范围有关。当我将该语句放在 for
范围的 if 语句之外时,列表为 null
。我应该如何解决这个问题,以返回完整的账户 ID 列表?
以下是该方法的代码。
public List<Integer> getAllAccountIDs(List<Account> allAccounts) {
List<Integer> ownList = new ArrayList<>();
allAccounts = accDao.findAll();
try (Connection conn = ConnectionUtil.getConnection()) {
String sql = "SELECT id FROM project0.accounts";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
int accId;
allAccounts = accDao.findAll();
while (rs.next()) {
accId = rs.getInt("id");
Account owner = null;
for (int i = 0; i < allAccounts.size(); i++) {
if (allAccounts.get(i).getId() == accId) {
owner = allAccounts.get(i);
ownList.add(owner.getId());
}
}
return ownList;
}
} catch (SQLException e) {
e.printStackTrace();
System.out.println("OWNER id 不存在任何账户");
return null;
}
return null;
}
注意:此处给出的代码是您提供的内容的翻译部分。如果需要进一步的解释或帮助,请随时提问。
英文:
I am trying to populate a list of integers relating to accounts in a database. However, when I populate it and return it, the list only shows one element. I believe it has to do with the ownList.add(owner.getId()
being in the scope of the if
. When I put that statement outside of the if statement in the for
scope the list is null
. How should I remedy this to return the full list of account Ids?
Here's my code for that method.
public List<Integer>getAllAccountIDs(List<Account>allAccounts) {
List<Integer>ownList=new ArrayList<>();
allAccounts=accDao.findAll();
try (Connection conn =ConnectionUtil.getConnection()){
String sql = "SELECT id FROM project0.accounts";
Statement stmt =conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
int accId;
allAccounts =accDao.findAll();
while (rs.next()) {
accId=rs.getInt("id");
Account owner=null;
for(int i=0; i <allAccounts.size();i++) {
if(allAccounts.get(i).getId()==accId) {
owner=allAccounts.get(i);
ownList.add(owner.getId());
}
return ownList;
}
}
}catch(SQLException e) {
e.printStackTrace();
System.out.println("NO ACCOUNT EXIST FOR OWNER id");
return null;
}
return null;
}
}
答案1
得分: 1
你的语句 return ownList
位于 for 循环内部。该函数会执行你编写的操作,也就是在第一次遍历后返回结果,并在添加第一条记录后立即返回。
你可能希望将 return
移动到 for 循环之后(或者在 while 循环之后)。
英文:
Your statement return ownList
is inside for-loop. The function will do what you programmed, that is, return result after the first pass and just after adding the first record.
You might want to move return
after for
loop (or after while
).
答案2
得分: 0
问题在于在嵌套的 for 循环的第一次迭代后就进行了 return
:
for (int i = 0; i < allAccounts.size(); i++) {
if (allAccounts.get(i).getId() == accId) {
owner = allAccounts.get(i);
ownList.add(owner.getId());
}
return ownList;
^^^^^^
}
在满足 allAccounts.size()
条件后,您应该在添加新项时进行追加,而不是仅添加一次并离开函数。
英文:
The isue is that you return
after the first iteration of the nested for loop:
for(int i=0; i <allAccounts.size();i++) {
if(allAccounts.get(i).getId()==accId) {
owner=allAccounts.get(i);
ownList.add(owner.getId());
}
return ownList;
^^^^^^
}
Instead of appending a new item after satisfying the condition for allAccounts.size()
, you add an item once and leave the function
答案3
得分: 0
你需要按以下方式修改你的函数:
public List<Integer> getAllAccountIDs(List<Account> allAccounts) {
List<Integer> ownList = new ArrayList<>();
try (Connection conn = ConnectionUtil.getConnection()) {
String sql = "SELECT id FROM project0.accounts";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
int accId = rs.getInt("id");
Account owner = null;
for (int i = 0; i < allAccounts.size(); i++) {
if (allAccounts.get(i).getId() == accId) {
owner = allAccounts.get(i);
ownList.add(owner.getId());
}
}
}
return ownList;
} catch (SQLException e) {
e.printStackTrace();
System.out.println("NO ACCOUNT EXIST FOR OWNER id");
}
return null;
}
确保在 try
块的末尾返回有效的列表,而不是在你的 for
循环的第一次迭代之后返回。
英文:
You need to modify your function as follows:
public List < Integer > getAllAccountIDs(List < Account > allAccounts) {
List < Integer > ownList = new ArrayList < > ();
allAccounts = accDao.findAll();
try (Connection conn = ConnectionUtil.getConnection()) {
String sql = "SELECT id FROM project0.accounts";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
int accId;
allAccounts = accDao.findAll();
while (rs.next()) {
accId = rs.getInt("id");
Account owner = null;
for (int i = 0; i < allAccounts.size(); i++) {
if (allAccounts.get(i).getId() == accId) {
owner = allAccounts.get(i);
ownList.add(owner.getId());
}
}
}
return ownList;
} catch (SQLException e) {
e.printStackTrace();
System.out.println("NO ACCOUNT EXIST FOR OWNER id");
}
return null;
}
to make sure that the valid list is returned at the end of try
block not after the first iteration of your for-loop
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论