【问题标题】:Displaying group of elements in an Arraylist在 Arraylist 中显示元素组
【发布时间】:2015-12-09 12:48:48
【问题描述】:

如果 contition 为真,我正在尝试显示 arraylist 的几个元素。该方法获取应在arrayList 中找到的字符串。之后,在已找到的 List 中的行之后包含一些其他值。 我需要打印出 1_4_1334-Automatic 的 thause 行。我曾尝试使用 Iterator 但没有运气。它只是看到我无法得到它。

因此,如果我正在寻找 2210002_4_1294​​-Group 我应该得到所有包含“Automatic”的字符串,直到达到 2210003_4_1295-Group。

知道怎么做吗?

非常感谢:)

MyArrayList:

2210002_4_1294-Group
1_4_1334-Automatic
2_4_1336-Automatic
3_4_1338-Automatic
4_4_1340-Automatic
5_4_1342-Automatic
6_4_1344-Automatic
7_4_1346-Automatic
8_4_1348-Automatic
9_4_1350-Automatic
2210003_4_1295-Group
1_4_1378-Automatic
2_4_1380-Automatic
2210004_4_1296-Group
1_4_1384-Manual
2_4_1386-Manual

方法可能如下所示:

private void findValueInList(String group){
    Iterator<String> iter = arrayList.iterator();
    while(iter.hasNext()){
        String name = iter.next();
        if(name.equals(group)){
             here i need to get ValueThatINeed
        }
    }
}

【问题讨论】:

  • 您的问题不清楚...您到底卡在哪里了?如果您可以提供列表中的实际值示例以及您将打印出来的示例(以及为什么)...
  • 嗨,假设有一个字符串列表。有一个我知道的价值,在那个价值之后,我需要获得更多的价值。但只是 thuse 价值观,不要再进一步了。因此,在我找到 ValueToBeFound 之后,我需要获取 ValueThatINeed ,仅此而已。在获得值之后,我可以退出循环或其他东西。你知道我在说什么吗? :)
  • 我还更新了我的帖子以更准确:)

标签: java string list arraylist iterator


【解决方案1】:

我猜你的问题已经回答了Here

只需遍历您的数组列表并检查每个值,如下面的代码:

ArrayList<String> myList ...
String searchString = "someValue";

for (String curVal : myList){
  if (curVal.contains(searchString)){
    // The condition you are looking for is satisfied
  }
}

【讨论】:

    【解决方案2】:

    我是这样解决的:

        private ArrayList<String> filterList(String nameToFind) {
        ArrayList<String> elements = new ArrayList<String>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).equals(nameToFind)) {
                while (list.get(i+1).contains("Manual") || list.get(i+1).contains("Automatic")) {
                    elements.add(list.get(i+1));
                    i++;
                }
            }
        }
        return elements;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-13
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-21
      相关资源
      最近更新 更多