首页 > 代码库 > List的功能用法

List的功能用法

一、基本介绍

1、List集合内的元素是有序的,能用下标获取的

2、List接口的两个常用实现类:ArrayList 和 LinkedList 

3、一些常用方法:

    get()、set()、add()、remove()、subList()

4、List和数组之间的转换:

  a、List转换为数组:

  b、数组转换为List:

  

        /**
         * List转换为数组:toArray()方法
         */
        List<String> list = new ArrayList<String>();
        list.add("one");
        list.add("two");
        list.add("three");
        list.add("four");
        System.out.println("原集合为:"+list);
        String[] array = list.toArray(new String[]{});
        for(String str:array){
            System.out.println(str);
        }
        /**
         * 数组转换为List: Arrays.asList()方法
         */
        String[] array1 = {"five","six","seven","eight"};
        List<String> list1 = Arrays.asList(array1);
        System.out.println(list1);
    }

5、List的排序功能:

 (1)自然排序(从小到大):

        /**
         * List的自然排序
         */
        List<Integer> list = new ArrayList<Integer>();
        Random ran = new Random();
        for(int i=0;i<10;i++){
            list.add(i,ran.nextInt(100));
        }
        System.out.println("原集合为:"+list);
        Collections.sort(list);
        System.out.println("自然排序后的集合为:"+list);

 (2)引用类型的排序(两种实现方法):

 a、Comparable:引用元素必须是Comparable的子类

package Java20170330;

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

class Point implements Comparable<Point>{
    int x;
    int y;
    public Point(int x, int y) {
        super();
        this.x = x;
        this.y = y;
    }
    public String toString() {
        return "("+x + "," + y +")";
    }

    public int compareTo(Point p){
        return (int) (Math.sqrt(this.x*this.x+this.y*this.y)-Math.sqrt(p.x*p.x-p.y*p.y));
    }
    }

public class Blogtest01 {
    public static void main(String[] args) {
        List<Point> list = new ArrayList<Point>();
        list.add(new Point(3,3));
        list.add(new Point(1,4));
        list.add(new Point(4,3));
        list.add(new Point(2,5));
        Collections.sort(list);
        System.out.println("排序后的集合为:"+list);    
    }
}



运行结果为:
排序后的集合为:[(3,3), (1,4), (4,3), (2,5)]

 

 

 b、Comparator : 适用于临时指定比较规则

Demo1:

package Java20170330;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Blogtest {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        list.add("cnmgb");
        list.add("nmb");
        list.add("nmgb");
        list.add("nm");
        System.out.println("原集合为:"+list);
        Collections.sort(list,new Comparator<String>(){
            public int compare(String str1,String str2){
                return str1.length()-str2.length();
            }
        });
        System.out.println("排序后的集合为:"+list);
    }
}



运行结果为:

     原集合为:[cnmgb, nmb, nmgb, nm]
     排序后的集合为:[nm, nmb, nmgb, cnmgb]


 

 Demo2:

package Java20170330;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

class Point{
        int x;
        int y;
        public Point(int x, int y) {
            super();
            this.x = x;
            this.y = y;
        }
        @Override
        public String toString() {
            return "(" + x + "," + y + ")";
        }
    }
public class Blogtest {
    
    public static void main(String[] args) {
        List<Point> list = new ArrayList<Point>();
        list.add(new Point(3,3));
        list.add(new Point(1,4));
        list.add(new Point(4,3));
        list.add(new Point(2,5));
        System.out.println("原集合为:"+list); 
        Collections.sort(list,new Comparator<Point>(){
            public int compare(Point p1,Point p2){
            return (int) (Math.sqrt(p1.x*p1.x+p1.y*p1.y)-Math.sqrt(p2.x*p2.x-p2.y*p2.y));
}
});
        System.out.println("排序后的集合为:"+list);
}



运行结果为:
原集合为:[(3,3), (1,4), (4,3), (2,5)]
排序后的集合为:[(3,3), (1,4), (4,3), (2,5)]

 

List的功能用法