• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

2D Array or linked list

 
Krishna Ruddarraju
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • 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
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
srinivas srinivasmeenavalli
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic