2015-04-03 87 views
1

我是新來的arraylists,想問我如何知道如果傳入參數數組是哪種類型(字符串,詮釋,雙..),我怎麼能在以下方法處理?:有兩種類型的Arraylist:如何確定第一種類型?

public static <T> Pair<T, Integer> mode(T items[]) 

第二種類型始終是一組Integers,但T(第一種類型)可以是任何其他類型,如Integer,Double,String等。我需要提取傳入方法的最常用字符(或數字或字符串)的數量。在測試文件我有水木清華這樣的:

@Test(timeout=2000) public void string_mode_3(){ 
    test_mode(new String[]{"b","a","b","a","b","c","b"}, 
       "b",4); 

@Test(timeout=2000) public void integer_mode_1(){ 
    test_mode(new Integer[]{30,10,10,20}, 
       10,2); 

如何將我必須找出,如果第一類是整數,雙,字符串的方法mode()

public class Pair<X,Y>{ 
    private X first; 
    private Y second; 

    public Pair(X x, Y y){ 
    this.first = x; 
    this.second = y; 
    } 

    public X getFirst(){ 
    return this.first; 
    } 
    public Y getSecond(){ 
    return this.second; 
    } 

    public boolean equals(Object o){ 
    if(!(o instanceof Pair)){ 
     return false; 
    } 
    Pair p = (Pair) o; 
    return 
     this.first.equals(p.first) && 
     this.second.equals(p.second); 
    } 

    public String toString(){ 
    return String.format("(%s,%s)",first,second); 
    } 

} 

import java.util.ArrayList; 

public class Mode { 

    public static <T> Pair<T, Integer> mode(T items[]) 
    { 
     return 
    } 



} 

回答

1

你並不真的需要知道數組元素的類型來實現這一點。請注意,您只需計算每個元素出現的次數,不管他們是什麼類型。

這是使用泛型的優勢之一:您可以創建可透明地用於許多不同類型的代碼。

像這樣的東西會工作:

public static <T> Pair<T, Integer> mode(T items[]) { 
    // If the array is empty, return a dummy pair object 
    if (items.length == 0) { 
     return new Pair(null, 0); 
    } 

    // Create a map to store the elements count 
    Map<T, Integer> countFromItem = new HashMap<>(); 
    // For each item in the array 
    for (T item : items) { 
     // Get the current count 
     Integer count = countFromItem.get(item); 
     // If there is no current count 
     if (count == null) { 
      // Set the count to 0 
      count = 0; 
     } 
     // Add 1 to the item current count 
     countFromItem.put(item, count + 1); 
    } 

    // After we found correct count for each element 
    T mode = null; 
    int maxCount = 0; 
    // Go through each entry (element: count) in the map 
    for (Map.Entry<T, Integer> entry : countFromItem.entrySet()) { 
     // If the this entry count is greater than the greatest count until now 
     if (entry.getValue() > maxCount) { 
      // This entry element is the mode 
      mode = entry.getKey(); 
      // This entry count is the maxCount 
      maxCount = entry.getValue(); 
     } 
    } 
    return new Pair(mode, maxCount); 
} 
+0

我明白做你的方式,但我已經做了沒有地圖的類似。謝謝! – jordan 2015-04-03 16:13:43

0

您可以嘗試instanceOf來檢查對象的類型。對於仿製藥,你可以使用整數,雙對象並做了檢查這個..只是一個黑客:)編碼快樂.. 編輯:附加代碼的我一個供大家參考

if (objectVal instanceof Double) { 
       errors.add(CommonValidatorUtil.isNegative(field.getName(), (Double) field.get(obj))); 
      } else if (objectVal instanceof Long) { 
       errors.add(CommonValidatorUtil.isNegative(field.getName(), (Long) field.get(obj))); 
      } else if (objectVal instanceof String) { 
       errors.add(CommonValidatorUtil.isNegative(field.getName(), Double.valueOf((String) field.get(obj)))); 
      } else if (objectVal instanceof Integer) { 
       errors.add(CommonValidatorUtil.isNegative(field.getName(), (Integer) field.get(obj))); 
      } 
+0

我試圖像(項目[]的instanceof字符串),但有些事不便是錯誤的 – jordan 2015-04-03 06:36:27

+0

嘗試(項目[0]的instanceof字符串) – Ravikiran763 2015-04-03 06:39:34

相關問題