雅虎一个笔试题,有关2个list比较

package com.test; import java.util.ArrayList; import java.util.List; /** * aiguoxin * 2010-10-26上午11:10:53 * * 比较2个list是否相等,即每项对应相等 * 如果是对象,则equals相等 * 如果是Float、Integer、Double,只要值相等即可 */ public class CompareList { /** * @param args */ public static void main(String[] args) { List<Object> la = new ArrayList<Object>(); List<Object> lb = new ArrayList<Object>(); la.add(new Integer(1)); la.add(new String("asd")); la.add(new Double(1.4)); lb.add(new Float(1.1f)); lb.add(new String("asd")); lb.add(new Float(2.6)); boolean b = compare(la,lb); System.out.println(b); } public static boolean compare(List<Object> la,List<Object> lb){ boolean flag = true; for(int i = 0; i < la.size(); i++){ Object a = la.get(i); Object b = lb.get(i); int ia = 1; int ib = -1; if(a instanceof Float)ia = ((Float) a).intValue(); if(a instanceof Double)ia = ((Double) a).intValue(); if(a instanceof Integer)ia = ((Integer) a).intValue(); if(b instanceof Float)ib = ((Float) b).intValue(); if(b instanceof Double)ib = ((Double) b).intValue(); if(b instanceof Integer)ib = ((Integer) b).intValue(); if(!a.equals(b) && ia != ib)flag = false; } return flag; } }

 

感觉很繁琐,望高手有更好办法~

你可能感兴趣的:(object,String,list,Integer,equals,float)