File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Find the previous element in a List

 
Daniel Vlad
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a question to think about: What is the previous element when i is 0?
 
Mohamed Sanaulla
Saloon Keeper
Pie
Posts: 3159
33
Google App Engine Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or with what value does the array indexing begins?
 
Daniel Vlad
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3159
33
Google App Engine Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic