jQuery in Action, 2nd edition*
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 Android Security Essentials Live Lessons this week in the Android 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: 14074
    
  16

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: 3068
    
  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: 3068
    
  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
InputStreamReader returns null
string tokenizer prob
Array out of bounds exception .split()
Checking for No Value
why is the problem with this logic ?