This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Find the previous element in a List Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Find the previous element in a List" Watch "Find the previous element in a List" New topic
Author

Find the previous element in a List

Daniel Vlad
Greenhorn

Joined: Aug 07, 2011
Posts: 29
Ok i have a List that looks sort of like this:


now what i'm trying to do is this to detect if the string that precedes a given string is "=" but i can't figure out how to get that previous string:



i tried it in different ways, one of them being as you can see in the above code. I understand why i'm getting an error at that line but i can't really figure out if it is possible to do what i want using this kind of list.

Any help would be appreciated,
Daniel
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
You want to compare an element with the previous element. So basically the logic should work from the second element in the List. You should ignore the first one as no element will be present in the previous index.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

Here's a question to think about: What is the previous element when i is 0?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

or with what value does the array indexing begins?


Mohamed Sanaulla | My Blog
Daniel Vlad
Greenhorn

Joined: Aug 07, 2011
Posts: 29
Ok well, array indexing starts from 0 (unless i'm missing something). So if i'm understanding correctly "int i" should get the value of "1" so when i try to get the previous element i don't hit my head on the ArrayIndexOutOfBounds ? . But if i do that i won't be able to grab the first element of my list, so i guess i'm still missing something?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

Daniel Vlad wrote:Ok well, array indexing starts from 0 (unless i'm missing something). So if i'm understanding correctly "int i" should get the value of "1" so when i try to get the previous element i don't hit my head on the ArrayIndexOutOfBounds ? . But if i do that i won't be able to grab the first element of my list, so i guess i'm still missing something?

So if i=0 then you dont check for previous element, because there exists none. And then your loop continues to the next element.

or you could start with i=1, because there would be a previous element starting from the second element.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Find the previous element in a List
 
Similar Threads
why is the problem with this logic ?
Checking for No Value
string tokenizer prob
InputStreamReader returns null
Array out of bounds exception .split()