-1

i'm trying to display few elements of an arraylist if contition is true. The method gets String that should be found in arrayList. After that there are some other values that are contained after the line in List that has beed found. I need to print thause line's out that would be 1_4_1334-Automatic.... I have tried to use Iterator but with no luck. It just seens that i just cannot get it.

So if am looking for 2210002_4_1294-Group i should get all strings that contain "Automatic" till 2210003_4_1295-Group is reached.

Any idea how it could be done ?

Thanks a lot :)

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

Method might look like this:

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
        }
    }
}
Artiom
  • 81
  • 12
  • 2
    Your question is unclear... Where exactly are you stuck? It would help if you can give a sample of _actual values_ which will be in the list and those which you will print out of them (and why)... – Codebender Sep 13 '15 at 11:36
  • Hi, imagine there is an list of strings. there is an value that i know and after that value there are some more values that i need to get. But just thause values and do not go no further. So after i found ValueToBeFound i need to get ValueThatINeed and thats it. After thause values are got i could exit the loop or something. Do you know what i ment ? :) – Artiom Sep 13 '15 at 11:46
  • I also updated my post to be more accurate :) – Artiom Sep 13 '15 at 11:59

2 Answers2

0

I guess your question is already answered Here

Simply iterate over your arraylist and check each value like the code below:

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

for (String curVal : myList){
  if (curVal.contains(searchString)){
    // The condition you are looking for is satisfied
  }
}
Community
  • 1
  • 1
Arash khangaldi
  • 110
  • 2
  • 15
0

I solved it like this:

    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;
}
Artiom
  • 81
  • 12