我是新來的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
}
}
我明白做你的方式,但我已經做了沒有地圖的類似。謝謝! – jordan 2015-04-03 16:13:43