List - Insertion order
List interface keeps the order of insertion of elements i.e. the order in which the elements added to the list will remain in same order after any operation. Every addition takes the index of last inserted element plus one (last index + 1).
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | package net.raj.test; import java.util.ArrayList; import java.util.List; public class ArrayListImpl { public static void main(String[] args) { // Declare an ArrayList List<String> list = new ArrayList<>(); list.add("one"); list.add("two"); list.add("three"); list.add("four"); System.out.println("Sample ArrayList 1:: " + list.toString()); // Reuse the same object // list keeps the order list = new ArrayList<>(); list.add("three"); list.add("four"); list.add("one"); list.add("two"); System.out.println("Sample ArrayList 2:: " + list.toString()); } } |
Output:
Sample ArrayList 1:: [one, two, three, four]
Sample ArrayList 1:: [one, two, three, four, five]
Sample ArrayList 1:: [one, two, three, four, five]
Look at my other posts:
Some other interesting blogs:
Comments
Post a Comment