java list
list承诺可以将元素维护在特定的序列中,List接口在Collection的基础上添加了大量的方法,使得可以在List的中间插入和移动元素
有两种List
基本的ArrayList,它长于随机访问元素,但时在List的中间插入和移除元素较慢
LinkedList,它通过代价较低的在List中间进行的插入和删除操作,提供了优化的顺序访问.LinkedList在随机访问方面相对较慢,但它的特性较ArrayList更大
package java.util; import java.util.function.UnaryOperator; public interface List<E> extends Collection<E> { int size(); boolean isEmpty();//判断List是否为空 boolean contains(Object o); //确定某个对象是否在列表中
//对于基本数据类型和 String类型 如果字面值一样将返回true
//对于对象和包装类如果是新new 的一个对象,即使字面值一样也返回false
Iterator<E> iterator(); //迭代器
Object[] toArray(); //创建一个合适的数组
<T> T[] toArray(T[] a);//将 a转化为一个数组
boolean add(E e); //
boolean remove(Object o);//将对象引用传递给remove()方法移除一个对象
boolean containsAll(Collection<?> c); //判断一个List列表是否被包含在里面
boolean addAll(Collection<? extends E> c);//插入一个List列表 boolean addAll(int index, Collection<? extends E> c);//插入一个对象 boolean removeAll(Collection<?> c); //基于equals(),移除List中的所有c中所包含的元素 boolean retainAll(Collection<?> c); //retainAll()方法是一种"交集"操作,retainAll()行为依赖于equals() default void replaceAll(UnaryOperator<E> operator) { Objects.requireNonNull(operator); final ListIterator<E> li = this.listIterator(); while (li.hasNext()) { li.set(operator.apply(li.next())); } } @SuppressWarnings({"unchecked", "rawtypes"}) default void sort(Comparator<? super E> c) { //排序List //Collection类中shuffle()方法为随机打乱 Object[] a = this.toArray(); Arrays.sort(a, (Comparator) c); ListIterator<E> i = this.listIterator(); for (Object e : a) { i.next(); i.set((E) e); } } void clear(); //清空List boolean equals(Object o); //contains(),removeAll()和indexOf都会调用equals(),equals()属于Object的一部分 //List的行为会根据equals()的行为有所变化 int hashCode(); E get(int index); //返回Index所在的element E set(int index, E element);//和Set存在潜在冲突,该方法将index号元素修改为element void add(int index, E element);//添加element到index位置 E remove(int index); //移除Index所在的element int indexOf(Object o); //查找对象在List中所处的位置的索引编号(查找的是引用) //即使两个对象看起来一样也不相等,但是如果是基本数据类型则会相等 int lastIndexOf(Object o); ListIterator<E> listIterator(); ListIterator<E> listIterator(int index); List<E> subList(int fromIndex, int toIndex);//从一个大的List中创建一个片段(引用),不包括toIndex的值 //修改片段将大导致的List列表被修改 @Override default Spliterator<E> spliterator() { return Spliterators.spliterator(this, Spliterator.ORDERED); } }
下面是一个Integer应用实例
package object; //: holding/E05_IntegerListFeatures.java /****************** Exercise 5 ****************** * Use Integers instead of Pets to modify * ListFeatures.java (remember autoboxing). * Explain any difference in results. ***********************************************/ package holding; import java.util.*; import static net.mindview.util.Print.*; import java.util.*; import static net.util.Print.*; public class E05_IntegerListFeatures { static Random rand = new Random(47); public static void main(String[] args) { List<Integer> ints = new ArrayList<Integer>( Arrays.asList(1, 2, 3, 4, 5, 6, 7)); print("1: " + ints); ints.add(8); print("2: " + ints); print("3: " + ints.contains(8)); ints.remove(Integer.valueOf(8)); Integer i = ints.get(2); print("4: " + i + " " + ints.indexOf(i)); Integer j = Integer.valueOf(1); print("5: " + ints.indexOf(j)); print("6: " + ints.remove(j)); print("7: " + ints.remove(i)); print("8: " + ints); ints.add(3, 0); print("9: " + ints); List<Integer> sub = ints.subList(1, 4); print("subList: " + sub); print("10: " + ints.containsAll(sub)); Collections.sort(sub); print("sorted subList: " + sub); print("11: " + ints.containsAll(sub)); Collections.shuffle(sub, rand); print("shuffled subList: " + sub); print("12: " + ints.containsAll(sub)); List<Integer> copy = new ArrayList<Integer>(ints); sub = Arrays.asList(ints.get(1), ints.get(4)); print("sub: " + sub); copy.retainAll(sub); print("13: " + copy); copy = new ArrayList<Integer>(ints); // Thinking in Java, 4th Edition Annotated Solution Guide 184 copy.remove(2); print("14: " + copy); copy.removeAll(sub); print("15: " + copy); copy.set(1, 9); print("16: " + copy); copy.addAll(2, sub); print("17: " + copy); print("18: " + ints.isEmpty()); ints.clear(); print("19: " + ints); print("20: " + ints.isEmpty()); ints.addAll(Arrays.asList(1, 2, 3, 4)); print("21: " + ints); Object[] o = ints.toArray(); print("22: " + o[3]); Integer[] ia = ints.toArray(new Integer[0]); print("22: " + ia[3]); } } /* * Output: 1: [1, 2, 3, 4, 5, 6, 7] 2: [1, 2, 3, 4, 5, 6, 7, 8] 3: true 4: 3 * 2 5: 0 6: true 7: true 8: [2, 4, 5, 6, 7] 9: [2, 4, 5, 0, 6, 7] subList: * [4, 5, 0] 10: true sorted subList: [0, 4, 5] 11: true shuffled subList: * [4, 0, 5] 12: true sub: [4, 6] 13: [4, 6] 14: [2, 4, 5, 6, 7] 15: [2, 5, * 7] 16: [2, 9, 7] 17: [2, 9, 4, 6, 7] 18: false 19: [] 20: true 21: [1, 2, * 3, 4] 22: 4 22: 4 Holding Your Objects 185 */// :~
String
package ch06; //: holding/E05_StringListFeatures.java /****************** Exercise 5 ****************** * Use Strings instead of Pets to modify * ListFeatures.java (remember autoboxing). * Explain any difference in results. ***********************************************/ package holding; import java.util.*; import static net.mindview.util.Print.*; import java.util.*; import static net.util.Print.*; public class E05_StringListFeatures { static Random rand = new Random(47); public static void main(String[] args) { List<String> ints = new ArrayList<String>( Arrays.asList("dog", "pig", "duck", "fly", "event", "boast", "convince")); print("1: " + ints); ints.add("yes"); print("2: " + ints); print("3: " + ints.contains("pig")); ints.remove(String.valueOf("pig")); String i = ints.get(2); print("4: " + i + " " + ints.indexOf(i)); String j = String.valueOf("event"); print("5: " + ints.indexOf(j)); print("6: " + ints.remove(j)); print("7: " + ints.remove(i)); print("8: " + ints); ints.add(3, "no"); print("9: " + ints); List<String> sub = ints.subList(1, 4); print("subList: " + sub); print("10: " + ints.containsAll(sub)); Collections.sort(sub); print("sorted subList: " + sub); print("11: " + ints.containsAll(sub)); Collections.shuffle(sub, rand); print("shuffled subList: " + sub); print("12: " + ints.containsAll(sub)); List<String> copy = new ArrayList<String>(ints); sub = Arrays.asList(ints.get(1), ints.get(4)); print("sub: " + sub); copy.retainAll(sub); print("13: " + copy); copy = new ArrayList<String>(ints); // Thinking in Java, 4th Edition Annotated Solution Guide 184 copy.remove(2); print("14: " + copy); copy.removeAll(sub); print("15: " + copy); copy.set(1, "connect"); print("16: " + copy); copy.addAll(2, sub); print("17: " + copy); print("18: " + ints.isEmpty()); ints.clear(); print("19: " + ints); print("20: " + ints.isEmpty()); ints.addAll(Arrays.asList("register", "vistor", "fork", "danger")); print("21: " + ints); Object[] o = ints.toArray(); print("22: " + o[3]); String[] ia = ints.toArray(new String[0]); print("22: " + ia[3]); } } /* * Output: 1: [1, 2, 3, 4, 5, 6, 7] 2: [1, 2, 3, 4, 5, 6, 7, 8] 3: true 4: 3 * 2 5: 0 6: true 7: true 8: [2, 4, 5, 6, 7] 9: [2, 4, 5, 0, 6, 7] subList: * [4, 5, 0] 10: true sorted subList: [0, 4, 5] 11: true shuffled subList: * [4, 0, 5] 12: true sub: [4, 6] 13: [4, 6] 14: [2, 4, 5, 6, 7] 15: [2, 5, * 7] 16: [2, 9, 7] 17: [2, 9, 4, 6, 7] 18: false 19: [] 20: true 21: [1, 2, * 3, 4] 22: 4 22: 4 Holding Your Objects 185 */// :~