大家好:
我看了个教程里面的java代码如下:
import java.util.*;
import java.lang.*;
class ArrayListTest
{
public static void printElements(Collection c)//通用的,只要集合类,都可以用这个参数
{
Iterator it=c.iterator();
while(it.hasNext())
{
System.out.println("排序的结果是: "+it.next());
}
}
public static void main(String[] args)
{
Student s1=new Student(2,"zhangsan");
Student s2=new Student(1,"lisi");
Student s3=new Student(3,"wangwu");
ArrayList<Object> al=new ArrayList<Object>();
//ArrayList<Student> al=new ArrayList<Student>();
al.add(s1);
al.add(s2);
al.add(s3);
//List l=(List)al;
//Collections.sort(al,null);//参数是要排序的列表和比较器(比较器也可以用null指按自然排序)这个两个参数
Collections.sort(al,new Student.StudentComparator());
printElements(al);
}
}
class Point
{
int x,y;
Point(int x,int y)
{
this.x=x;
this.y=y;
}
public String toString()
{
return "x="+x+",y="+y;
}
}
class Student implements Comparable
{
int num;
String name;
static class StudentComparator implements Comparator//声明为静态类,不用实例化这个内部类了
{
public int compare(Object o1,Object o2)
{
Student s1=(Student)o1;
Student s2=(Student)o2;
//return s1.num>s2.num ? 1 :(s1.num==s2.num ? 0 : -1);
int result=s1.num>s2.num ? 1 :(s1.num==s2.num ? 0 : -1);
if(result==0)
{
result=s1.name.compareTo(s2.name);
}
return result;
}
}
Student(int num,String name)
{
this.num=num;
this.name=name;
}
public int compareTo(Object o)//compareTo方法是lang包中的方法
{
Student s=(Student)o;//把Object类型的O转换成Student类型的
return num>s.num ? 1:(num==s.num ? 0 :-1);//二元条件表达式
}
public String toString()//重写toString方法
{
return num+": :+"+name;
}
}
在用javac ArrayListTest.java编译的时候出现了
注: ArrayListTest.java使用了未经检查或不安全的操作。
注: 有关详细信息, 请使用 -Xlint:unchecked 重新编译。
提示,请问要怎么写才能不提示并进行编译?谢谢!
------解决方案--------------------
import java.util.*;
class ArrayListTest {
public static void printElements(Collection<Student> c)// 通用的,只要集合类,都可以用这个参数
{
Iterator<Student> it = c.iterator();
while (it.hasNext()) {
System.out.println("排序的结果是: " + it.next());
}
}
public static void main(String[] args) {
Student s1 = new Student(2, "zhangsan");
Student s2 = new Student(1, "lisi");
Student s3 = new Student(3, "wangwu");
ArrayList<Student> al = new ArrayList<Student>();
// ArrayList<Student> al=new ArrayList<Student>();
al.add(s1);
al.add(s2);
al.add(s3);
// List l=(List)al;
// Collections.sort(al,null);//参数是要排序的列表和比较器(比较器也可以用null指按自然排序)这个两个参数
Collections.sort(al, new Student.StudentComparator());
printElements(al);
}
}
class Point {
int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return "x=" + x + ",y=" + y;
}
}
class Student implements Comparable<Student> {
int num;
String name;
static class StudentComparator implements Comparator<Student>// 声明为静态类,不用实例化这个内部类了
{
public int compare(Student o1, Student o2) {
Student s1 = o1;
Student s2 = o2;
// return s1.num>s2.num ? 1 :(s1.num==s2.num ? 0 : -1);
int result = s1.num > s2.num ? 1 : (s1.num == s2.num ? 0 : -1);
if (result == 0) {
result = s1.name.compareTo(s2.name);
}
return result;
}
}
Student(int num, String name) {
this.num = num;
this.name = name;
}
public int compareTo(Student o)// compareTo方法是lang包中的方法
{
Student s = o;// 把Object类型的O转换成Student类型的
return num > s.num ? 1 : (num == s.num ? 0 : -1);// 二元条件表达式
}
public String toString()// 重写toString方法
{
return num + ": :+" + name;
}
}
这好闲着,帮你改下,这样就不会报那个警告了
------解决方案--------------------