日期:2014-05-18 浏览次数:21184 次
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ListHava {
public static void main(String[] args) {
List<Object> list = new ArrayList<Object>();
A a1 = new A();
A a2 = new A();
A a3 = new A();
list.add(a1);
list.add(a2);
list.add(a3);
B b1 = new B();
list.add(b1);
System.out.println(listHaveEqual(list, a1, b1));
}
public static boolean listHaveEqual(List<Object> l, Object p, Object q) {
if((l.contains(p)&&l.contains(q))==false){
return false;
}
Class pclass = p.getClass();
Class qclass = q.getClass();
Field[] f1 = pclass.getDeclaredFields();
Field[] f2 = qclass.getDeclaredFields();
for(int i=0;i<f1.length;i++){
f1[i].setAccessible(true);
}
for(int i=0;i<f2.length;i++){
f2[i].setAccessible(true);
}
for (Field f : f1) {
System.out.println(f);
String n = f.getName();
Class t = f.getType();
Boolean equal = false;
for (Field ff : f2) {