我有 2 个数组列表。我遍历list1并将与seat_no匹配的数据存储到list2的索引中。
list1 包含:
|seat_no | id |
===================
| 1 | 001 |
| 2 | 002 |
| 3 | 003 |
| 4 | 004 |
| 7 | 005 |
| 10 | 006 |
| 11 | 007 |
list2 包含:
|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);
}