简体   繁体   中英

Converting a List to Double[][]

Using below code I am trying to convert a List of List of Doubles to Double[][] . But on method : Double[][] dim = list1.toArray(new Double[2][2]); I receive this error :

Exception in thread "main" java.lang.ArrayStoreException
    at java.lang.System.arraycopy(Native Method)
    at java.util.ArrayList.toArray(ArrayList.java:361)
    at clustering.main.ConvertArrayTest.main(ConvertArrayTest.java:22)

The error occurs on this line:

Double[][] dim = list1.toArray(new Double[2][2]); 

How am I not converting the List correctly ?

The code :

import java.util.ArrayList;
import java.util.List;

public class ConvertArrayTest {

    public static void main(String args[]){

        List<ArrayList<Double>> list1 = new ArrayList<ArrayList<Double>>();
        ArrayList<Double> list2 = new ArrayList<Double>();

        list2.add(1.0);
        list2.add(1.0);
        list1.add(list2);

        list2 = new ArrayList<Double>();
        list2.add(2.0);
        list2.add(2.0);
        list1.add(list2);

        Double[][] dim = list1.toArray(new Double[2][2]);

    }

}

Because list1 is a list of lists, when you call toArray , you'll get an array of lists. You need to iterate through it, converting each inner list individually.

Here you go, fixed main method :

public static void main(String args[]){

        List<ArrayList<Double>> list1 = new ArrayList<ArrayList<Double>>();
        ArrayList<Double> list2 = new ArrayList<Double>();

        list2.add(1.0);
        list2.add(1.0);
        list1.add(list2);

        list2 = new ArrayList<Double>();
        list2.add(2.0);
        list2.add(2.0);
        list1.add(list2);

        Double[][] dim = new Double[2][2];
        int i = 0;
        for(ArrayList<Double> inner : list1)
            dim[i++] = inner.toArray(new Double[0]);
    }

Your first list is list of array lists so you need to iterate through it.

I would probably create a method like this -

public static Double[][] toDoubleArrayArray(
    List<ArrayList<Double>> al) {
  if (al == null) { // return null on null.
    return null;
  }
  Double[][] ret = new Double[al.size()][]; // declare the return array array.
  for (int i = 0; i < al.size(); i++) {
    ArrayList<Double> list = al.get(i); // get the inner list.
    if (list == null) { // handle null.
      ret[i] = null;
    } else {
      Double[] inner = new Double[list.size()]; // make the inner list an array.
      ret[i] = list.toArray(inner); // store that array.
    }
  }
  return ret; // return
}

And then I tested it like so

public static void main(String[] args) {
  List<ArrayList<Double>> list1 = new ArrayList<ArrayList<Double>>();
  list1.add(new ArrayList<Double>(Arrays.asList(1.0, 1.0)));
  list1.add(new ArrayList<Double>(Arrays.asList(2.0, 2.0)));

  Double[][] arr = toDoubleArrayArray(list1);
  for (int i = 0; i < arr.length; i++) {
    System.out.println(Arrays.toString(arr[i]));
  }
}

Which output the expected

[1.0, 1.0]
[2.0, 2.0]

Use this way:

Code :

public static void main(String[] args) {
    List<Double[]> list = new ArrayList<Double[]>();
    list.add(new Double[] { 1.0 });
    list.add(new Double[] { 2.0, 2.0 });
    list.add(new Double[] { 3.0, 3.0, 3.0 });
    Double[][] array = list.toArray(new Double[list.size()][]);
    for (Double[] numbers : array) {
        System.out.println(Arrays.toString(numbers));
    }
}

Output :

[1.0]
[2.0, 2.0]
[3.0, 3.0, 3.0]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM