黑马程序员技术交流社区

标题: java的一维数组 [打印本页]

作者: 15900400069    时间: 2016-4-27 23:41
标题: java的一维数组
给你一个一维数组,如何去除数组中相同的数,比如int[] arr={1,2,6,5,8,2,3,6,7},-----{1,2,6,5,8,,3,7}
作者: sunchuan    时间: 2016-4-28 18:21
这个应该可以的
作者: sunchuan    时间: 2016-4-28 18:23
class demo123 {
        public static void main(String[] args) {
                int[] arr = {1,2,3,5,2,4,6};
                toArray(arr);
                array(arr);
                //toArray(arr);
        }
       
        public static void toArray(int[] arr){
                for(int a=0; a<arr.length; a++){
                        System.out.print(arr[a]+",");
                }
                System.out.println();
        }
        public static void array(int[] arr){
                        int temp=0;
        for(int x=0; x<arr.length; x++){
                        for(int y=arr.length-1; y>x; y--){
                                if(arr[x] == arr[y]){
                                        temp = x;
                                       
                                        for(int i=0; i<arr.length; i++){
                                                if(i!=temp){
                                                        System.out.print(arr[i]+",");
                                                }
                                               
                                        }
                                }
                        }
        }
        }
}
作者: jy6728228    时间: 2016-4-28 18:23
用arr2,遍历arr往arr2里面加,如果arr2已经存在了就不加,最后,把arr=arr2 赋值就可以了




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2