Java 如何在第三个数组中复制一个数组中但不在另一个数组中的元素
我有两个数组,其中填充了随机的int值。我想做的是将其中一个数组中的元素,而不是另一个数组中的元素放在第三个数组中。我怎么做Java 如何在第三个数组中复制一个数组中但不在另一个数组中的元素,java,Java,我有两个数组,其中填充了随机的int值。我想做的是将其中一个数组中的元素,而不是另一个数组中的元素放在第三个数组中。我怎么做 int[] array1 = new int[10]; int[] array2 = new int[10]; for(int j=0;j<array2.length;j++){ int alea = rant.nextInt(10); valorpos = alea; array1[j] = rant.nextInt(10); a
int[] array1 = new int[10];
int[] array2 = new int[10];
for(int j=0;j<array2.length;j++){
int alea = rant.nextInt(10);
valorpos = alea;
array1[j] = rant.nextInt(10);
array2[j] = rant.nextInt(10);
}
最直接的方法是使用集合来实现这一点。从第一个数组创建一个列表,从第二个数组创建一个集合。然后从第一个集合中删除第二个集合的所有元素。最后,生成一个数组。大致如下:
List<Integer> list1 = Arrays.stream(array1).boxed().collect(Collectors.toList());
Set<Integer> set2 = Arrays.stream(array2).boxed().collect(Collectors.toSet());
list1.removeAll(set2);
Integer[] array3 = new Integer[list1.size()];
list1.toArray(array3);
List list1=Arrays.stream(array1.boxed().collect(Collectors.toList());
Set set2=Arrays.stream(array2.boxed().collect(Collectors.toSet());
清单1.移除所有(set2);
整数[]数组3=新整数[list1.size()];
清单1.toArray(阵列3);
您可以尝试嵌套for循环吗?这不是最有效的方法,但足够简单满足您的需要
boolean contain;
//loop through the first array
for(int number1 : array1){
//set contain to false
contain = false;
//loop through the second array
for(int number2 : array2){
//if number1 in first array equals number2 in second array
if(number1 == number2){
//set contain to true and break the loop
contain = true;
break;
}
}
//if contain is still false
if(!contain){
//add number1 to third array...
}
}
这里最好的办法就是退一步,想想你想解决什么问题。 有两个数组,每个数组中都有随机数,假设它们类似于: 数组A: 1,3,4,5,6 数组B: 4,9,4,5,6 因此,我们需要找到数组A中不在数组B中的值。你会怎样在纸上写出来?您可以从数组A的第一个数字开始,扫描数组B中的每个数字,检查它是否在其中,如果是,我们停止查找,否则我们继续到最后一个数字。因此,对于上述示例,我们会发现: 数组结果: 1,3 因此,让我们将其转换为一些代码:
// a list which will be used to put the unique values in
List<Integer> result = new ArrayList<>();
// loop through each number in the first array
for(int i=0; i< arrayOne.length; i++){
boolean isInOtherArray = false;
// check this number against each number in the second array
for(int j=0; j<arrayTwo.length; j++){
// if the number matches, then we know its not unique
// so we stop the search
if(arrayOne[i] == arrayTwo[j]){
isInOtherArray=true;
break;
}
}
// if its not in the other array we know its unique so it goes
// in our result list
if(!isInOtherArray){
result.add(arrayOne[i]);
}
}
//用于将唯一值放入的列表
列表结果=新建ArrayList();
//循环遍历第一个数组中的每个数字
for(int i=0;i 对于(int j=0;j尝试两个for循环。第一个循环通过第一个数组,然后考虑每个当前元素,然后循环通过第二个数组来检查<代码>当前的< /代码>编号是否从<代码> ARARY1在<代码> ARARY2中。如果它不存在于<代码> ARARY2中,则将其添加到<代码> ARARY3。最后我添加了一块COD。e将阵列3
修剪至尺寸
int[] array1=new int[10];
int[] array2=new int[10];
int[] array3=new int[10];
// populate array1 and array2 ....
boolean isInArray2;
int current;
int counter = 0;
for (int i = 0; i < array1.length; i++) {
current = array1[i];
isInArray2= false;
for (int j = 0; j < array2.length; j++) {
if(array2[j] == current) {
isInArray2= true;
break;
}
}
if(!isInArray2) {
// not in array2 -> must add in array3
array3[counter] = current;
counter++;
}
}
// Trim array3 to avoid null values:
int[] tmp = array3;
array3=new int[counter];
for (int i = 0; i < array3.length; i++) {
array3[i] =tmp[i];
}
int[]array1=新的int[10];
int[]array2=新int[10];
int[]数组3=新int[10];
//填充阵列1和阵列2。。。。
布尔isInArray2;
电流;
int计数器=0;
for(int i=0;i必须在阵列3中添加
阵列3[计数器]=电流;
计数器++;
}
}
//修剪阵列3以避免空值:
int[]tmp=array3;
array3=新整数[计数器];
for(int i=0;i
我尝试在两个数组和if(array1[i]!=array2[j]){thirdarray[i]==array[i]之间使用双循环;以及类似的内容请移到您的问题。注释中的代码不可读。是否您的问题基本上是在问,如果数字1在数组1中,但不在数组2中,请将其放在数组3中?此人无法理解此代码的任何一行。@burrito77答案中对代码进行了完整的描述。当然,对您来说,它是完美的这是有道理的,但对于一个可能不知道集合或列表是什么的人来说,这肯定会让他们感到困惑,因为他们不知道这一切是如何发生的。如果他们甚至不知道如何找出两个数字是否不同,如果是这样的话,将它们放入一个新的数组中,那么他们在理解这一点之前还有很长的路要走。我说的是AP Java和我们的cl驴子在1.25个学期后都还没有做到这一点!@burrito77这是一个正确且相当有效的答案。如果它需要一些努力才能理解它,我完全可以。大多数好的答案都可以。哈哈,很好的编辑你没有在编译器或其他东西中编写这篇文章吗?恐怕这并不能回答问题。你的代码没有真正测试数字ich“在一个数组中,但不在另一个数组中”。这就是问题的关键所在。@GriffinBender很好,你可能只需要否定条件,这应该可以解决问题,对吧?@burrito77number1!=number2
不会解决任何问题。这不是一个有效的条件“number1
在array2
中不存在。它只检查array2
是否有某些元素与number1
不同@GriffinBender更好。您也可以在contain=true;
之后立即中断循环,无需循环到结尾。缺少列表。toArray()?当然可以补充,但我回答的重点是帮助用户解决@burrito77问题背后的逻辑
int[] array1=new int[10];
int[] array2=new int[10];
int[] array3=new int[10];
// populate array1 and array2 ....
boolean isInArray2;
int current;
int counter = 0;
for (int i = 0; i < array1.length; i++) {
current = array1[i];
isInArray2= false;
for (int j = 0; j < array2.length; j++) {
if(array2[j] == current) {
isInArray2= true;
break;
}
}
if(!isInArray2) {
// not in array2 -> must add in array3
array3[counter] = current;
counter++;
}
}
// Trim array3 to avoid null values:
int[] tmp = array3;
array3=new int[counter];
for (int i = 0; i < array3.length; i++) {
array3[i] =tmp[i];
}