List of java interfaces iterative process of adding elements

list Interface: There are subscripts, orderly access, allowing duplicate elements (the equals method), whether the comparison duplicate elements.

Common interface classes: ArrayList collection set Linkedlist

1      // ordered by an index value may be repeated 
2      List <String> ARR = new new the ArrayList <String> (); // this case polymorphic objects created and remains set 
. 3      arr.add ( "1" );
 . 4      / / additive element to a specified position after the original shift element 
. 5      arr.add (0, "2" );
 . 6      arr.add (. 1, "2" );
 . 7      // get all the elements of the specified index 
. 8      the System .out.println (arr.get (0 ));
 . 9      // delete the specified position of the element 
10      System.out.println ( "delete elements" arr.remove + (. 1 ));
 . 11      // delete the specified element returns a Boolean value 
12      System.out.println ( "deleted elements" + arr.remove ( "1"));
 13      // replace elements in the specified position 
14      arr.set (. 1, "Hello" );
 15      // . 3 Species iterate over the control loop for common strong for loop 
16      for ( int I = 0; I <ARR .size (); I ++ ) {
 . 17          System.out.println (arr.get (I));
 18 is      }

How to add an element in an iterative process: Do not add and delete elements of a collection of the best iteration

1  An alternative may be a newly created element Collection be added to individual elements, then the iteration of these elements:
 2  
. 3 Collection <String> Arrays.asList List = ( new new String [] { "the Hello", "World!" } );
 . 4 Collection <String> = additionalList new new the ArrayList <String> ();
 . 5  
. 6  for (S String: List) {
 . 7      // Found A need new new Element to the Add A to the iterate over,
 . 8      // SO to the Add Another IT Will that later Iterated BE List: 
. 9      additionalList.add (S);
 10  }
 . 11  
12 is  for (String S: additionalList) {
 13 is      // Iterate over the elements that needs to be iterated over:
14     System.out.println(s);
15 }

Guess you like

Origin www.cnblogs.com/mlf19920916/p/12109893.html