简体   繁体   English

如何将ArrayList限制为15?

[英]How to limit ArrayList to 15?

I have 2 array lists. 我有2个数组列表。 i iterate through list1 and store the data that matches the seat_no to the index of list2. 我遍历list1并将与seat_no匹配的数据存储到list2的索引。

list1 contains: list1包含:

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

list2 contains: 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      |

what list2 should contain: where list2 must have a limit of 15 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"  |

Here's my code so far... pls help me.. i plan to use array but i don't know if its applicable to my codes now.. 到目前为止,这是我的代码...请帮助我..我计划使用数组,但现在不知道它是否适用于我的代码。

ViewGridObject.java 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 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);
            }

You can limit that only when you explicitly check that before you add, 仅当在添加之前明确检查时才可以限制它,
or by creating a new MyArrayList where you have an ArrayLIst integrated, but in add() you do not add when size is > 15; 或通过创建一个新的MyArrayList并在其中集成了ArrayLIst,但是在size(> 15)中不会添加在add()中; you could return false in that case. 在这种情况下,您可以返回false。

I suggest use the "check that before you ad"d approach: 我建议使用“广告前检查”方法:

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 are boundless by implementation. Arraylist通过实现是无限的。 Couple of options; 几个选择;

  1. Use an array and use Arrays.asList to perform arraylist functions 使用数组并使用Arrays.asList执行arraylist函数
  2. Implement AbstractList and implement the add method like 实现AbstractList并实现类似的add方法

    if (size()> getBoundedSize()) 如果(size()> getBoundedSize())

Let me know if you need a clearer example 让我知道您是否需要更清楚的例子

Using Java 8, you can convert the ArrayList to a Stream and limit it: 使用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