1

I have one question.

In Java I have an ArrayList containing an ArrayList with double-values. From this I want to make an double[][] array.

I know how to make it with an 1D- Array via the method 'toArray' but in this case I'm not sure how to do ist and always get an error message in my code.

My Aktuell Code is:

double[][] test = new double[Data.getArrayList().size()][Data.getArrayList().size()];

double[][] array = Data.getArrayList().toArray(test);

Where Data is my ArrayList of ArrayLists.

2
  • show us the declaration of Data. If it were an ArrayList than Data.getArrayList() would be a compiler error. Commented Dec 17, 2012 at 16:01
  • Oh. My bad. Data is a class I designet by myself. with the Method Data.getArrayList I get out the data as ArrayList<ArrayList<Double>>. But this shouldn't be the Problem. Commented Dec 18, 2012 at 10:29

3 Answers 3

1

I would use a method like this (put it in an Utility class)

public static double[][] to2DArray(List<List<Double>> input) {
    double[][] output = new double[input.size()][];
    for (int i = 0; i < input.size(); i++) {
        output[i] = new double[input.get(i).size()];
        for (int j = 0; j < input.get(i).size(); j++) {
            output[i][j] = input.get(i).get(j);
        }
    }
    return output;
}
Sign up to request clarification or add additional context in comments.

Comments

1

You have to

  1. iterate the outer ArrayList
  2. convert each entry to an array using toArray
  3. add these to an array of arrays, one by one

It's not beautiful, but there isn't really a better way to do it.

Comments

0

This is probably faster than iterating through every single element in the matrix:

private Double[][] to2DArray(ArrayList<ArrayList<Double>> data)
{
    Double[][] result = new Double[data.size()][];
    Double[] temp;
    int mainIdx = 0;
    for (List<Double> arrayOfDouble : data)
    {
        temp = new Double[arrayOfDouble.size()];
        result[mainIdx++] = arrayOfDouble.toArray(temp);
    }
    return result;
}

8 Comments

@LuisRamos you are creating a Double[][], OP asked for a double[][]. No it's not faster, because you are also iterating over every single element in the matrix. You're just not doing it yourself, you let toArray() do it for you. You can see it iterating over every element in it's implementation.
Yeah, That was my Problem now when I tryed to use the code. Is there a way to use this with double[][] as output? But in Priniciple I'm would say Luis Ramos is right. Its always nicer to not use a loop in a loop and I think that the toArray-Order is better because I experianced, that build-in-methodes are alway faster than making it by yourself.
@HansDampf a way to get double[][] would be to use my answer. This answer also contains a loop in a loop, because the implementation of toArray() contains a loop. If you compare the source of toArray() to my solution you will see that toArray() is neither faster nor slower in your case. But you are right, we shouldn't reinvent the wheel, if not necessary.
@jlordo Thank you. In principle I use you methode by now, because as you said it geives me th right type. And when is faster: better! :-) I think with two loops its ok. But when you have more than 2 loop there is also an an aesthetical argument. Because with to many loops in loops I find it realy hard to read a source code.
I would prefer my answer if it was correct (returning double[][] instead of Double[][]), because it has less lines of code, not reinventing the wheel, no single-letter variables and no loops inside loops. But it is incorrect :-) To unbox the Doubles I would need something else, not toArray. I would use @jlordo's answer for double[][] and mine for Double[][].
|

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.