[英]finding non repeating element.Code Does't print anything
我想将数组A中的唯一元素添加到列表L。此代码不显示任何内容。 请告诉我我要去哪里错了。
import java.util.*;
public class Unique {
public static void main(String[] args){
List<Integer> L = new LinkedList<>();
int[] A = {3,3,9,1,6,5,8,1,5,3};
int[] B = {3,3,9,1,6,5,8,1,5,3};
int count =0;
//outer loop
for(int i=0;i<A.length;i++){
//inner loop
for(int j=0;j<B.length;j++){
if(A[i]== B[j])count++;
}
if(count==1) L.add(A[i]);
}
for(int i =0;i<L.size();i++){
System.out.print(L.get(i)+" ");
}
}
}
在内部循环内将下面的行移动:
int count = 0;
您的最终代码将是:
// outer loop
for (int i = 0; i < A.length; i++) {
int count = 0;
// inner loop
for (int j = 0; j < B.length; j++) {
if (A[i] == B[j])
count++;
}
if (count == 1)
L.add(A[i]);
}
产量
9 6 8
另外,您也可以这样编写代码:
int count;
// outer loop
for (int i = 0; i < A.length; i++) {
count = 0;
// inner loop
for (int j = 0; j < B.length; j++) {
if (A[i] == B[j])
count++;
}
if (count == 1)
L.add(A[i]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.