java - 如何迭代arraylist并存储到另一个arraylist?

标签 java arraylist

我有一个对象数组列表(list1),它包含 Seat_no、图像及其 ID。我如何迭代 list1 并将每个数据存储到另一个列表。前提是座位号。与固定大小为 50 的另一个列表 (list2) 的索引匹配。我应该创建另一个对象类吗?请帮忙...

示例:

列表1包含

|seat_no | id     | image            |
======================================
| 1      | 001    | R.drawable.chair |
| 2      | 002    | R.drawable.chair |
| 3      | 003    | R.drawable.chair |
| 4      | 004    | R.drawable.chair |
| 7      | 005    | R.drawable.chair |
| 10     | 006    | R.drawable.chair |

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      |

这是我的代码

main.java

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

        try 
        {
            JSONObject jsonArray = new JSONObject(result);

            JSONArray info = jsonArray.getJSONArray("info");

            list1 = new ArrayList<ViewGridObject>();

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

                map.put("id", String.valueOf(i));
                map.put("sid", e.getString("id"));
                map.put("flag", Integer.toString(seat[0]));
                map.put("seat_no", e.getString("seat_no"));
                take.add(map);

                list1.add(new ViewGridObject(e.getString("id"), R.drawable.chair, e.getString("seat_no")));


            }

ViewGridObject.java

public class ViewGridObject 
{
public String stud_id, seat_no;
int chair;

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

}
}

最佳答案

没有。 Java ArrayList 支持 null 值并保持插入顺序。您可以在有可用索引的对象的地方插入 null。

An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.

Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list. (This class is roughly equivalent to Vector, except that it is unsynchronized.)

例如:- 你有这个对象 - ViewGridObject1,ViewGridObject2, ViewGridObject3, ViewGridObject4,ViewGridObject7;

list1.add(ViewGridObject1);
list1.add(ViewGridObject2);
list1.add(ViewGridObject3);
list1.add(ViewGridObject4);
list1.add(null);
list1.add(null);
list1.add(ViewGridObject7);

所以你的列表将包含 - obj1, obj2, obj3, obj4, null, null, obj7,...

关于java - 如何迭代arraylist并存储到另一个arraylist?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13658998/

相关文章:

java - 将 JSON 文件中的所有元素收集到单个列表中

java - 调试 Java 代理

c# - 如何恢复 C# 服务器和 Java 客户端之间损坏的下载

java - 从 ArrayList 检索数据时出现逻辑错误

java - 如何找到与同一对象关联的最大对象数

c# - 将arraylist写入文本文件C#

Java Elasticsearch - 具有存在条件的 Boolquery

java - Concat Publishers 与 Reracor 但处理元素不同

java - 类型不匹配 : cannot convert from boolean to int

java - 如何从java swing中的多个文本框中获取值