2017-08-03 65 views
0

例如,在C++中,我可以找到INT = 0,向量< INT> = 1,矢量< <矢量< INT>> = 2由以下:如何查找容器深度(例如:ArrayList <String> = 1,ArrayList <ArrayList <String>> = 2)?

#include <vector> 
#include <stdlib.h> 

template <typename T> 
class Printer{ 
public: 
    static int print(){ 
     return 0; 
    } 
}; 

template <typename T,typename Alloc> 
class Printer<std::vector<T,Alloc> >{ 
public: 
    static int print(){ 
    return 1+Printer<T>::print(); 
    } 
}; 

int main(){ 
    printf("%d\n",Printer<int>::print()); 
    printf("%d\n",Printer<std::vector<int> >::print()); 
    printf("%d\n",Printer<std::vector<std::vector<int> > >::print()); 
    return 0; 
} 

,其不需要任何实例,例如:

std::vector<int> v; 

要做到这一点。我怎样才能在Java中实现这个功能?我想:

public static void print(List<Integer> list){ 
} 

public static <T> void print(List<? extends T> list) { 
} 

public static <T>void print(){ 
} 

public static void print() { 
} 

但都表示 “这两种方法有同一消失”。我也考虑过使用如果其他像:

public static void print(Object obj) { 
    if(obj instanceof List){ 
     List list=(List)obj; 
     for(Object obj2 : list){ 
      print(obj2); 
     } 
    }else{ 
    } 
} 

但我不能在函数中获取元素的类型。是否有可能在Java中做到这一点?

+0

很多例子,但对我来说,如果你要显示实际的Java列表,以及你期望与他们发生什么,会更有帮助。不,Java的泛型不如C++模板强大。 – GhostCat

回答

-1

在Java中,编译时会删除“T”。 如果你想获得List的深度,你需要确保这个List不为null。比得到第一个,并检查该类。

public int getDepth(Object collection){ 
    return getDepth(collection,1); 
} 

/** 
* get Collection depth 
* @param depth now depth 
* @return 
*/ 
private int getDepth(Object collection,int depth){ 
    Object temp; 
    if(collection instanceof Collection 
      && !((Collection)collection).isEmpty()){ 
     //get first one 
     temp = ((Collection)collection).iterator().next(); 
     return getDepth(temp,depth +1); 
    }else { 
     return depth; 
    } 
} 

:)

0

这里有一个递归方法,会做你想要达到的目标。在初始呼叫时,始终将depth参数传递为0,这就是计数器。

public static int listDepth(Object obj, int depth){ 
    if(obj instanceof List){      //Check if given object is a list 
     List<?> list = (List)obj;     //Retrieve the list 
     depth++;         //Increment depth counter 
     if(list.isEmpty())       //Nothing in the current list 
      return depth;       //Then return current depth 
     else depth = listDepth(list.get(0), depth);//Else call the method on the first element, with the current depth    
    }   
    return depth;         //obj is not a List, we return the depth 
} 

而且这里有一个例子:

ArrayList second = new ArrayList<String>(); 
ArrayList first = new ArrayList<ArrayList>(); 

first.add(second); 

System.out.println(" Depth : " + listDepth(first, 0));// Output : 2 

和另一之一:

ArrayList third = new ArrayList<String>(); 
ArrayList second = new ArrayList<ArrayList>(); 
ArrayList first = new ArrayList<ArrayList>(); 

third.add("Not an array"); 
second.add(third); 
first.add(second); 

System.out.println(" Depth : " + listDepth(first, 0));// Output : 3 

编辑:替换上阉支票或不在列表中的try/catch是空的,因为这是一个更合适的方式来做到这一点。

+0

“在初始调用时始终为深度参数传递0”这是糟糕的设计。有一个私人函数,它需要2个参数,一个公共函数只需要一个参数,除了调用'privateFunc(list,0)'外,什么都不做。' – Michael

+0

@Michael如果他希望计数器从一个特定的' int' ..我只是在这里提出一个算法来完成这个任务,这取决于他如何实现它的好方法。但我明白你的观点。 – Asew

+0

@Michael我不知道,但这不取决于我.. – Asew

相关问题