• Post Reply Bookmark Topic Watch Topic
  • New Topic

get values in multiple ArrayList  RSS feed

 
sam liya
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have several ArrayLists which contain different amount of data.Below is example



I need to print values of these ArrayList.So I used for loop.If there is no valus in any arraylist i print blank.how to do this?because if i try below it gives me IndexOutOfBoundsException.

a.get(3)
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the index of the first element? Consequently, which indexes can you use for a list containing 3 elements?

Also check out Iterators combined with enhanced for loops: https://blogs.oracle.com/CoreJavaTechTips/entry/using_enhanced_for_loops_with
 
sam liya
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the problem is number of elements in ArrayLists are differ.So c has 5 element.Then i have to run the for loop 5 times.So when 3rd time it definitely give IndexOutOfBoundsException for b.How to avoid that?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sam liyanage wrote:the problem is number of elements in ArrayLists are differ.So c has 5 element.Then i have to run the for loop 5 times.So when 3rd time it definitely give IndexOutOfBoundsException for b.How to avoid that?


Can't you simply check the size of the ArrayList before trying to use it? you should be doing that regardless.

Henry
 
sam liya
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Henry.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!