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
*///:~
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值