Int [] [] convert --to & # 8594; Vector <Vector <Double>>

I would like to convert integer [] [] to Vector<Vector<Double>> . After long readings, it seems that no one has left a search post on the Internet for something like that. many int vector for double vector, arraylist to vector, etc. Unfortunately, I did not find what I was looking for. So ... Do any of you know a suitable method for this? I was thinking of converting my int[][] strings to strings and then converting them to Vector<Vector<Double>> . Opinions? Something like this would be helpful, i.e. converting an array to an array of objects

 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 can have fewer steps? Thanks again Bunch.

+1
source share
3 answers

Vector is an old class that is not deprecated, but should no longer be used. Use an ArrayList instead.

You should use the LIst interface and not use the specific Vector class. The program is on interfaces, not on implementations.

In addition, the repetition of such transformations shows a lack of design. Encapsulate your data in used objects that do not need to be converted every time you need 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); } 

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

+1
source

First of all: avoid Vector , it is out of date; use an ArrayList instead (or something similar). More here

Secondly, if I had to convert a 2d array to a list of lists, I would save it very simply:

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

+2
source

The vector is one-dimensional. You can 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 for use.

0
source

All Articles