繁体   English   中英

如何将ArrayList限制为15?

[英]How to limit ArrayList to 15?

我有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);
            }

仅当在添加之前明确检查时才可以限制它,
或通过创建一个新的MyArrayList并在其中集成了ArrayLIst,但是在size(> 15)中不会添加在add()中; 在这种情况下,您可以返回false。

我建议使用“广告前检查”方法:

int limit = 15;

if (list.size() < limit) {
  list.add(value);
}
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");
    }
}

Arraylist通过实现是无限的。 几个选择;

  1. 使用数组并使用Arrays.asList执行arraylist函数
  2. 实现AbstractList并实现类似的add方法

    如果(size()> getBoundedSize())

让我知道您是否需要更清楚的例子

使用Java 8,您可以将ArrayList转换为Stream并对其进行限制:

List<Object> largerArray = Arrays.asList(members);
largerArray.stream()
    .limit(3)
    .forEach(member -> {
        // ...
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM