0x45
0x45

Reputation: 819

Find String in Multidimensional ArrayList

Scenario

I've got an ArrayList<[String, String, String, String]> to store data.
Now I'm trying to check if the ArrayList contains String.

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

I've implemented a toString() Method but it also didn't solve my problem.

Question

How can I check each Element of ArrayList ([String,String,String,String])

if it contains my search String.

And why wouldn't it even find it in my toString() method?

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

}

Example

ArrayList<HeaderAndBodyTestcase> where HeaderAndBodyTestcase =

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

Upvotes: 2

Views: 2001

Answers (3)

Ludvig W
Ludvig W

Reputation: 824

You can do something simple as this:

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

Or you could use the java stream API. (Requires 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
    }

Where arrayList contains your strings. Im not sure what you meant by ArrayList<[String, String, String, String]>, but this might be what you are looking for.

Upvotes: 0

azro
azro

Reputation: 54148

You need to implement a method in the class which will look if the search String is one of the attribute :

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

And use like this : boolean bool = list.stream().anyMatch(e -> e.objectContainString(search));

Upvotes: 3

cнŝdk
cнŝdk

Reputation: 32145

If you were using List<String[]> you can use Arrays.asList(), to check if the String[] contains your string with .contains() method.

But in your case it's a List of HeaderAndBodyTestcase, so you need to implement a contains method in your HeaderAndBodyTestcase class to check if any of the class members is equal to the searched String.

This is how should be your code:

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);
}

Upvotes: 5

Related Questions