雅虎2個面試

  1. package com.test;  
  2. import java.util.ArrayList;  
  3. import java.util.List;  
  4.  /** 
  5.  * aiguoxin 
  6.  * 2010-10-26上午11:10:53 
  7.  *  
  8.  * 比較2個list是否相等,即每項對應相等 
  9.  * 如果是對象,則equals相等 
  10.  * 如果是Float、Integer、Double,只要值相等即可 
  11.  */  
  12. public class CompareList {  
  13.     /** 
  14.      * @param args 
  15.      */  
  16.     public static void main(String[] args) {  
  17.         List<Object> la = new ArrayList<Object>();  
  18.         List<Object> lb = new ArrayList<Object>();  
  19.         la.add(new Integer(1));  
  20.         la.add(new String("asd"));  
  21.         la.add(new Double(1.4));  
  22.           
  23.         lb.add(new Float(1.1f));  
  24.         lb.add(new String("asd"));  
  25.         lb.add(new Float(2.6));  
  26.           
  27.         boolean b = compare(la,lb);  
  28.         System.out.println(b);  
  29.     }  
  30.       
  31.     public static boolean compare(List<Object> la,List<Object> lb){  
  32.         boolean flag = true;  
  33.         for(int i = 0; i < la.size(); i++){  
  34.             Object a = la.get(i);  
  35.             Object b = lb.get(i);  
  36.             int ia = 1;  
  37.             int ib = -1;  
  38.             if(a instanceof Float)ia = ((Float) a).intValue();  
  39.             if(a instanceof Double)ia = ((Double) a).intValue();  
  40.             if(a instanceof Integer)ia = ((Integer) a).intValue();  
  41.               
  42.             if(b instanceof Float)ib = ((Float) b).intValue();  
  43.             if(b instanceof Double)ib = ((Double) b).intValue();  
  44.             if(b instanceof Integer)ib = ((Integer) b).intValue();  
  45.               
  46.             if(!a.equals(b) && ia != ib)flag = false;  
  47.         }  
  48.           
  49.         return flag;  
  50.     }  
  51. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章