/************Student.java begin***************/
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Student {
private String name;
private String password;
public String getName() {
return name;
}
public String getPassword() {
return password;
}
public void setName(String name) {
this.name = name;
}
public void setPassword(String password) {
this.password = password;
}
public Student(String name, String password) {
super();
this.name = name;
this.password = password;
}
public Student(){}
public List show(String name,String password){
ArrayList list=new ArrayList();
return list;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "name= "+name+" password= "+password;
}
public static void main(String args[]){
List list=new ArrayList();
//向list中添加对象
list.add(new Student("xsxs","xsxs"));
list.add(new Student("mingkun","ambow"));
list.add(new Student("frfr","frfr"));
list.add(new Student("bgbg","frfr"));
list.add(new Student("cdcd","cdcd"));
// Iterator 这个是迭代器
Iterator it=list.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
/************Student.java end***************/
/************TestArrayList.java begin***************/
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class TestArrayList {
public static void main(String args[]){
List list=new ArrayList();
list.add(new Integer(43));
list.add(54);
list.add(32);
list.add(76);
list.add(31);
list.add(89);
//索引指定查询
// System.out.println(list.get(0));
// for(int i=0;i<7;i++){
// System.out.println(list.get(i));
// }
// for(int i=0;i<list.size();i++){
// System.out.println(list.get(i));
// }
//判断数组中对象是否存在contains(),返回时boolean
// System.out.println(list.contains("hyhy"));
//迭代器Iterator(迭代集合中所有的对象)
Iterator it=list.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
Collections.sort(list);
System.out.println("****************");
Iterator it1=list.iterator();
while(it1.hasNext()){
System.out.println(it1.next());
}
}
}
/************TestArrayList.java end***************/
/************Person.java begin***************/
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Collections;
public class Person implements Comparable{
private String name;
private int age;
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Person(){}
public String toString(){
return "name="+name+" age=" +age;
}
public void printList(List list){
Iterator i=list.iterator();
while(i.hasNext()){
System.out.println(i.next());
}
}
public int compareTo(Object obj) {
/**
*这里面堆对象的age进行比较产生序列
*/
if(obj instanceof Person){
Person p=(Person) obj;
// return this.age-p.getAge();
return p.getAge()-this.age;
}
return 0;
}
public static void main(String args[]){
Person p=new Person();
TestPerson tp=new TestPerson();
List list=new ArrayList();
list.add(new Person("大一",25));
list.add(new Person("张三",24));
list.add(new Person("王二",22));
list.add(new Person("李四",27));
p.printList(list);
Collections.sort(list,tp);
System.out.println("*************");
p.printList(list);
}
}
class TestPerson implements Comparator{
public int compare(Object o1, Object o2) {
// TODO Auto-generated method stub
if(o1 instanceof Person && o2 instanceof Person){
Person p=(Person) o1;
Person p1=(Person) o2;
return p.getAge()-p1.getAge();
// return p1.getAge()-p.getAge();
}
return 0;
}
}
/************Person.java end***************/