Majda Benaomar
Majda Benaomar

Reputation: 3

Remove elements from 2D array

I want to remove elements that are in routedClients from array, so I converted it to an ArrayList, then used remove, finally I converted it back to double[][] array. But when I execute it, it gives me this message about this line:

double[][] remainingStockout = (double[][]) stockout.toArray();

The error is:

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [[Ljava.lang.Double;

Any help would be really appreciated. :)

public double[][] removeSite(double[][] array) {

    List<double[]> stockout = new ArrayList<double[]>(Arrays.asList(array));

    for (int i = 0; i < array.length; i++) {
        for (int j = 0; j < routedClients.size(); j++) {

            if (array[i][0] == routedClients.get(j)) {

                stockout.remove(i);
            }
        }
    }

    double[][] remainingStockout = (double[][]) stockout.toArray();

    return remainingStockout;

}

Upvotes: 0

Views: 8480

Answers (4)

Harry
Harry

Reputation: 46

The below appears to work

double[][] remainingStockout = (double[][]) stockout.toArray(new double[][]{});

Full class for testing:

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

public class Test {
    static ArrayList<Double> routedClients = new ArrayList<Double>();

    public static void main(String[] args) {
        double[][] arr1 = { { 2, 4, 6 }, { 3, 6, 9 }, { 5, 10, 15 } };
        routedClients.add(new Double(1));
        routedClients.add(new Double(2));
        routedClients.add(new Double(3));

        print(arr1);
        double[][] arr2 = removeSite(arr1);
        print(arr2);
    }

    private static void print(double[][] arr1) {
        for (int i = 0; i < arr1.length; i++) {
            double[] arr2 = arr1[i];
            for (int j = 0; j < arr2.length; j++) {
                System.out.println("arr1[" + i + "][" + j + "] = " + arr1[i][j]);
            }
        }
    }


    public static double[][] removeSite(double[][] array) {

        List<double[]> stockout = new ArrayList<double[]>(Arrays.asList(array));
        System.out.println("length before = " + stockout.size());

        for (int i = array.length-1; i >= 0; i--) {
            for (int j = 0; j < routedClients.size(); j++) {
                if (array[i][0] == routedClients.get(j)) {
                    System.out.println("removing " + routedClients.get(j));
                    stockout.remove(i);
                }
            }
        }
        double[][] remainingStockout = (double[][]) stockout.toArray(new double[][] {});
        System.out.println("length after = " + remainingStockout.length);
        return remainingStockout;
    }

}

Here is the output

arr1[0][0] = 2.0
arr1[0][1] = 4.0
arr1[0][2] = 6.0
arr1[1][0] = 3.0
arr1[1][1] = 6.0
arr1[1][2] = 9.0
arr1[2][0] = 5.0
arr1[2][1] = 10.0
arr1[2][2] = 15.0
length before = 3
removing 3.0
removing 2.0
length after = 1
arr1[0][0] = 5.0
arr1[0][1] = 10.0
arr1[0][2] = 15.0

Upvotes: 3

Rohit
Rohit

Reputation: 89

you can use the overloaded method of toArray() .. like this -

double[][] remainingStockout = new double[array.length][array.length];

    stockout.toArray(remainingStockout);

Upvotes: 0

Felipe Centeno
Felipe Centeno

Reputation: 3747

You can cast a double array into a list array because the compiler just makes a list with all the elements, but the compiler can't know which size to make a 2D array.

Maybe you could try creating a list of lists. You would need to traverse your array two more times though. One for assigning the values to the nested list and then to assign them again to the double array before returning the value.

Upvotes: 0

Pritam Banerjee
Pritam Banerjee

Reputation: 18933

You are trying to cast an object into an array.

This cannot be done.

Instead you will need to convert each element in the array and then need to add it to they the 2D Array.

The following line will never work:

double[][] remainingStockout = (double[][]) stockout.toArray();

Upvotes: 1

Related Questions