1

I would like to convert an integer [] [] to a Vector<Vector<Double>>. After much reading, it seems no one has left a searchable post on the web for something of this nature. plenty of int vector to double vector, arraylist to vector, etc. Sadly I haven't found what i am looking for. So..Do any of you folks know an appropriate method for this? I was thinking of converting my int[][] to strings then convert that to vector<vector<Double>>. Opinions? Would something like this be useful, ie. converting my array to object array

Object[] a1d = { "Hello World", new Date(), Calendar.getInstance(), };
// Arrays.asList(Object[]) --> List
List l = Arrays.asList(a1d);

// Vector contstructor takes Collection
// List is a subclass of Collection
Vector v;
v = new Vector(l);

// Or, more simply:
v = new Vector(Arrays.asList(a1d));

Otherwise could you give me a better example that may have less steps? Thanks a Bunch again.

Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192

3 Answers3

2

First of all: avoid Vector, it is obsolete; use ArrayList instead (or something simmilar). Read more here

Secondly, if I had to convert a 2d array to a list of lists, I'd keep it very simple:

List<List<Double>> list = new ArrayList<ArrayList<Double>>();
for(int i=0; i<100; i++) //100 or whatever the size is..
{
        List<Double> tmp = new ArrayList<Double>();
        tmp = Arrays.asList( ... );
        list.add( tmp );
}

I hope I understood your problem right.

Community
  • 1
  • 1
emesx
  • 12,555
  • 10
  • 58
  • 91
1

Vector is an old class that is not deprecated but shouldn't be used anymore. Use ArrayList instead.

You should use the LIst interface rather than using the concrete Vector class. Program on interfaces, not on implementations.

Moreover, having repeating conversions like this shows a lack of design. Encapsulate your data into usable objects that don't need conversion each time you need a new functionality.

If you really need to do this: use loops:

int[][] array = ...;
List<List<Double>> outer = new Vector<List<Double>>();
for (int[] row : array) {
    List<Double> inner = new Vector<Double>();
    for (int i : row) {
        inner.add(Double.valueOf(i));
    }
    outer.add(inner);
}

Transforming from int to STring and then from String to Double is wasteful.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • You could improve the answer by mentioning that `Vector`, as an implementation of the `List` interface is preferable to `ArrayList` only if one needs a thread-safe implementation. – Kohányi Róbert Dec 04 '11 at 13:32
  • If I need a synchonized list, I prefer using Collections.synchronizedList(list). And this is most of the time not sufficient anyway to build a thread-safe program, because you often need compare-and-set operations anyway, needing explicit synchronization. – JB Nizet Dec 04 '11 at 13:35
  • I do see everyone's point in regards to vector, but the reason I asked to convert to vector is that the assignment asks for it, personally i don't see any point in completing another step since I have all the functionality that was required. On the other hand being forced to use this skeleton class to store my final int array out to, would force me to familiarize myself with super class structures and inheritance/polymorphism within java. thank you for the answers – Wuts.the.Martyr Dec 04 '11 at 23:01
0

A Vector is one dimensional. You could have a Vector of Vectors to simulate a 2D array:

    Vector v = new Vector();
    for (int i = 0; i < 100; i++) {
        v.add(new Vector());
    }

    //add something to a Vector
     ((Vector) v.get(50)).add("Hello, world!");

    //get it again
    String str = (String) ((Vector) v.get(50)).get(0);

Note: Vector is an old collection that is not recommended to be used

ziggy
  • 15,677
  • 67
  • 194
  • 287