A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

这个题目,是自己参看网友的,有的地方没看懂,请懂的童鞋给点建议,谢谢!
  1. import java.util.ArrayList;

  2. public class Test5 {
  3.     public static void main(String[] args) {
  4.             int[] arr = {4,2,4,6,1,2,4,7,8};
  5.             
  6.             zhuanhuan(arr);
  7. }
  8.     public static void zhuanhuan(int[] arr){
  9.                     //定义了两个容器,用于存储去重前后的数组;
  10.             ArrayList<Integer> list = new ArrayList<Integer>();
  11.             ArrayList<Integer> list1 = new ArrayList<Integer>();
  12.             for(int x = 0;x < arr.length;x++){
  13.            
  14.                     list.add(arr[x]);
  15.             }
  16.                    /*Iterator it = list.iterator();这里不能用迭代器了  因为不知道索引了
  17.                     while(it.hasNext()){
  18.                     
  19.                             if(!list1.contains(it.next())) {
  20.                             list1.add(it.next());
  21.                             }
  22.                     }
  23.                     */
  24.             for(Integer o: list){   
  25.             if(!list1.contains(o)){
  26.                  list1.add(o);
  27.                  }
  28.             }

  29.             System.out.println("原数组:"+list);
  30.             System.out.println("去重后的数组:"+list1);
  31.     }
  32. }
复制代码



评分

参与人数 1技术分 +1 收起 理由
敏敏好学 + 1

查看全部评分

13 个回复

倒序浏览
这是我写的一个方法,希望可以帮到你
class  Test
{
        public static void main(String[] args)
        {
                //创建数组.
                byte[] arr = {4,2,4,6,1,2,4,7,8};
                //调用方法.
                byte[] by = single(arr);
                for (int x=0;x<by.length ;x++ )
                {
                        //由于将byte变成字符,再由字符变成byte,在ASCII中对应的值加了48;
                        //所以要减去48,(对应的ASCII中字符是'0');
                        System.out.print(by[x]-'0');
                }
        }

        public static byte[] single(byte[] arr)
        {
                //定义一个容器
                StringBuilder sb = new StringBuilder();
                //遍历给定的数组
                for(int x=0;x<arr.length;x++)
                {
                        //将sb变成字符串
                        String string = sb.toString();
                        //将得到的数组元素变成字符串
                        String str = Byte.toString(arr[x]);
                        //如果sb所在的字符串不包含遍历到的元素,就往容器sb里面添加.
                        if(!(string.contains(str)))
                                sb.append(arr[x]);
                }
                //将容器中的元素变成String,再调用String类变成byte数组的方法
                byte[] by = sb.toString().getBytes();
                //返回得到的数组.
                return by;
        }
}

点评

不错  发表于 2015-7-19 09:50

评分

参与人数 1技术分 +1 收起 理由
敏敏好学 + 1

查看全部评分

回复 使用道具 举报 1 0
鲁行 发表于 2014-10-6 22:15
这是我写的一个方法,希望可以帮到你
class  Test
{

看了你的代码,方法还是挺不错的,:)想想是不是Set集合会更优点呢,
回复 使用道具 举报
坚持525 发表于 2014-10-7 15:02
看了你的代码,方法还是挺不错的,想想是不是Set集合会更优点呢,

既然出了这道题目,肯定是让你自己想一个方法去重复,如果直接用Set添加,那么就没有出这道题目的意义了.
回复 使用道具 举报
觉得越简单越好
回复 使用道具 举报
鲁行 发表于 2014-10-6 22:15
这是我写的一个方法,希望可以帮到你
class  Test
{

你这个方法不行哦,这不是最根本的方法,你是把所有数放到容器里然后判断是否有重复,而如果数组中出现十位数就全部会作为两个个位数来处理,我运行过了,比如把数组变成{4,2,4,10,1,2,4,7,10},那么你的程序出来的结果将是42107,
回复 使用道具 举报
学习 一下 顶
回复 使用道具 举报
不错。,。,。,。,,。,。,
回复 使用道具 举报
鲁行 发表于 2014-10-6 22:15
这是我写的一个方法,希望可以帮到你
class  Test
{

不太懂,刚学完多态,是不是没法用你的这种方法啊,还是我学的很渣
回复 使用道具 举报
还在学习中,觉得方法不错
回复 使用道具 举报
本帖最后由 崔判官 于 2015-11-5 19:11 编辑

public class Test {
                /**
                 * java程序运行入口
                 * jvm自动加载main方法
                 */
                public static void main(String[] args) {
                        
               
                    int[] arr = { 4, 2, 4, 6, 1, 2, 4, 7, 8 };
                    //创建对象
                    //这里没有泛型,若果有泛型则报错
                    ArrayList<Integer> data = new ArrayList<Integer>();
                    //增强for
                    for (int num : arr) {
                        if (!data.contains(num)) {
                            data.add(num);
                        }
                    }
                    //打印数组
                    System.out.println(data);
                }
        }
这个有点简单,可以看一下
回复 使用道具 举报
楼主,可以用迭代器的,
Iterator<Integer> it = list.iterator();
                while(it.hasNext()) {
                        Object ob = it.next();                                 //记录住每一个元素       
                        if(!newlist.contains(ob)) {
                                newlist.add((Integer)ob);                //类型从object转化成Integer
                        }
                }
回复 使用道具 举报
solomanlove 发表于 2016-5-22 22:35
楼主,可以用迭代器的,
Iterator it = list.iterator();
                while(it.hasNext()) {

因为增强for循环的底层用的是迭代器Iterator
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马