List的 sublist() 和 clear()方法的使用

package com.yihaodian.mandy.bdb;

import java.util.ArrayList;
import java.util.List;

public class ListObjTest {
public static void main(String[] args) {
List<Product> list1 = new ArrayList<Product>();
for(int i=1;i<20;i++){
Product p = new Product(i, "p" + i);
System.out.print(p + "\t");
list1.add(p);
}
System.out.println();
// List<Product> list2 = list1.subList(0, 5);
//
// for(Product p : list2){
// p.setName("shit");
// }

int size = list1.size()/5;
int last = list1.size()%5;
System.out.println(size + "   " + last);
List<Product> tempList = null;
for(int i=0;i<size;i++){
tempList = list1.subList(i*5, (i+1)*5);
for(Product p:tempList){
p.setName("shit" + i);
System.out.print(p+"\t");
}
System.out.println();
}
tempList = list1.subList(list1.size() - last, list1.size());
for(Product p:tempList){
p.setName("shit" + size);
System.out.print(p+"\t");
}
System.out.println();
for(Product p : list1){
System.out.print(p+"\t");
}
}
}

输出结果:
Product [id=1, name=p1] Product [id=2, name=p2] Product [id=3, name=p3] Product [id=4, name=p4] Product [id=5, name=p5] Product [id=6, name=p6] Product [id=7, name=p7] Product [id=8, name=p8] Product [id=9, name=p9] Product [id=10, name=p10] Product [id=11, name=p11] Product [id=12, name=p12] Product [id=13, name=p13] Product [id=14, name=p14] Product [id=15, name=p15] Product [id=16, name=p16] Product [id=17, name=p17] Product [id=18, name=p18] Product [id=19, name=p19]
3   4
Product [id=1, name=shit0] Product [id=2, name=shit0] Product [id=3, name=shit0] Product [id=4, name=shit0] Product [id=5, name=shit0]
Product [id=6, name=shit1] Product [id=7, name=shit1] Product [id=8, name=shit1] Product [id=9, name=shit1] Product [id=10, name=shit1]
Product [id=11, name=shit2] Product [id=12, name=shit2] Product [id=13, name=shit2] Product [id=14, name=shit2] Product [id=15, name=shit2]
Product [id=16, name=shit3] Product [id=17, name=shit3] Product [id=18, name=shit3] Product [id=19, name=shit3]
Product [id=1, name=shit0] Product [id=2, name=shit0] Product [id=3, name=shit0] Product [id=4, name=shit0] Product [id=5, name=shit0] Product [id=6, name=shit1] Product [id=7, name=shit1] Product [id=8, name=shit1] Product [id=9, name=shit1] Product [id=10, name=shit1] Product [id=11, name=shit2] Product [id=12, name=shit2] Product [id=13, name=shit2] Product [id=14, name=shit2] Product [id=15, name=shit2] Product [id=16, name=shit3] Product [id=17, name=shit3] Product [id=18, name=shit3] Product [id=19, name=shit3]


对于集合list的sublist()方法:
修改tempList = list1.subList(i*5, (i+1)*5);
集合的对象,会对list1原集合的对象做修改,因为他们指向的是内存中的同一个地址,
但如果tempList = new ArrayList<Product>(list1.subList(i*5, (i+1)*5));
对tempList的修改对list1的对象的值没影响。


package com.yihaodian.mandy.bdb;

import java.util.ArrayList;
import java.util.List;

public class ListTest {
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args) {
List list1 = new ArrayList();
for(int i=0; i<20; i++){
list1.add(i);
}
List list2 = new ArrayList(list1.subList(0, 5));
list1.subList(0, 5).clear();
list2.add(11);

List list3 = new ArrayList(list1.subList(0, 5));
list1.subList(0, 5).clear();

// List list4 = list1.subList(0, 5);
// list4.add(22);
// list1.subList(0, 5).clear();

for (Object obj : list1) {
System.out.print(obj + "\t");
}
System.out.println();
for (Object obj : list2) {
System.out.print(obj + "\t");
}
System.out.println();
for (Object obj : list3) {
System.out.print(obj + "\t");
}
// System.out.println();
// for (Object obj : list4) {
// System.out.print(obj + "\t");
// }
}
}

输出结果:
10 11 12 13 14 15 16 17 18 19
0 1 2 3 4 11
5 6 7 8 9

list的clear()方法会把sublist截取的集合部分从list1中移除掉。

你可能感兴趣的:(clear)