2

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

developer
  • 9,116
  • 29
  • 91
  • 150
arjacsoh
  • 8,932
  • 28
  • 106
  • 166
  • why you are looking for vector, are you working on synchronised objects? – developer Oct 05 '11 at 12:57
  • 1
    http://stackoverflow.com/questions/157944/how-to-create-arraylist-arraylistt-from-array-t-in-java – mikey Oct 05 '11 at 13:11
  • Yes, you can certainly have `List myList = new ArrayList();` 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 05 '11 at 13:18

7 Answers7

5

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

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
3

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[]>();
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
1

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.

helpermethod
  • 59,493
  • 71
  • 188
  • 276
1

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.

John B
  • 32,493
  • 6
  • 77
  • 98
0

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

Jason S
  • 184,598
  • 164
  • 608
  • 970
0

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

michael667
  • 3,241
  • 24
  • 32
0

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

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215