11、Holding--List

public class ListFeatures {
  public static void main(String[] args) {
	Random rand = new Random(47);
    List<Pet> pets = Pets.arrayList(7);
    print("1: " + pets);// 1: [Rat, Manx, Cymric, Mutt, Pug, Cymric, Pug]
    Hamster h = new Hamster();
    pets.add(h); // Automatically resizes
    print("2: " + pets); //2: [Rat, Manx, Cymric, Mutt, Pug, Cymric, Pug, Hamster]
    /*
     * contains() indexOf() remove()这些方法都要用到equals方法
     */
    print("3: " + pets.contains(h));//	      3: true
    pets.remove(h); // Remove by object 将对象的引用传递给remove方法
    Pet p = pets.get(2);
    print("4: " +  p + " " + pets.indexOf(p));// 4: Cymric 2 ##indexOf发现对象在list中的索引位置
    Pet cymric = new Cymric();
    print("5: " + pets.indexOf(cymric));//	      5: -1
    print("6: " + pets.remove(cymric));//	      6: false
    // Must be the exact object:
    print("7: " + pets.remove(p));//	      7: true
    print("8: " + pets);//8: [Rat, Manx, Mutt, Pug, Cymric, Pug]
    pets.add(3, new Mouse()); // Insert at an index
    print("9: " + pets);//	      9: [Rat, Manx, Mutt, Mouse, Pug, Cymric, Pug]
    
    /*
     * subList(int,int)方法从较大的List中很容易的创建出一个片段  产生的列表幕后是原始的列表
     * 索引对subList方法返回的列表的修改都会反映到初始列表中。
     * containsAll方法对参数的集合中元素的顺序并不关心 ,因为它是对作为参数的集合进行遍历 查看是否存在各个元素
     */
    List<Pet> sub = pets.subList(1, 4);
    print("subList: " + sub);//	      subList: [Manx, Mutt, Mouse]
    print("10: " + pets.containsAll(sub));//	      10: true
    Collections.sort(sub); // In-place sort
    print("sorted subList: " + sub);//sorted subList: [Manx, Mouse, Mutt]
    // Order is not important in containsAll():
    print("11: " + pets.containsAll(sub));//	  11: true
    Collections.shuffle(sub, rand); // Mix it up
    print("shuffled subList: " + sub);//	  shuffled subList: [Mouse, Manx, Mutt]
    
    print("12: " + pets.containsAll(sub));//	  12: true
    
    List<Pet> copy = new ArrayList<Pet>(pets);
    sub = Arrays.asList(pets.get(1), pets.get(4));
    print("sub: " + sub);//	  sub: [Mouse, Pug]
    
    copy.retainAll(sub);//依赖于equals()方法 返回的值是(copy是否是sub的超集)既是否从copy中移除了在sub中没有的数据
    print("13: " + copy);//	  13: [Mouse, Pug]
    copy = new ArrayList<Pet>(pets); // Get a fresh copy
    copy.remove(2); // Remove by index
    print("14: " + copy);//	  14: [Rat, Mouse, Mutt, Pug, Cymric, Pug]
    copy.removeAll(sub); // copy中移除参数中包括的所有的元素
    print("15: " + copy);//	  15: [Rat, Mutt, Cymric, Pug]
    copy.set(1, new Mouse()); // Replace an element
    print("16: " + copy);//	  16: [Rat, Mouse, Cymric, Pug]
    copy.addAll(2, sub); // 在指定的索引处插入参数中的所有
    print("17: " + copy);//	  17: [Rat, Mouse, Mouse, Pug, Cymric, Pug]
    print("18: " + pets.isEmpty());//	  18: false
    pets.clear(); // Remove all elements
    print("19: " + pets);//	  19: []
    print("20: " + pets.isEmpty());//	  20: true
    pets.addAll(Pets.arrayList(4));
    print("21: " + pets);//	  21: [Manx, Cymric, Rat, EgyptianMau]
    Object[] o = pets.toArray();//创建合适大小的数组
    print("22: " + o[3]);//	  22: EgyptianMau
    Pet[] pa = pets.toArray(new Pet[0]);//目标类型的数据
    print("23: " + pa[3].id());//	  23: 14  
  } 
} 

 

你可能感兴趣的:(UP)