[Java] 纯文本查看 复制代码
public class Demo01 {
public static void main(String[] args) {
int[] arr={6,2,9,6,1,2,6,7,8};
int[] brr=selectArr(arr);
for (int i = 0; i < brr.length; i++) {
System.out.println(brr[i]);
}
}
public static int[] selectArr(int[] arr){
int[] brr=new int[arr.length];
int count=0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < brr.length; j++) {
//如果arr[i]和brr中新添加的元素挨个比较比较
if(j<count&&arr[i]!=brr[j]){
continue;
}
if(j<count&&arr[i]==brr[j]){
break;
}
//如果都不相同brr添加新的元素arr[i]
brr[count]=arr[i];
count++;
break;
}
}
int[] crr=new int[count];
for (int i = 0; i < crr.length; i++) {
crr[i]=brr[i];
}
return crr;
}
}
|