黑马程序员技术交流社区

标题: 关于Object类的使用,请大家帮忙指点 [打印本页]

作者: 孙志明    时间: 2015-12-15 23:25
标题: 关于Object类的使用,请大家帮忙指点
import java.util.*;
class ArrayListDemo{
        public static void show(Object obj){
                System.out.println(obj);
        }
        public static void main(String[] args){
                ArrayList al = new ArrayList();
                al.add("java01");
                al.add("java02");
                al.add("java03");
                al.add("java01");
                show(al);
               
        }
       
        public static ArrayList singleElement(ArrayList al){
                ArrayList newal = new ArrayList();
                Iterator it = al.iterator();
                while(it.hasNext()){
                        Objcet obj = it.next();
                        if(!(newal.contains(obj))){
                                newal.add(obj);
                        }
                }
                return newal;
        }
}

打印结果是:
错误: 找不到符号
Objcet obj = it.next();
^



作者: songjianzaina    时间: 2015-12-16 11:48
楼主的代码并没有调用ArrayList singleElement方法,修改如下:
  1. package com.sung.test;

  2. import java.util.*;
  3. class Test04{
  4.         public static void show(Object obj){
  5.                 System.out.println(obj);
  6.         }
  7.         public static void main(String[] args){
  8.                 ArrayList al = new ArrayList();
  9.                 al.add("java01");
  10.                 al.add("java02");
  11.                 al.add("java03");
  12.                 al.add("java01");
  13.                 ArrayList a2=singleElement(al);
  14.                 show(a2);
  15.         }
  16.         
  17.         public static ArrayList singleElement(ArrayList al){
  18.                 ArrayList newal = new ArrayList();
  19.                 Iterator it = al.iterator();
  20.                 while(it.hasNext()){
  21.                         Object obj = it.next();
  22.                         if(!(newal.contains(obj))){
  23.                                 newal.add(obj);
  24.                         }
  25.                 }
  26.                 return newal;
  27.         }
  28. }
复制代码
代码运行结果为:[java01, java02, java03]



作者: 孙志明    时间: 2015-12-16 16:25
真是谢了啊
作者: 孙志明    时间: 2015-12-16 16:26
谢了啊,忙活了半天没看出来,顶一哈
作者: hrfhwy    时间: 2015-12-16 20:16
你这个程序都没有调用singleElement(al)方法




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