Removing elements from a multidimensional array in Java

I am trying to remove elements 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 iterate through the original array, remove these elements from the list, then convert the list back to an array and return it.

I tried this, but got a type mismatch by returning to an array. I suspect that I did not do something with dimensions when converting from an array to a list.

Tip

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; } } 
+4
source share
4 answers

First, the following method does not do what you think:

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

Btw a good IDE will automatically change this for you:

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

So, when you do the following:

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

what happens is that the data [i] [j] [k] primitive is automatically assigned to the Float wrapper class, and then the delete methods check to see if it contains any element equal to your wrapped Float. It checks the entire list and does not find the Float, because ... You have a list float [] []. That way, basically your code will never remove an item from the list.

But in any case, you should know that if you find an element that matches the remove method, you will get an UnsupportedOperationException, because Arrays.asList just wraps your array, and your “list” still supports float [] [] [] . And you cannot resize arrays.

Then, in any case, you usually cannot compare floating point numbers using ==:

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

because it’s usually a terrible way to check if two floating numbers are equal. Floating numbers should be compared using epsilon (and you should always keep track of the spread of errors).

As for your last line, you can discard it in float [] [] [], but this is pointless because this list of float [] [] created in the first is supported by your original float [] [] [] and cannot be resized (well, you can resize individual elements, but you cannot resize it).

+2
source
 List points = Arrays.asList(data); 

Creates a list ( points ) of two-dimensional arrays, not the float that you need.

But the way you try to reach your requirement is impossible and logically invalid.

It looks like you need to turn your array into a list list. Which logically makes sense when you return it back to a three-dimensional array after deleting the elements, since it tracks the sizes.

+1
source

This is not because you are trying to return to a three-dimensional array.

You will need a loop through the array, filling in sizes 2 and 3, 1 on 1.

The whole idea seems too complicated because you need to know the dimensions after deleting the values, I think that you are probably best supporting the 3D array and removing the values ​​from it.

0
source

Multidimensional arrays are arrays of arrays. data - 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.

In addition, you cannot directly remove an item from the list returned by Arrays.asList() , since the list is directly supported by an array that cannot be modified. Instead, you should fill out an ArrayList , remove the elements, then convert back to an array and replace the original (see this question )

Finally, you are working with an array of primitives ( float s), you will need to convert from float[] to ArrayList<Float> and vice versa. This is not trivial; this question may help

0
source

Source: https://habr.com/ru/post/1386372/


All Articles