Collection<E>,Iterator<E>,还有,别写import java.lang.*;这句,这个包是默认导入的!
解决方案 »
- 打包成JAR后不能找到图片
- 请问在java applet 中怎么接收和处理windows消息?
- 谁有win32平台用的tar、compress、uncompress执行文件?小弟叩谢!
- 解析xml的问题
- 高分请教!!!!!!!!!!!!!!!!!!!
- 把Checkboxgroup改成可复选的Checkbox
- 高分求win2000下邮件服务器(基于javamail的)!!!!!!!!!!!11
- ObjectInputStream中有没有像RandomAccessFile的Seek()函数??
- 偶快疯了!!!!weblogic和SQLServer为和不行?????
- Exception occurred during event dispatching违例该怎么处理呀?
- swing
- 大家帮忙看一个正则表达式
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;
}
}
这好闲着,帮你改下,这样就不会报那个警告了
public static void printElements(Collection<E> c)//通用的,只要集合类,都可以用这个参数
{
Iterator it=c.iterator<E>();
while(it.hasNext())
{
System.out.println("排序的结果是: "+it.next());
}
}
现在提示:
E:\教程\java\java教程\JavaLesson\Lesson6>java
ArrayListTest.java:6: 错误: 非法的表达式开始
Iterator it=c.iterator<E>();