This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
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 OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "2D Array or linked list" Watch "2D Array or linked list" New topic

2D Array or linked list

Krishna Ruddarraju

Joined: Dec 24, 2009
Posts: 4

I am copying some values into a 2D array.

row col1 col2 col3 col4

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?

Marco Ehrentreich
best scout

Joined: Mar 07, 2007
Posts: 1294

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.

Rob Spoor

Joined: Oct 27, 2005
Posts: 20132

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.

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.
I agree. Here's the link:
subject: 2D Array or linked list
jQuery in Action, 3rd edition