简体   繁体   English

java中ArrayList中的ArrayList

[英]ArrayList in the ArrayList in java

If I have the code likes this List<List<Maindish>> meal , the List<Maindish> is in other class like: public Maindish (salad, rice, drink) .如果我的代码喜欢这个List<List<Maindish>> meal ,则List<Maindish>在其他类中,例如: public Maindish (salad, rice, drink) So if I want the value of the second maindish (rice) in the first meal, (I may need to go through the loop later for all valuable) should I do something like:因此,如果我想要第一顿饭中第二道主菜(米饭)的价值,(我可能需要稍后遍历所有有价值的东西),我应该这样做:

for (i = 0; i < meal.length(); i++) {
        return meal[i][1];
    }

The data type you are using is using is Lists not Arrays.您正在使用的数据类型是列表而不是数组。 Try using List.get(int i) other than [].尝试使用 [] 以外的 List.get(int i)。

If you want to do an operation on first elements of your sub-lists, you can try the code below:如果要对子列表的第一个元素进行操作,可以尝试以下代码:

 for (List<Maindish> list : meal) {
        System.out.println(list.get(0));    
    }

First you need to create setter / getter for your model class Maindish首先,您需要为模型类Maindish创建 setter / getter

than you can iterate like below to get first item in your sub list比您可以像下面这样迭代以获取子列表中的第一项

     // Assuming your sublist have 1 Maindish
    for(List<Maindish> submeal : meal){
        String rice = submeal.get(0).getRice(); // Assuming getRice() is your getter and return type is String
    }

You don't have to iterate by each element to read or write elemts in Lists.您不必遍历每个元素来读取或写入列表中的元素。 You can utilize the following methods.您可以使用以下方法。

  • add(int index, E element)添加(整数索引,E 元素)
  • addAll(Collection c) addAll(集合 c)
  • addAll(int index, Collection c) addAll(int 索引,集合 c)
  • get(int index)获取(整数索引)
  • set(int index, E element)设置(整数索引,E 元素)

You can find the full list of methods available here .您可以在此处找到可用方法的完整列表。

you ask help with value of the second maindish (rice) in the first meal , Since this is a list of a list including maindishes what we can retrieve is the second maindish in the first meal of the first list of meals .您向第一餐中的第二道主菜(米饭)的价值寻求帮助,由于这是一个包括主菜的清单,我们可以检索的是第一餐清单中第一餐中的第二道主菜 that can archive by the following code.可以通过以下代码存档。

meal.get(0).get(0).getRice();

Assumption : That you have implemented all the setter and getters in Maindish class.假设:您已经在 Maindish 类中实现了所有的 setter 和 getter。


If you are saying at least for this example that you want to always return the 2nd main dish for each meal, you would probably need to build another ArrayList of only the 2nd main dishes.如果你至少在这个例子中说你想要总是为每顿饭返回第二道主菜,你可能需要构建另一个仅包含第二道主菜的 ArrayList。 By returning in your example, you will only get the first middle dish no matter how many items are in meal.通过返回您的示例,无论膳食中有多少项目,您都只会得到第一道中间菜。 I'm making a lot of assumptions because you didn't give enough info, but if we say that the actual dishes stored in a Maindish object are strings, then you could do the following:我做了很多假设,因为您没有提供足够的信息,但是如果我们说 Maindish 对象中存储的实际菜肴是字符串,那么您可以执行以下操作:

ArrayList<String> secondMains = new ArrayList<>();
Iterator<MainDish> it = meal.iterator();

while(it.hasNext()) {
    Maindish mainDish = it.next();
    if (mainDish.size() >= 2) {
        secondMains.add(mainDish.get(1));
    }
}
return secondMains;

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

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