gpt4 book ai didi

java - for 方法中的索引不增加

转载 作者:行者123 更新时间:2023-12-01 21:35:00 26 4
gpt4 key购买 nike

好吧,这可能看起来很简单,但它已经困扰我好几天了,老实说,我不知道为什么索引不会增加并获取其他数据。除了返回之外我不知道去哪里。我将它分成两行,第一行仅给出数据库中的第一行数据,第二行仅给出数据库中的最后一行数据。 (参见下面注释掉的行)。如何获取符合 if 语句的每一行?

这是我的代码:

public Object[] populateTable(ArrayList<Outlet> outletList, String selection, int size, int monthCtr, String selDay){ 

for(int i = 0; i<outletList.size(); i++){
if(outletList.get(i).getCity().equalsIgnoreCase(selection)){
if(outletList.get(i).getStatus().equals("ACTIVE")){
bar = outletList.get(i).getBarangay();
code = Integer.toString(outletList.get(i).getCode());
name = outletList.get(i).getName();

data = new Object[]{bar, name, code};
//return data ->gives the first one in the database
}
}
}
}
//return data -> gives the last one in the database
}

最佳答案

您需要将所有结果保存在另一个数组中并返回该数组。

public Object[] populateTable(ArrayList<Outlet> outletList, String selection, int size, int monthCtr, String selDay)
{
List<object> result = new List<object>();

for(int i = 0; i<outletList.size(); i++)
{
if(outletList.get(i).getCity().equalsIgnoreCase(selection))
{
if(outletList.get(i).getStatus().equals("ACTIVE"))
{
bar = outletList.get(i).getBarangay();
code = Integer.toString(outletList.get(i).getCode());
name = outletList.get(i).getName();

data = new Object[]{bar, name, code};
result.Add(data);
}
}
}
return result.ToArray();
}

关于java - for 方法中的索引不增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37021204/

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