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中移除掉。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值