简体   繁体   中英

Removing items from a multi dimensional array in Java

I am trying to remove items from a 3 dimensional array.

I understand that one of the best ways to do this is to convert the array to a list and whilst iterating through the original array remove those items from the list then convert the list back to an array and return it.

I tried this but got an type mismatch when coming back to the array. I suspect I have not done something with the dimensions when converting from array to list.

Advice?

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

public class RepatitionRemoval {
    public float[][][] process(float[][][] data) {
        //this method with step through all of the the strokes and remove
        //points which occupy the same position. This should help with time
        //warping regconition

        //Change the array to a list
        List points = Arrays.asList(data);

        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                for (int k = 0; k < data[i][j].length-1; k++) {
                    //if the current coordinate is the same as the one next
                    //then remove it
                    if (data[i][j][k] == data[i][j][k+1]) {
                         points.remove(data[i][j][k]);
                    }

                }
            }
         }

         float[][][] returnData = points.toArray();

         return returnData;
    }
}

First the following method doesn't do what you think it does:

//Change the array to a list
List points = Arrays.asList(data);

Btw a good IDE will automatically transform that for you to:

List<float[][]> points = Arrays.asList(data);

So when you next do a:

points.remove(data[i][j][k])

what happens is that data[i][j][k] primitive gets auto-boxed to the Float wrapper class and then the remove methods checks to see if it contains any element equal to your wrapped Float . It checks the entire list and doesn't find any Float because... You've got a list of float[][] . So basically your code will never remove any element from the list.

But anyway, you should know that should you find an element that would match in the remove method, you'd get an UnsupportedOperationException because Arrays.asList simply wraps your array and your "list" is still backed the float[][][] array. And you cannot resize arrays.

Then, anyway you typically cannot compare floating point numbers using ==:

if (data[i][j][k] == data[i][j][k+1]) {

for it is ususally a terrible way to check if two floating numbers are equal. Floating numbers should be compared using an epsilon (and you should always keep track of the error propagation).

Regarding your last line, you can cast back to a float[][][] but it's pointless because that list of float[][] you created in the first is backed by your original float[][][] and cannot be modified (well, you may modify individual elements, but you cannot resize it).

List points = Arrays.asList(data);

It's creating list ( points ) of two dimensional arrays not float s which is what you need.

But the way you are trying to achieve your requirement is not possible and logically invalid.

It sounds like you have to turn your array into list of list of list. Which will logically makes sense when you turn it back into a three dimensional array after removing elements because keeps track of the dimensions.

Is it not because you are trying to push back into a three dimensional array.

You would need to cycle through your array populating the dimensions 2 and 3, 1 by 1.

The whole idea seems overly complex because you would need to know the dimensions after removing the values, I think you are probably best maintaining the 3d array and removing values from that.

Multi-dimensional arrays are arrays of arrays . data is an array of float[][] objects; data[i] is an array of float[] objects, and data[i][j] is an array of float objects. Instead of converting data to a list, you need to convert data[i][j] to a list.

Also, you can't directly remove an item from the list returned by Arrays.asList() , as the list is directly backed by the array, which cannot be resized. Instead, you have to populate an ArrayList , remove the elements, then convert back to an array and replace the original (see this question )

Finally, you're working with an array of primitives ( float s), you'll need to convert from float[] to ArrayList<Float> and back again. This isn't trivial; this question may help

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