Title:
ArrayList list = new ArrayList(20);Medium list Expand several times () A 0 B 1 C 2 D 3
Answer: A
We all know that the default ArrayList is 10, so if you want to add 20 elements to the list, you must expand it once (1.5 times of the original), but the display here indicates how much space is needed, so you need to allocate so much space at one time, that is, you don't need to expand it.
The difference between a set and an array:
1. The length of the set is variable. Through the array copy method at the bottom, it can achieve continuous growth. Only reference data types can be stored, but the data types can be stored through the wrapper class.
2. The array length is fixed, which can store basic data types or reference data types.
The Iterable interface is the parent interface of the Collection.
Example 1:
//The origin of iterator ArrayList<Person> list=new ArrayList<>(); list.add(new Person("Black",35)); list.add(new Person("Whites",25)); list.add(new Person("the yellow race",28)); /* Object[] array = list.toArray(); for (int i = 0; i < array.length; i++) { Person p=(Person) array[i]; System.out.println(p.getName()+"..."+p.getAge()); //The inconvenience of this way leads to the iterator }*/ /*Iterator it=list.iterator(); while(it.hasNext()){ Person p=(Person)it.next(); System.out.println(p.getName()+"..."+p.getAge()); //It's also inconvenient to have strong conversion, which leads to the generic mechanism }*/ Iterator<Person>it=list.iterator(); while(it.hasNext()){ Person p=it.next(); System.out.println(p.getName()+"..."+p.getAge()); }
Example 2:
public class Test02 { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("a"); list.add("b"); list.add("0"); list.add("d"); list.add("9"); list.add("3"); deleteNum(list); Iterator<String> it = list.iterator(); while (it.hasNext()) { System.out.println(it.next()); } } private static void deleteNum(List<String> list) { /*for (int i = 0; i < list.size(); i++) { String s = (String) list.get(i); char[] arr = s.toCharArray(); for (int j = 0; j < arr.length; j++) { if (arr[j] >= '0' && arr[j] <= '9') { list.remove(i); break; // Why use break here? // Problems encountered when deleting collection elements // The set length will be shorter, but the pointer will still be in the original position // So it will cross some elements and not traverse them, so i -- move the pointer back to one cell // This is the drawback of using for loop to delete a collection } }*/ Iterator<String> it=list.iterator(); while(it.hasNext()){ String ss=(String)it.next(); for(Integer x=0;x<10;x++){ String s=x.toString(); if(ss.contains(s)){ it.remove();
break; } } } } }
The differences of different traversal methods are as follows:
Normal for loop:
It can be deleted, but the index should --, because once deleted, the collection elements will move forward to fill the vacancy.
Iterator:
It can be deleted, but the iterator's own remove method must be used, or concurrent modification exceptions will occur.
Enhanced for loop:
Cannot be deleted, otherwise concurrent modification exception will be reported, because its underlying dependency is iterator.