• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

2D Array or linked list

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am copying some values into a 2D array.

row col1 col2 col3 col4
row1
row2
row3

Based on input from user for col1, col2, I have to show its corresponding col3 and col4 for all rows.

Number of rows is not definite(mostly unpredictable).
Should I go ahead with creating 2D arrays by defining number of rows as some number(eg:100)?
Will linkedlists make this complicated?

Could someone please let me know what needs to be done?

Thanks,
Krishna
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Krishna,

if you have multi-dimensional data of unpredictable size I'd really recommend you to use collections, i.e. something like LinkedLists to implement this. That doesn't make it look beautiful but at least you can deal with all kind of 2-dimensional data and you don't waste too much memory which could be the case with a large 2-dimensional array.

Marco
 
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the number of columns is fixed, use a List<String[]> or List<Object[]>. If not, a List<List<String>> or List<List<Object>> is the way to go. A fixed array size with a String[][] or Object[][] is bad; you need to be careful of null values and even worse, an array that is too small.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
looks like number of columns are fixed in your requirement

If the number of columns is fixed, use a List<String[]> or List<Object[]>.


I agree with Rob commnets. I have another recommendation with value objects.
you can have like following way also.
List<RowVO> , RowVO consists of col1,col2,col3,col4.
reply
    Bookmark Topic Watch Topic
  • New Topic