Data Structure Decision

T

TonY

hi,

I need advice on the appropriate Java Data Structure to use in my
program.

I need a 2D data structue where the number of rows and columns are not
constant and can't preset. the number of rows and columns can only
increase during the program execution. each row/column can have
different size.

in the program bulk processing, i 'll have to compare a certain
variable value with the column elements. upon this comparaison, i
might insert this variable in a particular column

i can think on a vector of vector, but is there not any more suitable
java built-in data structure

many thanks
 
O

Oscar kind

TonY said:
I need a 2D data structue where the number of rows and columns are not
constant and can't preset. the number of rows and columns can only
increase during the program execution. each row/column can have
different size.

You mention rows and columns, so I assume you're talking about a
2-dimensional euclidian space where the coordinates are non-negative.

In this case, I'd suggest a List of List's.

in the program bulk processing, i 'll have to compare a certain
variable value with the column elements. upon this comparaison, i
might insert this variable in a particular column

Thus, I'd use an ArrayList to assign to the List variables. It has better
random access than a LinkedList, and if columns aren't removed or inserted
(only appended), it's inefficiencies in that area don't surface.

i can think on a vector of vector, but is there not any more suitable
java built-in data structure

Vector acts the same as an ArrayList, but has additional synchronization.
Do not use it unless you really need that, and even then you're probably
better off using a synchronized List anyway: synchronization is just part
of thread-safe programming, and is likely to induce a false sense of "ok,
that's fixed". It does at least with me.

I'd use the List interface and one of the new List classes (ArrayList &
LinkedList) instead.
 
M

Michael Rauscher

Hi Tony,
hi,

I need advice on the appropriate Java Data Structure to use in my
program.

I need a 2D data structue where the number of rows and columns are not
constant and can't preset. the number of rows and columns can only
increase during the program execution. each row/column can have
different size.

in the program bulk processing, i 'll have to compare a certain
variable value with the column elements. upon this comparaison, i
might insert this variable in a particular column

i can think on a vector of vector, but is there not any more suitable
java built-in data structure

Why should there be such a special data structure? Write your own.

Here's an untested approach, handle it like pseudo-code:

class Index2D {
public int row;
public int column;

public Index2D( int row, int column ) {
this.row = row;
this.column = column;
}

public int hashCode() {
return row+column; // there are better ones
}

public boolean equals( Object o ) {
if ( o == null || !(o instanceof Index2D) )
return false;
Index2D i = (Index2D)o;
return (row == i.row) && (column == i.column);
}
}

class TwoDimensionalDataSet {
private HashMap data = new HashMap();

public TwoDimensionalDataSet() {
}

public void set( int row, int column, Object value ) {
data.put( new Index2D(row,column), value );
}

public Object get( int row, int column ) {
return data.get( new Index2D(row, column) );
}
}

Bye
Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top