【问题标题】:Trying to get the size of an inner arraylists of class object inside an outer arraylist of arraylists试图在数组列表的外部数组列表中获取类对象的内部数组列表的大小
【发布时间】:2020-09-29 06:21:07
【问题描述】:

我有一个对象类型的数组列表的数组列表,试图获取特定数组列表的大小,但我一直在获取整个数组列表的大小

healthyKoalas = new ArrayList<Koala>();
hKoalas = new ArrayList<ArrayList<Koala>>();

public void hkoala(int x)
    {
         for (int i=0; i<x; i++) // x is the number of healthy koalas in an observation point
         {
             int age = (int)(Math.random()*18)+1; 
             Koala k = new Koala(age); 
             addHKoala(k);
             healthyKoalas.sort(Comparator.comparing(Koala::getAge).reversed());
             //Collections.sort(healthyKoalas, Collections.reverseOrder());
            }
        hKoalas.add(healthyKoalas);
    }

然后当我尝试

hKoalas.get(x).size();

它给了我所有考拉对象的数量,而我想获得特定内部数组列表的大小

【问题讨论】:

  • public void hkoala(int x) { for (int i=0; i
  • this 回答你的问题了吗?
  • 请提供更多信息,例如数组列表,以便我们更好地理解您的观点。
  • ArrayList = new ArrayList(); hKoalas> = new ArrayList>();
  • public void hkoala(int x) { for (int i=0; i

标签: java loops arraylist


【解决方案1】:

给定hKoalas = new ArrayList&lt;ArrayList&lt;Koala&gt;&gt;();,然后hKoalas.get(1).size() 为您提供第二个“内部列表”的元素数量(索引 1 = 商店中的第二个,因为索引 0 是第一个一)。如果您认为它返回了所有内部列表中所有考拉的数量,那么您的代码中存在另一个错误并且您错误地识别了正在发生的事情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-22
    • 2020-09-02
    • 2019-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多