当添加元素时:The method add(String) in the type Collection<String> is not applicable for the arguments (int) (3)并发修改异常:ConcurrentModificationException;对集合进行增删操作; 迭代器不知道集合中的变化,容易发生调用的不确定性,[ListIterator了解即可] publicclassListIteratorDemo {publicstaticvoidmain(St...
list.add("edf"); list.add("ghi");for(Iterator<String> it=list.iterator();it.hasNext();) { System.out.println(it.next()); } } 执行结果: 回到顶部 二、ArrayList的Iterator实现 privateclass Itrimplements Iterator<E> {int cursor;//index of next element to returnint lastRet = -1;//inde...
ListIterator<String> listIt = list.listIterator(); while(listIt.hasNext()){ String s = listIt.next(); if("abc3".equals(s)){ listIt.add("itcast"); } System.out.println(s); } System.out.println(list); } 集合类(包括List)现在都有一个forEach方法,对元素进行迭代(遍历),所以我们不需...
public void add( int index, E element):将指定的元素,添加到该集合中的指定位置上。 // 在c和d之间添加一个itheima list.add(3,"itheima"); // [a,b,c,itheima,d,a] 1. 2. public E remove( int index ):移除列表中指定位置的元素,,返回的是被移除的元素。 // 移除元素 String removeE = l...
2.List接口在<tt>Collection</tt>接口指定的契约之外,还对<tt>iterator</tt>、<tt>add</tt>、<tt>remove</tt>、<tt>equals</tt>和<tt>hashCode</tt>方法的契约提供了额外的规定。在这里还包括其他继承方法的声明,以方便使用。 3.List接口提供了四种方法来对列表元素进行位置(索引)访问。
add("aaa"); list.add("bbb"); list.add("ccc"); //1.迭代器 Iterator<String> it = list.iterator(); while (it.hasNext()) { String str = it.next(); System.out.println(str); } //2.增强for for (String s : list) { System.out.println(s); } //3.Lambda表达式 //匿名内部类...
numbers.add(1); numbers.add(3); numbers.add(2); System.out.println("ArrayList: "+ numbers);// Creating an instance of ListIteratorListIterator<Integer> iterate = numbers.listIterator(); iterate.next(); iterate.next();// Using the previous() methodintnumber1 = iterate.previous(); ...
public boolean add(E e) public boolean add(int index, E e) Method parameter –The element ‘e’ to be appended to the end of this list. If the optional fromIndex parameter is supplied, the element is added to this index. All the subsequent elements are moved one position to the right...
Returns true if this list iterator has more elements when traversing the list in the reverse direction. Enext() Returns the next element in the list and advances the cursor position. intnextIndex() Returns the index of the element that would be returned by a subsequent call to next(). Epr...
ArrayList是List接口的实现类。是List存储特征的具体实现。底层是用数组实现的存储。特点:查询效率高,增删效率低,线程不安全。【5】Vector实现类Vector底层是用数组实现的,相关的方法都加了同步检查,因此“线程安全,效率低”。比如,indexOf方法就增加了synchronized同步标记。Vector的使用与ArrayList是相同的,因为他们都...