Last active
April 2, 2017 02:15
-
-
Save goldcode88/35dd8c139ee7779f5da418fdda3e82fb to your computer and use it in GitHub Desktop.
ArrayList在内循环中删除
1.The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException.
参考:http://stackoverflow.com/quest…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| public class ArrayListExamples { | |
| public static void main(String args[]) { | |
| // Creating an empty array list | |
| ArrayList<String> list = new ArrayList<String>(); | |
| // Adding items to arrayList | |
| list.add("Item1"); | |
| list.add("Item2"); | |
| list.add(2, "Item3"); // it will add Item3 to the third position of | |
| // array list | |
| list.add("Item4"); | |
| // Display the contents of the array list | |
| System.out.println("The arraylist contains the following elements: " | |
| + list); | |
| // Checking index of an item | |
| int pos = list.indexOf("Item2"); | |
| System.out.println("The index of Item2 is: " + pos); | |
| // Checking if array list is empty | |
| boolean check = list.isEmpty(); | |
| System.out.println("Checking if the arraylist is empty: " + check); | |
| // Getting the size of the list | |
| int size = list.size(); | |
| System.out.println("The size of the list is: " + size); | |
| // Checking if an element is included to the list | |
| boolean element = list.contains("Item5"); | |
| System.out.println("Checking if the arraylist contains the object Item5: " | |
| + element); | |
| // Getting the element in a specific position | |
| String item = list.get(0); | |
| System.out.println("The item is the index 0 is: " + item); | |
| // Retrieve elements from the arraylist | |
| // 1st way: loop using index and size list | |
| System.out | |
| .println("Retrieving items with loop using index and size list"); | |
| for (int i = 0; i < list.size(); i++) { | |
| System.out.println("Index: " + i + " - Item: " + list.get(i)); | |
| } | |
| // 2nd way:using foreach loop | |
| System.out.println("Retrieving items using foreach loop"); | |
| for (String str : list) { | |
| System.out.println("Item is: " + str); | |
| } | |
| // 3rd way:using iterator | |
| // hasNext(): returns true if there are more elements | |
| // next(): returns the next element | |
| System.out.println("Retrieving items using iterator"); | |
| for (Iterator<String> it = list.iterator(); it.hasNext();) { | |
| System.out.println("Item is: " + it.next()); | |
| } | |
| // Replacing an element | |
| list.set(1, "NewItem"); | |
| System.out.println("The arraylist after the replacement is: " + list); | |
| // Removing items | |
| // removing the item in index 0 | |
| list.remove(0); | |
| // removing the first occurrence of item "Item3" | |
| list.remove("Item3"); | |
| System.out.println("The final contents of the arraylist are: " + list); | |
| // Converting ArrayList to Array | |
| String[] simpleArray = list.toArray(new String[list.size()]); | |
| System.out.println("The array created after the conversion of our arraylist is: " | |
| + Arrays.toString(simpleArray)); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| class Test { | |
| public static ArrayList<String> removeBad(ArrayList<String> phrases){ | |
| for(String aPhrase:phrases) | |
| { | |
| if(aPhrase.indexOf("bad")>=0){ | |
| //phrases.remove(aPhrase); | |
| System.out.println(aPhrase); | |
| } | |
| } | |
| return phrases; | |
| } | |
| public static void main(String[] args) { | |
| ArrayList<String> phrases = new ArrayList<String>(); | |
| phrases.add("good day"); | |
| phrases.add("bad hair"); | |
| phrases.add("cookie are good"); | |
| phrases.add("bad applies"); | |
| removeBad(phrases); | |
| System.out.print(phrases); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| class Test { | |
| public static ArrayList<String> removeBad(ArrayList<String> phrases){ | |
| int size = phrases.size(); | |
| System.out.println(size); | |
| for(int i = size - 1; i>=0; i--) | |
| { | |
| if(phrases.get(i).indexOf("bad")>=0){ | |
| System.out.println(phrases.get(i)); | |
| phrases.remove(i); | |
| } | |
| } | |
| return phrases; | |
| } | |
| public static void main(String[] args) { | |
| ArrayList<String> phrases = new ArrayList<String>(); | |
| phrases.add("good day"); | |
| phrases.add("bad hair"); | |
| phrases.add("cookie are good"); | |
| phrases.add("bad applies"); | |
| removeBad(phrases); | |
| System.out.print(phrases); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment