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

3
  • why you are looking for vector, are you working on synchronised objects? Commented Oct 5, 2011 at 12:57
  • 1
    stackoverflow.com/questions/157944/… Commented Oct 5, 2011 at 13:11
  • 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 Commented Oct 5, 2011 at 13:18

7 Answers 7

5

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

Sign up to request clarification or add additional context in comments.

Comments

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[]>();

Comments

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.

1 Comment

I disagree about the mixing of Collections and arrays
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.

Comments

0

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

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.