-3

How do you set an ArrayList of integers[] equal to an ArrayList of doubles[]?

I tried a lot of different things but none of them worked. I'm sure there is a really simple answer to this problem but I could not find it on google thanks.

Im coding in Java

I tried this

drawablerect =  (ArrayList int[]) rect;

And setting each array in drawables to each array in rect.

drawablerect is an ArrayList of int[] and rect is a ArrayList of double[]

This is what I did

for(int i = 0; i < rect.size(); i++)
       for(int index = 0; index < 7; index++)
                 drawablerect.get(i)[index] = (int) rect.get(i)[index];
4

2 回答 2

1
  1. Create an integer of the appropriate size
  2. Iterate over the ArrayList
  3. Assign each value to the array
于 2012-11-27T12:27:33.567 回答
0

One possible solution:

// Beware of precision loss (0.99999 --> 0)
public static List<int[]> toListIntArray(List<double[]> input) {
    List<int[]> output = new ArrayList<>(input.size());
    for (double[] in : input) {
        int[] ints = new int[in.length];
        for (int i = 0; i < in.length; i++) {
            ints[i] = (int) in[i];
        }
        output.add(ints);
    }
    return output;
}

You can use it like this:

drawablerect = toListIntArray(rect);
于 2012-11-27T12:44:30.963 回答