Take the 2-minute tour ×

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).

share|improve this question
why you are looking for vector, are you working on synchronised objects? –  developer Oct 5 '11 at 12:57
Yes, you can certainly have List<double[]> myList = new ArrayList<double[]>(); I don;t see anything inherently wrong with that. If you need a list of arrays, then you need a list of arrays –  Java Drinker Oct 5 '11 at 13:18
add comment

7 Answers

up vote 1 down vote accepted

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>>();
Vector<Double> doubles=new ArrayList<Double>();

and of course you can pass array object.

ArrayList<String[]> obj=new ArrayList<String[]>();
share|improve this answer
add comment

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

share|improve this answer
add comment

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

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

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


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


Vector is effectively deprecated. Use ArrayList instead.

share|improve this answer
I disagree about the mixing of Collections and arrays –  michael667 Oct 5 '11 at 12:59
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
add comment

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

share|improve this answer
add comment

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).

share|improve this answer
add comment

Your Answer


