从另一个数组中删除一个数组中的值

图8

有谁知道我可以不使用ArrayList而从另一个数组中删除值?我认为以前已经有人问过这个问题,但是我只能使用ArrayList找到解决方案。

例如

int[] set01 = {1, 2, 3, 4, 5, 6};
int[] set02 = {1, 2};
int[] set03;

//function to remove set02 from set01, yielding set03
//set03 = {3, 4, 5, 6}

实际脚本的摘录:

//just a placeholder, length of 'ptSet' is variable
//these are point coordinates {x, y} - the actual values don't matter
float[][] ptSet = new float[100][2];

//populate ptSet with 100 points

//'set02' of original question
int[] cchOrdered = {1, 12, 22, 32, 54};

//'set03' of original question
int[] eligiblePts = new int[0];

//compare 'cchPts' with 'ptSet'
for (int j = 0; j < ptSet.length; j++) {
  for (int k = 0; k < cchOrdered.length; k++) {
    //this doesn't work, as the nested loop invalidates any duplicate checks
    if (!(cchOrdered[k] == j)){
      eligiblePts = (int[]) append(eligiblePts, j);
    }
  }
}

再次,对不起,如果我的代码不够干净...仍在这里学习:S

米诺利一切

最好的解决方案是使用ArrayList。由于您不想使用它,因此可以执行以下操作。

import java.util.Arrays;
import org.apache.commons.lang3.ArrayUtils;

public class MyClass {
    public static void main(String args[]) {
        int[] set01 = {1, 2, 3, 4, 5, 6};
        int[] set02 = {1, 2};

        //assume set01 having more elements than set02 array 
        int[] set03 = Arrays.copyOf(set01, set01.length);            

       // sorting array is optional if you know the arrays are sort.
        Arrays.sort(set01);
        Arrays.sort(set02);

        for(int value :set02){
            set03 = ArrayUtils.removeElement(set03, value);          
        }

        for(int value:set03){
           System.out.println("set03 array :"+ value);

        }      
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从另一个数组中删除另一个数组中的值的最快方法

从另一个数组中删除一个数组的内容

通过另一个数组中的值过滤一个数组?

使用另一个数组中的值过滤一个数组

删除另一个数组中包含的一维数组中的值

删除另一个数组中的元素

根据另一个数组中的值删除嵌套对象数组中的值

打印一个数组中的 JSON 值,这个数组在另一个数组中

删除另一个数组的数组值

根据另一个数组中的值从数组中删除对象

使用php中的另一个数组从数组中删除公共值

如何从另一个数组中的一个数组中选择一个值?

按值删除另一个数组中包含的数组对象

如何基于另一个数组删除或屏蔽numpy数组中的值

Rapidjson,在另一个数组的数组中获取一个值

使用另一个数组中的值返回一个数组中的数组

将一个数组列表中的值引用到另一个数组中的值

根据另一个数组中的值替换一个数组中的值

php-将一个数组中的值与另一个数组中的值互斥

将一个数组中的值分配给另一个数组中的值

将一个数组中的值与另一个数组中的值合并/覆盖

将一个数组中的某些值与另一个数组中的值组合

从一个数组中删除另一个数组中的元素

删除一个数组中的项目,该项目在另一个数组中成角度?

如何在php中从另一个数组中删除一个数组?

从一个数组中删除存在于另一个数组中的项目?

将一个数组(键)中的对象数组变成另一个数组(值)

另一个数组中的数组

javascript:如果一个值在另一个数组中,则只删除一次