• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Print attributes of ListArray in reverse

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Guys,
thanks for all your help thus far, really learning alot!

I have a ListArray called lines 2 which contains some info, now i can print all the information in the ListArray out easily from position 0 to the end of the array doing as follows;


Works grand but i want to print from the end of the list to the top starting at the last bit of information in the ListArray, i tried this;
[/code]
for (int i = lines2.size(); i > 0; i--)
{
System.out.println(lines2.get(i));
}
[/code]

But it causes an error, I thought by making i equal the size of the array and having as long as i is greater than 0 to decrement down to 0 from the end of the list it would print i from last position in array, am i thinking about it wrong?

Thanks Mark
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ArrayList, not ListArray, yes?

The only problem here is an "off-by-one error." The last element is at index size()-1, not size(), and the first element is at 0, not 1; so a proper loop could look like

for (int i=lines2.size()-1; i >= 0; --i)
 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry ArrayList,

Yea that worked, thanks very much



Can i just get it straight in my head,
In reality if i write int i=lines2.size();, and print i it is 6 (six elements),
but since the first element is located at 0, you write int i=lines2.size()-1 to start at element 5 which is last in ArrayList and i >= 0 so it will iterate down to 0 opposed to stopping at 1 if i write i > 0, Im i correct there?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, perfect.
 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great Thanks
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic