简体   繁体   English

获取此列表中每个数组的大小

[英]get the size of each array in this list

I inserted these two ArraysList into this List : 我将这两个ArraysList插入到此List

    ArrayList<String> category = new ArrayList<>();
    ArrayList<Integer> number = new ArrayList<>();
    List list = new ArrayList<>();

    category.add("cat");
    category.add("dog");
    category.add("fish");
    category.add("Hamster");

    number.add(1);
    number.add(2);
    number.add(3);
    number.add(4);

    list.add(category);
    list.add(number);

Now how do I get the size of each array in this list? 现在如何获取此列表中每个数组的大小?

Try like this 这样尝试

 int sizeOfCategory = category.size();
 int sizeOfNumber = number.size();

OR 要么

    for(Object li : list) {
        System.out.println(((List) li).size());
    }

Cast the result object of get method from the list with ArrayList. 使用ArrayList从列表中强制转换get方法的结果对象。

for(int i=0; i<list.size(); i++) {
   System.out.println(((ArrayList)list.get(i)).size());
}

Check object instanceOf , if it is type of ArrayList which you added, then down-cast it to ArrayList and get size. 检查对象instanceOf ,如果它是您添加的ArrayList类型,则将其向下转换为ArrayList并获取大小。

list.add(category);
list.add(number);
for (Object o : list) {
    if (o instanceof ArrayList) {
        System.out.println(((ArrayList) o).size());
    } else {
         // unknown type
    }
}

If you want you can get List item type too. 如果需要,您也可以获取列表项类型。

System.out.println("Object is type of " + o.getClass());

The problem is that you are using the raw typ of List . 问题是您正在使用List原始类型 You would get it on your own, if you had declared list using a generic type T for List<T> which provides a size() method. 如果您使用List<T>通用类型 T size()提供了size()方法size()声明了list ,则可以自己获得它。 Using the raw type the interface List will return elements of type Object - hence there is no size() method. 使用原始类型,接口List将返回Object类型的元素-因此,没有size()方法。

As list contains two instances of ArrayList there are some candidates for T available. 由于list包含ArrayList两个实例,因此有一些T候选。 For example: 例如:

  • ArrayList<?>
  • List<?>
  • Collection<?>

It depends on what you need to do with the inner lists ( category , number ). 这取决于您需要使用内部列表( categorynumber )。 If you need only the size of them then Collectiion<?> will be sufficient. 如果只需要它们的大小,那么Collectiion<?>就足够了。

System.out.println(list.get(0).size()); // 4
System.out.println(list.get(1).size()); // 4

Since the inner lists are using Integer and String as their types, there is no much they have in common in order to replace the wildcard <?> . 由于内部列表使用IntegerString作为它们的类型,因此它们之间没有什么共同之处来替换通配符<?> String and Integer are both implementing Comparable but are only comparable to instance of their own type. StringInteger都实现Comparable但仅可与它们自己的类型的实例进行比较。 Thus declaring List<Collection<? extends Comparable<?>>> list 因此声明List<Collection<? extends Comparable<?>>> list List<Collection<? extends Comparable<?>>> list will not give any additional value. List<Collection<? extends Comparable<?>>> list将不会提供任何附加值。 Hence having list which contains both inner lists of different types without a useful common type limits the usability of list to cases where you don't need the concrete type of the elements of the inner lists. 因此,其list包含不同类型的两个内侧名单没有一个有用的常见类型限制的可用性list到你不需要内列出的元素的具体类型案件。


Further reading: What is a raw type and why shouldn't we use it? 进一步阅读: 什么是原始类型,为什么我们不应该使用它?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM