java集合

package polymorphism.container;

import java.util.Arrays;
import java.util.List;

public class ListTest {

/**
* @param args
*/
public static void main(String[] args) {

List<Integer> list = Arrays.asList(1,2,3,4,5);

System.out.println(list);

list.add(6);

}

}

这样的话,List(列表)就是固定大小的了.因为其底层表示的是静态数组 所以就不能add了
会抛异常 java.lang.UnsupportedOperationException


----------------------------------------------------------------------------------

package com.bit;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

public class AddingGroups {

/**
* @param args
*/
public static void main(String[] args) {

Collection<Integer> collection = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5));
         Integer[] moreInts = {6,7,8,9,10};
collection.addAll(Arrays.asList(moreInts));
Collections.addAll(collection, 11,12,13,14,15);
Collections.addAll(collection, moreInts);
System.out.println(collection);
}

}

结果:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 6, 7, 8, 9, 10]

----------------------------------------------------------------------------------

package com.bjsxt;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;

public class ArrayListTest {

public static void printElements(Collection<?> c) {
Iterator<?> it = c.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}

/**
* @param args
*/
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 al = new ArrayList();
al.add(s1);
al.add(s2);
al.add(s3);
Collections.sort(al);
printElements(al);
}

}

class Student implements Comparable {

int num;
String name;

Student(int num,String name) {
this.num = num;
this.name = name;
}

public int compareTo(Object o) {
Student s = (Student)o;
if(num > s.num) {
return 1;
}else if(num == s.num) {
return 0;
}else {
return -1;
}
}

public String toString() {
return num + ":" + name;
}

}

结果:

1:lisi
2:zhangsan
3:wangwu

--------------------------------------------------------------------------------

package diaodiao.shama;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;

public class ArrayListTest2 {

public static void printElements(Collection<?> c) {
Iterator<?> it = c.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}

/**
* @param args
*/
public static void main(String[] args) {

        Student s1 = new Student(2,"zhangsan");
        Student s2 = new Student(1,"lisi");
Student s3 = new Student(3,"wangwu");
Student s4 = new Student(2,"mybole");
ArrayList al = new ArrayList();
al.add(s1);
al.add(s2);
al.add(s3);
al.add(s4);
Collections.sort(al,new Student.StudentComparator());
printElements(al);

}

}

class Student {

int num;
String name;

static class StudentComparator implements Comparator {

public int compare(Object o1,Object o2) {
Student s1 = (Student)o1;
Student s2 = (Student)o2;
int result = 0;
if(s1.num > s2.num) {
result = 1;
} else if(s1.num == s2.num) {
result = 0;
} else if(s1.num < s2.num) {
result = -1;
}

if(result == 0) {
result = s1.name.compareTo(s2.name);
}

return result;
}

}

Student(int num,String name) {
   this.num = num;
   this.name = name;
}

public String toString() {
return  "num: "+ num + " name: " + name;
}

}


num: 1 name: lisi
num: 2 name: mybole
num: 2 name: zhangsan
num: 3 name: wangwu

----------------------------------------------------------------------------------


package kayizaoku.hama;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;

public class ArrayListTest3 {

public static void printElements(Collection<?> c) {
Iterator<?> it = c.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}

/**
* @param args
*/
public static void main(String[] args) {

        Student s1 = new Student(1,"lisi");
Student s2 = new Student(3,"wangwu");
Student s3 = new Student(2,"mybole");

ArrayList al = new ArrayList();
al.add(s1);
al.add(s2);
al.add(s3);
Collections.sort(al,Collections.reverseOrder());
printElements(al);

}

}


class Student implements Comparable{

int num;
String name;

Student(int num,String name) {
   this.num = num;
   this.name = name;
}

public String toString() {
return  "num: "+ num + " name: " + name;
}

public int compareTo(Object o) {
Student s = (Student)o;
if(num > s.num) {
return 1;
} else if(num == s.num) {
return 0;
} else {
return -1;
}
}

}
结果:
num: 3 name: wangwu
num: 2 name: mybole
num: 1 name: lisi

你可能感兴趣的:(Java List)