- */
- //利用两个数组,一个数组存储原始数组,另个数组存储去重后的结果
- public class Test7 {
-
- public static void main(String[] args){
- int[] arr1 = new int[]{4,2,4,6,1,2,4,7,8};
- int[] arr2 = new int[arr1.length];
- System.out.print("Array{");
- //打印旧数组
- for(int i=0;i<arr1.length;i++){
- System.out.print(arr1[i]+",");
- }
- System.out.print("}\n");
-
- int index = 0;
- boolean flag = true;
- //
- for(int i = 0;i < arr1.length;i++){
- for(int j = 0;j < index+1;j++){
- if(arr1[i] == arr2[j]){
- flag = false;
- }
- }
- //flag为真给arr2赋值
- if(flag){
- arr2[index]=arr1[i];
- index++;
- }
- flag=true;
- }
-
- System.out.print("newArray:{");
-
- for(int i=0;i<index;i++){
- System.out.print(arr2[i]+",");
- }
- System.out.print("}");
- }
- }
复制代码 |