list去掉重复对象

实在对不起,网上找到的例子出了些差错,小弟杨小一指正一下。
具体的写在了注释里面



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

import com.wangjialin.server.domain.News;


public class DistinctList {

public static   void main (String args[]){
List newes = new ArrayList();
newes.add(new News(10, "wangjialin", 20));
newes.add(new News(10, "wangjialin", 20));
newes.add(new News(10, "wangjialin", 20));
newes.add(new News(10, "wangjialin", 20));
newes.add(new News(45, "jialingege", 10));
newes.add(new News(89, "android", 50));
News news = new News();
for(int i=0;i
news = newes.get(i);
for(int j=i+1;j
if(news.getTitle().equals(newes.get(j).getTitle())){
                newes.remove(i);
                if(i!=0){   //这个判断一定要有,否则如果重复数据>2条,数组越界异常 
                  i--;
                }
                if(j!=0){   //这个判断一定要有,否则如果重复数据>2条,数组越界异常 
                  j--;
                }
                     
                }
}
}
System.out.println(newes.size());
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值