简体   繁体   English

在Java中从多维数组中删除项目

[英]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: 顺便说一句好的IDE会自动将其转换为:

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 . 会发生什么是data [i] [j] [k] primitive被自动装箱到Float包装器类,然后remove方法检查它是否包含任何等于你的包裹Float的元素。 It checks the entire list and doesn't find any Float because... You've got a list of float[][] . 它会检查整个列表并且找不到任何Float因为...你有一个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. 但无论如何,你应该知道如果你找到一个在remove方法中匹配的元素,你会得到一个UnsupportedOperationException,因为Arrays.asList只是包装你的数组而你的“list”仍然支持float [] [] []阵列。 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). 应使用epsilon比较浮动数字(您应始终跟踪错误传播)。

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). 关于你的最后一行,你可以回转到float [] [] []但是它没有意义,因为你在第一行中创建的float [] []列表由原始的float [] [] []支持 ,不能修改(好吧,你可以修改单个元素,但不能调整它的大小)。

List points = Arrays.asList(data);

It's creating list ( points ) of two dimensional arrays not float s which is what you need. 它正在创建二维数组的列表( points )而不是float ,这正是您所需要的。

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. 您需要在数组中循环填充尺寸2和3,1 x。

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. 整个想法似乎过于复杂,因为您需要在删除值后知道维度,我认为您可能最好维护3d数组并从中删除值。

Multi-dimensional arrays are arrays of arrays . 多维数组是数组的数组 data is an array of float[][] objects; datafloat[][]对象的数组; data[i] is an array of float[] objects, and data[i][j] is an array of float objects. data[i]float[]对象的数组, data[i][j]float对象的数组。 Instead of converting data to a list, you need to convert data[i][j] to a list. 您需要将data[i][j]转换为列表,而不是将data转换为列表。

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. 此外,您无法直接从Arrays.asList()返回的列表中删除项目,因为列表由数组直接支持,无法调整大小。 Instead, you have to populate an ArrayList , remove the elements, then convert back to an array and replace the original (see this question ) 相反,您必须填充ArrayList ,删除元素,然后转换回数组并替换原始(请参阅此问题

Finally, you're working with an array of primitives ( float s), you'll need to convert from float[] to ArrayList<Float> and back again. 最后,您正在使用一组基元( float s),您需要将float[]转换为ArrayList<Float>并再次返回。 This isn't trivial; 这不是微不足道的; this question may help 这个问题可能有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM