aspose file tools*
The moose likes Java in General and the fly likes 2D Array or linked list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "2D Array or linked list" Watch "2D Array or linked list" New topic
Author

2D Array or linked list

Krishna Ruddarraju
Greenhorn

Joined: Dec 24, 2009
Posts: 4
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

Joined: Mar 07, 2007
Posts: 1282

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

Joined: Oct 27, 2005
Posts: 19722
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
srinivas srinivasmeenavalli
Ranch Hand

Joined: Jul 13, 2008
Posts: 65
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: 2D Array or linked list