`
Anddy
  • 浏览: 191880 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

List杂记

阅读更多
package holding;
//: holding/ListFeatures.java

import typeinfo.pets.*;
import java.util.*;
import static net.mindview.util.Print.*;

public class ListFeatures {
  public static void main(String[] args) {
    Random rand = new Random(47);
    // 下面arrayList()是对象pets中的 静态方法。。
    List<Pet> pets = Pets.arrayList(7);
    print("1: " + pets);
    Hamster h = new Hamster();
    pets.add(h); // Automatically resizes
    print("2: " + pets);
    print("3: " + pets.contains(h));
    pets.remove(h); // Remove by object
    Pet p = pets.get(2);
    print("4: " +  p + " " + pets.indexOf(p));
    
  //当确定一元素是否属于某个list发现某个元素的索引,以及从某个list中删除一个元素,containalls()	时,
  //都会用到equals()方法(这个equals 方法并没有重写,而是根类Object 类的一部分)
    
    Pet cymric = new Cymric();
    print("5: " + pets.indexOf(cymric));
    print("6: " + pets.remove(cymric));
    
    // Must be the exact object:
    print("7: " + pets.remove(p));
    print("8: " + pets);
    pets.add(3, new Mouse()); // Insert at an index
    print("9: " + pets);
    
   //仅仅重新建立一个sublist,sublist里保存的仅仅是pets集合中几个位置,,可以肯定绝对没有
    //保留对象的引用。为什么这样认为,在下面当你Collections.sort(pets,rand);然后print(sub),你会发现打印出来的
    //的对象不再是原来的那三个对象。
    
    
    //所创建的subList 的元素的还是公用原来List中的对象,
   //于是就有了两种修改LIst对象的办法。(或者说是两种路径)。 
    List<Pet> sub = pets.subList(1, 4);
    print("subList: " + sub);
    print("10: " + pets.containsAll(sub));
    Collections.sort(sub); // In-place sort
    print("sorted subList: " + sub);
    // Order is not important in containsAll():
    
    //
    print("11: " + pets.containsAll(sub));
    Collections.shuffle(sub, rand); // Mix it up
    print("shuffled subList: " + sub);
    print("12: " + pets.containsAll(sub));
    
    /*
     * Collections.shuffle(pets,rand);
     * print(sub);
     * 上面说明了方法subList(int index1,int index2)创建的子列表和母列表的关系。
     */
    
    
    /*  
     * pets.remove(2);
     *  print(sub);
     */
    //试着通过pets修改与sub共有 的对象----(删除了第三个对象),然后我通过print(sub)去查看第三个对象是否删除,
    //结果不尽人意,throws 一个runtimeException的异常:ConcurrentModificationException
    /*
     * ConcurrentModificationException的解释如下:
     * 当方法检测到对象的并发修改,但不允许这种修改时,抛出此异常。
     * 
     */
   
    //但是下面的处理为什么又不会出现异常?
    /* 
     * sub.remove(2);
     * print(pets);
     */
    List<Pet> copy = new ArrayList<Pet>(pets);
    sub = Arrays.asList(pets.get(1), pets.get(4));
    print("sub: " + sub);
    copy.retainAll(sub);
    print("13: " + copy);
    copy = new ArrayList<Pet>(pets); // Get a fresh copy
    copy.remove(2); // Remove by index
    print("14: " + copy);
    copy.removeAll(sub); // Only removes exact objects
    print("15: " + copy);
    copy.set(1, new Mouse()); // Replace an element
    print("16: " + copy);
    copy.addAll(2, sub); // Insert a list in the middle
    print("17: " + copy);
    print("18: " + pets.isEmpty());
    pets.clear(); // Remove all elements
    print("19: " + pets);
    print("20: " + pets.isEmpty());
    pets.addAll(Pets.arrayList(4));
    print("21: " + pets);
    Object[] o = pets.toArray();
    print("22: " + o[3]);
    //重载的toArray()的参数是与转换后的的数组类型相同的数组,该参数数组的大小任意,如果太小,存放不下LIst中的所有元素,
    //toArray()将创建一个具有适合大小的数组。正如下面所示:new pet[0]创建长度为0的数组。
    Pet[] pa = pets.toArray(new Pet[0]);
    print("23: " + pa[3].id());
  }
}
/* Output:
1: [Rat, Manx, Cymric, Mutt, Pug, Cymric, Pug]
2: [Rat, Manx, Cymric, Mutt, Pug, Cymric, Pug, Hamster]
3: true
4: Cymric 2
5: -1
6: false
7: true
8: [Rat, Manx, Mutt, Pug, Cymric, Pug]
9: [Rat, Manx, Mutt, Mouse, Pug, Cymric, Pug]
subList: [Manx, Mutt, Mouse]
10: true
sorted subList: [Manx, Mouse, Mutt]
11: true
shuffled subList: [Mouse, Manx, Mutt]
12: true
sub: [Mouse, Pug]
13: [Mouse, Pug]
14: [Rat, Mouse, Mutt, Pug, Cymric, Pug]
15: [Rat, Mutt, Cymric, Pug]
16: [Rat, Mouse, Cymric, Pug]
17: [Rat, Mouse, Mouse, Pug, Cymric, Pug]
18: false
19: []
20: true
21: [Manx, Cymric, Rat, EgyptianMau]
22: EgyptianMau
23: 14
*///:~

分享到:
评论
3 楼 Anddy 2008-12-14  
helyho 写道

不知道你发的代码啥意图.


呵呵,对学习的总结。。把自己对代码的理解写出来,
2 楼 helyho 2008-12-07  
不知道你发的代码啥意图.
1 楼 Anddy 2008-12-05  
spyker 写道

没看明白要做什么

什么地方没看懂,,,看我能否帮你。。

相关推荐

Global site tag (gtag.js) - Google Analytics