arjacsoh
arjacsoh

Reputation: 9232

How to implement vector of arrays in Java?

I want to have a resizable structure in Java to keep one dimension-arrays or vectors of type double. What is the best way to do?

Is it possible to pass an array as parameter to an Arraylist? May be to a Vector?

If not what is a quite reasonable solution?

After all, based on the proposed implementation, what is the most effective way to gain values from the structure and to use them on calculations (adding arrays, multiply arrays ,etc).

Upvotes: 2

Views: 9395

Answers (7)

Donal Fellows
Donal Fellows

Reputation: 137567

Java's arrays are objects, so anywhere you can pass an Object, you can pass an array. An ArrayList is ideal for building an ordered sequence of arrays; don't use Vector unless you've got an old API that you're coding to (or you need the exact type of synchronization implemented by it; you probably don't want that).

Internally, an ArrayList contains an array of Object references.

If you're doing rectangular matrices or sparse arrays, there can be better ways of implementing things. Matrices incur less overhead if implemented with a single array rather than an array of arrays (or worse, an ArrayList of ArrayLists) but you have to do more work to hide the fact. Sparse arrays are better done as a variation on a Map (itself a somewhat costly thing, but you save by being able to omit much storage).

Upvotes: 0

John B
John B

Reputation: 32949

I would probably use a ListMultmap

ListMultimap<Integer, Double> listOfLists;

The integer key would be your index (0, 1, 2, etc). This structure takes care of creating the inner lists for you. It will check if a list already exists at an index and create prior to the insert if appropriate.

Upvotes: 1

KV Prajapati
KV Prajapati

Reputation: 94645

With generics, you can pass any type which is a sub-class of java.lang.Object. (Java primitive types are not sub-class of java.lang.Object)

ArrayList<ArrayList<Double>> doubles=new ArrayList<ArrayList<Double>>();
//or
Vector<Double> doubles=new ArrayList<Double>();

and of course you can pass array object.

ArrayList<String[]> obj=new ArrayList<String[]>();

Upvotes: 3

michael667
michael667

Reputation: 3260

It is possible to have List<List<Double>> or List<Double[]>.

You should use the more modern List/ArrayList instead of Vector.

The most effective choice depends on what exactly you want to do with it, but initially, you should look at the implementations of the List interface

Upvotes: 0

Jason S
Jason S

Reputation: 189646

Is this for matrices? If so, look at JAMA or CERN Colt, rather than trying to roll your own.

Upvotes: 0

helpermethod
helpermethod

Reputation: 62155

Create an ArrayList holding references to other ArrayLists, like so:

List<List<Double>> listOfLists = new ArrayList<List<Double>>();

List<Double> doubleList = new ArrayList<Double>();

listOfLists.add(doubleList);

You should not create a List of arrays, as Collections and arrays don't mix very well.

EDIT

Vector is effectively deprecated. Use ArrayList instead.

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533492

You can use an ArrayList<Double> however you may find that TDoubleArrayList is more efficient as this wraps a double[]

Upvotes: 5

Related Questions