![](/img/trans.png)
[英]How to add an ArrayList<Integer> to an ArrayList<ArrayList<Integer>> again and again with different elements in ArrayList<Integer>
[英]How can I add the last 3 elements of an Arraylist to a different Arraylist?
我正在制作圖靈機。 我有一個圖靈機的正則表達式文本文件,我必須將數據添加到數組列表中。 我成功地將所有數據添加到一個數組列表中。 但是,我想將最后 3 個元素添加到不同的數組列表中。 有沒有辦法,我可以只將 arraylist #1 的最后三個元素傳輸到 arraylist #2。 這是我在 arraylist #1 中的正則表達式數據。
(q0,1)->(q0,1,R)
(q0,0)->(q1,1,R)
(q1,1)->(q1,1,R)
(q1,_)->(q2,_,L)
(q2,1)->(qa,_,L)
q0
qa
qr
使用subList(from, to)
方法。
subList(myList.size()-3, myList().size());
例如:
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
List<Integer> lastThree = list.subList(list.size()-3, list.size());
System.out.println("All element ="+list);
System.out.println("Last three element ="+lastThree);
list.subList(list.size()-3, list.size()).clear();// will remove last three
System.out.println("After removing last three element="+list);
輸出:
All element =[1, 2, 3, 4]
Last three element =[2, 3, 4]
After removing last three element=[1]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.