各位大侠:40分求解一个小问题,望各位救我,十分感谢!

tjblyy 2005-09-11 10:29:02
import java.util.*;
class ArrayListTest
{
public static void main(String args[])
{
ArrayList al=new ArrayList();
al.add("a");
al.add("b");
System.out.println(al);
}
}

javac编译报警告: [unchecked] 对作为普通类型 java.util.ArrayList 的成员的 add(E) 的调用未经检查, al.add(“a”);
...全文
78 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
tjblyy 2005-09-11
  • 打赏
  • 举报
回复
非常感谢楼上的这位大哥,请问你学了有多久了?
believefym 2005-09-11
  • 打赏
  • 举报
回复
你的StringTest类没有定义compare的接口,Collections的sort当然排不了序了
说白了就是你的al存的是StringTest的对象,我怎么知道按什么顺序给你这么多的StringTest对象排序
zsjin0208 2005-09-11
  • 打赏
  • 举报
回复
我的jdk是1.5的,没问题啊
tjblyy 2005-09-11
  • 打赏
  • 举报
回复
import java.util.*;
import java.lang.*;

class ArrayListTest
{
public static void printElements(Collection c)
{
Iterator it=c.iterator(); //所有的collections都有Iterator,但是map接口除外。
while(it.hasNext())
{
System.out.println(it.next());
}
}
public static void main(String []args)
{
ArrayList<StringTest> al=new ArrayList<StringTest>(20);
StringTest s1=new StringTest("Tian");
StringTest s2=new StringTest("Jiang");
StringTest s3=new StringTest("bo");
al.add(s1);
al.add(s2);
al.add(s3);
for(int i=0;i<=al.size();i++)
{
System.out.println(al.get(i));
}
//printElements(a1);
Collections.sort(al);
Collections.sort(al,Collections.reverseOrder());
Object[] objs=al.toArray();
for(int i=0;i<objs.length;i++)
{
System.out.println(objs[i]);
}

List l=Arrays.asList(objs);
System.out.println(l);

}
}

//////////////////////////////////////////////////
class StringTest
{
public String s;
StringTest(String s)
{
this.s=s;
}
}

////////////////////////////////////////////////////
class Point
{
public int x,y;
Point(int x,int y)
{
this.x=x;
this.y=y;
}

public String toString()
{
return "x="+x+","+"y="+y;
}

}

///////////////////////////////////////////////
class students implements Comparable
{
int num;
String name;
students(int num,String name)
{
this.num=num;
this.name=name;
}

static class studentscomparator implements Comparator
{
public int compare(Object o1,Object o2)
{
students s1=(students)o1;
students s2=(students)o2;
int result=s1.num>s2.num?1:(s1.num==s2.num?0:-1);
if(result==0)
{
result=s1.name.compareTo(s2.name);
}
return result;
}
}

public int compareTo(Object o)
{
students s=(students)o;
return num > s.num ? 1 : (num==s.num ? 0 : -1);
}

public String toString()
{
return "num="+num+","+"name="+name;
}
}
interhanchi 2005-09-11
  • 打赏
  • 举报
回复
^_^,应该下一个1.5的文档.

Collections.sort(al);

这个也报错:ArrayListTest.java:29: 找不到符号,

Collections.sort(al);


你的这个没有错误,把你的整个代码帖出来!
believefym 2005-09-11
  • 打赏
  • 举报
回复
把代码贴全了看看

要下api就下1.5的好了,反正你用1.5的jdk
tjblyy 2005-09-11
  • 打赏
  • 举报
回复
Collections.sort(al);

这个也报错:ArrayListTest.java:29: 找不到符号,

Collections.sort(al);
^
楼上的这位大侠,还能不能帮忙解决一下,不胜感激!对了,我用的是jdk1.5,是不是该下载1.5的文档了?
believefym 2005-09-11
  • 打赏
  • 举报
回复
楼主用的jdk应该是1.5,
ArrayList al=new ArrayList();改成
ArrayList<String> al=new ArrayList<String>();

(尖括号内是Collection要存的类型)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧