【问题标题】:Find String in Multidimensional ArrayList在多维 ArrayList 中查找字符串
【发布时间】:2017-12-25 20:17:52
【问题描述】:

场景

我有一个 ArrayList<[String, String, String, String]> 来存储数据。
现在我正在尝试检查ArrayList 是否包含String

for(String i : checkArtefakts){
            System.out.println(i);
            if(headerAndBodyTestSuites.toString().contains(i)){
                System.out.println(i + "  " + headerAndBodyTestSuites.indexOf(headerAndBodyTestSuites.contains(i)));
            }
        }

我已经实现了toString() 方法,但它也没有解决我的问题。

问题

如何查看每个Element of ArrayList ([String,String,String,String])

如果它包含我的search String

为什么它甚至在我的 toString() 方法中找不到它?

public String toString() {
    return "[XQ: " + xqueryFileName + "] [Path: " + testDir + "] [INP: " + firstInputFile
        + "] [INP: " + secondInputFile + "] [EXP: " + expectedFile + "]";

}

示例

ArrayList<HeaderAndBodyTestcase> 其中HeaderAndBodyTestcase =

public HeaderAndBodyTestcase(final String xqueryFileName, final String testDir,
        final String firstInputFile, final String secondInputFile, final String expectedFile)

【问题讨论】:

  • 你可以创建一些更好的数据结构然后ArrayList ([String,String,String,String])
  • [String, String, String, String] 是什么类型?
  • 你有什么推荐的?@Abubakkar @TimBiegeleisen 是时候了 HeaderAndBodyTestcase 需要 4 个字符串。
  • 您想检查您的对象的四个字符串之一(对于每个列表)是否等于search string
  • 你这么不清楚..

标签: java string list arraylist contains


【解决方案1】:

如果您使用的是List<String[]>,您可以使用Arrays.asList(),通过.contains() 方法检查String[] 是否包含您的字符串。

但在您的情况下,它是HeaderAndBodyTestcase 中的List,因此您需要在HeaderAndBodyTestcase 类中实现contains 方法,以检查是否有任何类成员等于搜索到的String

这应该是你的代码:

public boolean contains(String search){
    return this.xqueryFileName.equals(search) || this.testDir.equals(serach) || this.firstInputFile.equals(search) || this.secondInputFile.equals(search) || this.expectedFile.equals(search);
}

【讨论】:

  • contains 而不是 equals 解决了我的问题。谢谢!
  • @0x45 太好了,很高兴它有帮助,是的,如果成员 String s 包含其他字符而不是搜索的字符串,则使用 .contains 会更好。
【解决方案2】:

您需要在类中实现一个方法,该方法将查看搜索字符串是否为属性之一:

public boolean objectContainString(String search) {
     return Arrays.asList(xqueryFileName, testDir, firstInputFile, secondInputFile, expectedFile)
                  .contains(search);
}

并像这样使用:boolean bool = list.stream().anyMatch(e -> e.objectContainString(search));

【讨论】:

  • @0x45 你可以考虑为你喜欢/感谢的答案投票;)
  • 我做了但是我的声望太低了>:-(
  • @0x45 我对你的问题投了赞成票,这样你就可以得到超过 15 个代表,所以你可以随时投票;)
【解决方案3】:

你可以做一些简单的事情:

    String searchedString = "SomeString";
    boolean found = false;
    for(String string : arrayList) {
          if (searchedString.equals(string)){
              found = true;
          }
    }

或者您可以使用 java 流 API。 (需要 Java 8)

    List<String> list = new ArrayList<>();
    list.add("SOME_STRING_1");
    list.add("SOME_STRING_2");
    list.add("SOME_STRING_3");
    String searchString = "SOME_STRING...";
    if(list.stream().anyMatch((string)->string.equals(searchString))) {
        // The string was in the list
    } else {
        // The string was not in the list
    }

其中 arrayList 包含您的字符串。我不确定你所说的 ArrayList 是什么意思,但这可能就是你要找的。​​p>

【讨论】:

  • @0x45 啊哈,您想检查搜索字符串是否是数组列表中任何字符串的子字符串?前任。 ["hello", "goodbye", "hello goodbye"],然后你给它提供例如:"ello",你会在索引 0 和 2 处得到字符串?是吗?
  • 我只是想检查它是否包含字符串,我不关心索引。但一般是的
  • 而不是list.stream().filter((string)-&gt;string.equals(searchString)).findFirst().isPresent()使用list.stream().anyMatch(...)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-10
  • 2018-06-13
  • 2015-07-14
  • 2014-05-24
  • 2017-03-30
  • 1970-01-01
相关资源
最近更新 更多