Java 中ArrayList<Object> 如何去除其中重复的对象? 最好举个例子
1个回答
展开全部
import java.util.ArrayList;
import java.util.List;
public class Test{
public static void main(String[] args) {
List<Object> list = new ArrayList<Object>() ;
A a = new A("a");
A b = a ;
A c = new A("c");
A d = new A("d");
A e = d ;
A f = a ;
list.add(a);
list.add(b);
list.add(c);
list.add(d);
list.add(e);
list.add(f);
List<Object> newList = new ArrayList<Object>();
int size = list.size() ;
for(int i = 0 ; i < size ; i++){
if(! list.subList(i+1, size).contains(list.get(i))){
newList.add(list.get(i));
}
}
for(Object o : newList){
System.out.println(o);
}
}
}
class A{
private String name ;
A(String name){
this.name = name ;
}
@Override
public String toString(){
return name ;
}
}
import java.util.List;
public class Test{
public static void main(String[] args) {
List<Object> list = new ArrayList<Object>() ;
A a = new A("a");
A b = a ;
A c = new A("c");
A d = new A("d");
A e = d ;
A f = a ;
list.add(a);
list.add(b);
list.add(c);
list.add(d);
list.add(e);
list.add(f);
List<Object> newList = new ArrayList<Object>();
int size = list.size() ;
for(int i = 0 ; i < size ; i++){
if(! list.subList(i+1, size).contains(list.get(i))){
newList.add(list.get(i));
}
}
for(Object o : newList){
System.out.println(o);
}
}
}
class A{
private String name ;
A(String name){
this.name = name ;
}
@Override
public String toString(){
return name ;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询