-1

Here's my list example:

List<String> l2 = new ArrayList<String>();
l2.add("Apple");
l2.add("Kiwi");
l2.add("Orange");

So Basically I want to iterate through this list until it finds for example "Orange" then it'll return a Line number (3 in this example). But I'm really not sure how to do this. Anyone that can help me?

luk2302
  • 46,204
  • 19
  • 86
  • 119
  • 1
    what have you tried so far? Your current expected output could simply be achieved using indexOf – luk2302 Dec 22 '15 at 21:11
  • 3
    It sounds a lot like a simple `List.indexOf(Object o) + 1`. If your List is sorted, you could write a faster search, like a binary search – phflack Dec 22 '15 at 21:15
  • do you want to print the line number of the code or the index of "Orange" in your list? For line numbers, you can use `new Exception().getStackTrace()[0].getLineNumber()` – GregH Dec 22 '15 at 21:18

3 Answers3

0

There are many ways to do this while iterating over the list. Here's an example:

int i = 0;
for( String s : l2){
    if(s.equals("Orange")) System.out.println(i);
    i++;
}
Nic Wilson
  • 36
  • 4
  • this will print the index of "Orange" in the List, not necessarily what line number `l2.add("Orange")` is on – GregH Dec 22 '15 at 21:15
  • 1
    I don't believe that is what the OP was asking for. If I am mistaken OP please correct me. – Nic Wilson Dec 22 '15 at 21:16
  • `foreach` is not a valid keyword, it's called a for-each loop but uses the same `for` keyword. Also note that this is the wrong usage of a for-each loop, you should just use a normal for loop in this case – phflack Dec 22 '15 at 21:18
  • @peggy If OP was looking for *literal* program line number, the expected result would be four (or greater - once you include the class this code is in) rather than 3 as the OP provides – DoubleDouble Dec 22 '15 at 21:19
  • @DoubleDouble Or if your line numbers start at 0, it could be 3. Except it seems to be clear that they meant in the List, not in the program – phflack Dec 22 '15 at 21:22
  • @DoubleDouble Ah yes you are correct. Sorry- I saw line number and thought line number... not index. – GregH Dec 22 '15 at 21:22
  • @phflack Sorry about the foreach, too much C# lately. And I don't believe it's correct to say it's the "wrong" usage for the for-each loop. The choice seems stylistic and should compile to comparable bytecode. – Nic Wilson Dec 22 '15 at 21:24
  • 1
    A for-each loop is designed to be clean and hide the iterator. If you're going to use the index it is more common to instead use an indexed for loop instead. [Check this answer](http://stackoverflow.com/a/3431543/5475891). Overall, using the for-each loop implies you don't care about the index – phflack Dec 22 '15 at 21:26
  • @phflack It may be more common but either way is correct. I think the for-each looks prettier. – Nic Wilson Dec 22 '15 at 21:28
  • @phflack *"I think the for-each looks prettier"* lmao. – user692942 Dec 22 '15 at 21:47
0

You need a for loop to iterate through any Array or list. In this case being an Array list you would iterate through it with the following for loop

for(int x = 0; x < l2.size();x++){
    if(l2.get(x).equals("Orange")){
      // this is where the code will end up when it selects Orange
    }
}

The l2.size() method gets the whole size of the ArrayList and x 1 is added to x each time it is smaller than the Arraylist size. The if statement will stop as soon as it finds the text in the ArrayList in the equals parameters, this is where you should put your code if you want to add, remove or do something at this point.

HelloWorld
  • 128
  • 1
  • 12
0

Did some research and found a much easier way: int indexOf(Object o) Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

  • 1
    @phflack actually provided this in a comment 10 minutes ago, but glad you figured it out regardless. – DoubleDouble Dec 22 '15 at 21:27
  • @DoubleDouble Made it an answer to make it easier to see. And I didn't see his comment. – Alan Tavakoli Dec 22 '15 at 21:29
  • @DoubleDouble actually luk2302 suggested it [first](http://stackoverflow.com/questions/34424485/iterate-through-a-list-until-a-specific-string-and-say-the-line-number#comment56590605_34424485). – user692942 Dec 22 '15 at 21:44
  • @Lankymart Thanks for clarifying. – DoubleDouble Dec 22 '15 at 21:50
  • I do think whoever is downvoting the answer because "someone else first" is rather ridiculous, I more or less just wanted to point out that sometimes people should pay attention to the comments.. If they truly wanted to get answer-points they would have posted it as an answer. – DoubleDouble Dec 22 '15 at 21:50
  • I mostly didn't provide it as an answer because I didn't feel it was enough to be an answer with the given question. It was unclear what you were asking so it was unclear what the answer should be – phflack Dec 23 '15 at 16:07