gpt4 book ai didi

java - 如何将ArrayList限制为15个?

转载 作者:行者123 更新时间:2023-12-01 06:45:58 28 4
gpt4 key购买 nike

我有 2 个数组列表。我迭代list1并将与seat_no匹配的数据存储到list2的索引中。

列表1包含:

|seat_no | id     |
===================
| 1 | 001 |
| 2 | 002 |
| 3 | 003 |
| 4 | 004 |
| 7 | 005 |
| 10 | 006 |
| 11 | 007 |

列表2包含:

|index   | id     | seat_no |
=============================
| 1 | 001 | 1 |
| 2 | 002 | 2 |
| 3 | 003 | 3 |
| 4 | 004 | 4 |
| 5 |"blank" |"blank" |
| 6 |"blank" |"blank" |
| 7 | 005 | 7 |
| 8 |"blank" |"blank" |
| 9 |"blank" |"blank" |
| 10 | 006 | 10 |
| 11 | 007 | 11 |

list2 应该包含什么:其中 list2 的限制必须为 15

|index   | id     | seat_no |
=============================
| 1 | 001 | 1 |
| 2 | 002 | 2 |
| 3 | 003 | 3 |
| 4 | 004 | 4 |
| 5 |"blank" |"blank" |
| 6 |"blank" |"blank" |
| 7 | 005 | 7 |
| 8 |"blank" |"blank" |
| 9 |"blank" |"blank" |
| 10 | 006 | 10 |
| 11 | 007 | 11 |
| 12 |"blank" |"blank" |
| 13 |"blank" |"blank" |
| 14 |"blank" |"blank" |
| 15 |"blank" |"blank" |

这是到目前为止我的代码...请帮助我..我计划使用数组,但我不知道它现在是否适用于我的代码..

ViewGridObject.java

public class ViewGridObject 
{
public String stud_id, seat_no;

public ViewGridObject(String stud_id, String seat_no)
{
this.stud_id = stud_id;
this.seat_no = seat_no;
}
}

main.java

ArrayList<HashMap<String, String>> to_take = new ArrayList<HashMap<String, String>>();

try
{
JSONObject jArray = new JSONObject(result);

JSONArray stud_info = jArray.getJSONArray("stud_info");

ca_list = new ArrayList<ViewGridObject>();

for (int i = 0; i < stud_info.length(); i++)
{
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = stud_info.getJSONObject(i);

map.put("id", String.valueOf(i));
map.put("stud_id", e.getString("student_id"));
map.put("seat_no", e.getString("seat_no"));
to_take.add(map);

ca_list.add(new ViewGridObject(e.getString("student_id"), e.getString("seat_no")));


}

List<ViewGridObject> list2 = new ArrayList<ViewGridObject>();
int i1 = 0;
for (ViewGridObject o : ca_list) {
for (i1++; !o.seat_no.equals(i1 + ""); i1++) {
list2.add(new ViewGridObject(null, 0, i1 + ""));
}
list2.add(o);
}

最佳答案

List<ViewGridObject> list2 = new ArrayList<ViewGridObject>();

for(int i=0;i<50;i++) {
try {
list2.add(list1.get(i));
} catch(Exception rr) {
list2.add(new ViewGridObject("blank","blank");
}
}

关于java - 如何将ArrayList限制为15个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13673914/

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