黑马程序员---Java Collection Framework

----------------------ASP.Net+Unity开发.Net培训、期待与您交流! ----------------------

1.概述

    可以存储任意类型的对象,并且长度可变,统称为集合。
    a.集合的分类
        1)单列集合
            Collection是单列集合的根接口,用于存储一系列符合某种规则的元素,它有两个主要的子接口,分别是List和Set。
                List        元素有序、可重复               接口的实现类主要有ArrayList和LinkedList                            
                Set        元素无序、不可重复            接口的实现类主要有HashSet和TreeSet                        
        2)双列集合
            Map是双列集合的根接口,用于存储具有键(key)值(value)映射关系的元素。每个元素都包含一对键值,在使用Map集合时
            可以通过指定的Key找到对应的value。Map接口的主要实现类有HashMap和TreeMap。


                                            
2.Collection接口
    Collection是所有单列集合的父接口。
                                                          Collection接口的方法
    ---------------------------------------------------------------------------------------------------------------------------------------
                        方法声明                                                    功能描述
        boolean add(Object o)                              向集合中添加一个元素
        boolean addAll(Collection c)                    将指定的Collection中的所有元素添加到该集合中
        void clear()                                                  删除该集合中的所有元素
        boolean remove(Object o)                        删除该集合中指定的元素
        boolean removeAll(Collection c)              删除该集合中的所有元素
        boolean isEmpty()                                      判断该集合是否为空
        boolean contains(Object o)                       判断该集合中是否包含某个元素
        boolean containsAll(Collection c)             判断该集合中是否包含指定集合中的所有元素
        Iterator iterator()                                         返回在该集合的元素上进行迭代的迭代器(Iterator)
        int size()                                                       获取该集合元素个数
    ---------------------------------------------------------------------------------------------------------------------------------------
    
3.List接口
    a.简介
        List接口继承自Collection接口。在List集合中允许出现重复的元素,所有元素是以一种线性方式进行存储的,在程序中可以通过索引老访问集合中指定的元素。List集合还有一个特点就是元素有序,即元素的存入顺序和取出顺序一致。
                                                        List集合常用方法表
        -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                            方法声明                                                               功能描述
            void add(int index,Object element)                         将元素element插入在List集合的index处
            boolean addAll(int index,Collection c)                    将集合c所包含的所有元素插入到List集合的index处
            Object get(int index)                                                  返回集合索引index处的元素
            Object remove(int index)                                           删除index索引处的元素
            Object set(int index,Object element)                       将索引index处元素替换成element对象,并将替换后的元素返回
            int indexOf(Object o)                                                 返回对象o在List集合中第一次出现的位置索引
            int lastIndexOf(Object o)                                           返回对象o在List集合中最后一次出现的位置索引
            List subList(int fromIndex,int toIndex)                      返回从索引fromIndex(包括)到索引toIndex(不包括)处所有元素集合组成的子集合
        ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        
    b.ArrayList集合
        ArrayList是List接口的一个实现类。在ArrayList内部封装了一个长度可变的数组,当存入的元素超过数组长度时,ArrayList会在内存中分配一个更大的的数组来存储这些元素(1.5倍增长,源码:newCapacity = oldCapacity + (oldCapacity >> 1);)
        Demo:

<pre name="code" class="java">public class ArrayListDemo {
    public static void main(String[] args) {
        ArrayList list = new ArrayList();//创建ArrayList集合对象
        list.add("stu1");
        list.add("stu2");
        list.add("stu3");
        list.add("stu4");
        System.out.println(list.size());//集合的长度
        System.out.println(list.get(1));//取得集合的第二个元素
    }
}
      
    注:集合和数组一样,索引的取值范围是从0开始的。由于ArrayList集合的底层是使用一个数组来保存元素,在增加或删除指定位置的元素时,会导致创建新的数组,效率比较低,因此不适合做大量的增删操作。但这种数组的结果允许程序通过索引的方式来访问元素,因此使用ArrayList集合查找匀速很便捷。
            
    c.LinkedList集合
        LinkedList是List接口的实现类。该集合内部维护了一个双向循环链表,链表中的每一个元素都使用引用的方式来记住它的前一个元素和后一个元素,从而可以将所有的元素彼此连接起来。当插入一个元素时,只需要修改元素之间的这种引用关系即可,删除一个节点也是如此。只是因为这样的存储结构,使得LinkedList集合对于元素的增删操作具有很高的效率。
                                                    LinkedList中定义的方法
        --------------------------------------------------------------------------------------------------------------------------------------
                        方法声明                                                        功能描述
            void add(int index,E element)                        在此列表中指定位置插入指定的元素
            void addFirst(Object o)                                   将指定元素插入此列表的开头
            void addLast(Object o)                                   将指定元素添加到此列表的结尾
            Object getFirst()                                              返回此列表的第一个元素
            Object getLast()                                               返回此列表的最后一个元素
            Object removeFirst()                                       移除并返回此列表的第一个元素
            Object removeLast()                                       移除并返回此列表的最后一个元素
        --------------------------------------------------------------------------------------------------------------------------------------
        Demo:

public class LinkedListDemo {
    public static void main(String[] args) {
        LinkedList list = new LinkedList();//创建LinkedList集合
        list.add("sut1");
        list.add("sut2");
        list.add("sut3");
        list.add("sut4");
        System.out.println(list.toString());//打印该集合中的元素
        list.add(3, "teacher1");
        System.out.println(list.toString());//打印该集合中的元素
        list.addFirst("person1");
        System.out.println(list.toString());//打印该集合中的元素
        System.out.println(list.getLast());
        list.remove(2);
        System.out.println(list.toString());//打印该集合中的元素
        list.removeLast();
        System.out.println(list.toString());//打印该集合中的元素
    }
}
           
    d.Iterator接口
        Iterator接口是Java集合框架中的一员,但它与Collection、Map接口有所不同,Collection接口与Map接口主要用于存储元素,而Iterator主要用于遍历Collection中的元素,因此,Iterator对象也被称为迭代器。
        Demo:

public class IteratorDemo {
    public static void main(String[] args) {
        ArrayList list = new ArrayList();//创建ArrayList集合对象
        list.add("stu1");
        list.add("stu2");
        list.add("stu3");
        list.add("stu4");
        Iterator it = list.iterator();//获取Iterator对象
        while(it.hasNext()){
            String str = (String)it.next();//next()方法返回的数据类型为Object
            System.out.print(str+",");
        }
    }
}
        注:Iterator迭代器对象在遍历集合时,内部采用指针的方式来跟踪集合中的元素。当通过迭代器获取ArrayList集合中的元素时,都会将这些元素当做Object类型来看待,如果想得到特定类型的元素,则需要进行强制类型转换。
            
    e.foreach循环
        foreach循环是一种更加简洁的for循环,也称增强for循环。
        语法格式:
            for(容器中元素类型 临时变量 : 容器变量){
                执行语句
            }
        Demo:

public class ForeachDemo {
    static String[] strs = {"aaa", "bbb", "ccc"};
    public static void main(String[] args) {
        ArrayList list = new ArrayList();//创建ArrayList集合对象
        list.add("stu1");
        list.add("stu2");
        list.add("stu3");
        list.add("stu4");
        for(Object obj : list){
            System.out.print(obj + ",");
        }
        System.out.println("\n--------------------------------------------");
        for(String str : strs){
            str = "ddd";
        }
        System.out.println(strs[0]+","+strs[1]+","+strs[2]);
    }
}
        注:foreach循环虽然书写起来很简洁,但在使用时也存在一定的局限性。当使用foreach循环遍历集合和数组时,只能访问集合中的元素,不能对其中 的元素进行修改。
        Demo:

public class IteratorDemo {
    public static void main(String[] args) {
        ArrayList list = new ArrayList();//创建ArrayList集合对象
        list.add("Jack");
        list.add("Annie");
        list.add("Rose");
        list.add("Tom");
        Iterator it = list.iterator();//获取Iterator对象
        while(it.hasNext()){
            Object obj = it.next();//next()方法返回的数据类型为Object
            if("Annie".equals(obj)){
                //list.remove(obj); //java.util.ConcurrentModificationException并发修改异常
                it.remove();
                /*//这种写法也可以,使用集合对象删除之后,直接结束循环
                 * list.remoce(obj);
                 * break;
                 * */
            }
        }
        System.out.print(list);
    }
}
        注:使用Iterator迭代器对集合中的元素进行迭代时,如果调用集合对象的remove()方法去删除元素,会出现并发修改异常。
            解决方案:
                1)使用集合对象删除元素后,直接结束遍历
                2)使用Iterator对象删除元素
                
    f.ListIterator接口
        ListIterator是Iterator的子类,ListIterator中提供了hasPrevious()方法和previous()方法,通过这两个方法可以实现反向迭代元素。
                                                        ListIterator方法表
        ---------------------------------------------------------------------------------------------------------------------------------
                        方法声明                                                功能描述
            void add(Object o)                                   将指定的元素插入列表
            boolean hasPrevious()                            如果已逆向遍历列表,列表
            Object previous()                                     返回列表总前一个元素
            void remove()                                           从列表中移除有next或previous返回的最后一个元素
        ---------------------------------------------------------------------------------------------------------------------------------
        Demo:

public class ListIteratorDemo {
    public static void main(String[] args) {
        ArrayList list = new ArrayList();//创建ArrayList集合对象
        list.add("Jack");
        list.add("Annie");
        list.add("Rose");
        list.add("Tom");
        System.out.println(list);
        ListIterator it = list.listIterator(list.size());//获取ListIterator对象
        while(it.hasPrevious()){
            Object obj = it.previous();
            System.out.print(obj+",");
        }
    }
}
        注:在使用listIterator(int index)方法获得ListIterator对象时,需要传递一个int类型的参数指定迭代的起始位置。ListIterator迭代器只能用于List集合。
    g.Enumeration接口
        在JDK1.2之前还没有Iterator接口的时候,遍历集合需要使用Enumeration。JDK中提供了一个Vector集合,该集合是List接口的一个实现类,用法和ArrayList完全相同,区别在于Vector集合时线程安全的,而ArrayList集合时线程不安全。在Vector类中提供了一个elements()方法用于返回Enumeration对象,通过Enumeration对象就可以遍历该集合中的元素。
        Demo:

public class EnumerationDemo {
    public static void main(String[] args) {
        Vector v = new Vector();
        v.add("jack");
        v.add("Rose");
        v.add("Tom");
        Enumeration e = v.elements();
        while(e.hasMoreElements()){
            Object obj = e.nextElement();
            System.out.print(obj+",");
        }
    }
}
           
4.Set接口
    a.概述
        Set接口和List接口一样,同样继承自Collection接口,它与Collection接口中的方法基本一致。与List接口不同的是,Set接口中元素无序,并且会以某种规则保证存入的元素不出现重复。
        Set接口主要有两个实现类,分别是HashSet和TreeSet。
            HashSet是根据对象的哈希值来确定元素在集合中的存储位置,因此具有良好的存取和查找性能。HashSet底层是由HashMap实现的。
            TreeSet是以二叉树的方式来存储元素,他可以实现对集合中的元素进行排序。
    b.HashSet集合
        HashSet是Set接口的一个实现类,它所存储的元素时不可重复的,并且元素都是无序的。当向HashSet集合中添加一个对象时,首先会调用该对象的 hashCode()方法来确定元素的存储位置,然后再调用对象的equals()方法来确保该位置没有重复的元素。
        Demo:

public class HashSetDemo {
    public static void main(String[] args) {
        HashSet hashSet = new HashSet();
        hashSet.add("Jack");
        hashSet.add("Eve");
        hashSet.add("Rose");
        hashSet.add("Eve");
        Iterator it = hashSet.iterator();
        while(it.hasNext()){
            Object obj = it.next();
            System.out.print(obj+",");
        }
        System.out.println();
        System.out.println(hashSet);
    }
}
        注:查看API源码可以看出,HashSet底层是有HashMap实现的。在add新元素时,要检查三个条件,满足就是同一元素,不满足就直接存储。
            HashMap存储元素方法源码:

public V put(K key, V value) {
    if (table == EMPTY_TABLE) {
        inflateTable(threshold);
    }
    if (key == null)
        return putForNullKey(value);
    int hash = hash(key);
    int i = indexFor(hash, table.length);
    for (Entry<K,V> e = table[i]; e != null; e = e.next) {
        Object k;
        if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
            V oldValue = e.value;
            e.value = value;
            e.recordAccess(this);
            return oldValue;
        }
    }

    modCount++;
    addEntry(hash, key, value, i);
    return null;
}
    c.重写hashCode()和equals()方法
        当向集合中存入元素时,为了保证HashSet正常工作,要求在存入对象时,重写该类中的hashCode()和equals()方法。
        Demo:

public class Demo {
    public static void main(String[] args) {
        Set set = new HashSet();
        Student stu1 = new Student("1","jack");
        Student stu2 = new Student("2","rose");
        Student stu3 = new Student("3","tom");
        Student stu4 = new Student("2","rose");
        set.add(stu1);
        set.add(stu2);
        set.add(stu3);
        set.add(stu4);
        System.out.println(set);
    }
}
class Student {
    String id;
    String name;
    public Student(String id, String name){
        this.id = id;
        this.name = name;
    }
    public String toString(){
        return id + "," + name;
    }
}
         注:结果为[3,tom, 2,rose, 1,jack, 2,rose],之所以没有除去重复的元素时因为在定义Student类时没有重写hashCode()和equals()方法。
        Demo:(修正之后的Demo)

public class Demo {
    public static void main(String[] args) {
        Set set = new HashSet();
        Student stu1 = new Student("1","jack");
        Student stu2 = new Student("2","rose");
        Student stu3 = new Student("3","tom");
        Student stu4 = new Student("2","rose");
        set.add(stu1);
        set.add(stu2);
        set.add(stu3);
        set.add(stu4);
        System.out.println(set);
    }
}
class Student {
    String id;
    String name;
    public Student(String id, String name){
        this.id = id;
        this.name = name;
    }
    //重写toString()方法
    public String toString(){
        return id + "," + name;
    }
    //重写hashCode()方法
    public int hashCode(){
        return id.hashCode();
    }
    //重写equals()方法
    public boolean equals(Object obj){
        if(this == obj){
            return true;
        }
        if(!(obj instanceof Student)){
            return false;
        }
        Student stu = (Student)obj;
        return this.id.equals(stu.id);
    }
}
           
    d.TreeSet集合
        TreeSet是Set接口的另一个实现类,它内部采用自平衡的排序二叉树来存储元素。这样的结构可以保证TreeSet集合中没有重复的元素,并且可以对元素进行排序。TreeSet的底层是由TreeMap实现的。
            TreeMap存储元素方法源码:

public V put(K key, V value) {
    Entry<K,V> t = root;
    if (t == null) {
        compare(key, key); // type (and possibly null) check

        root = new Entry<>(key, value, null);
        size = 1;
        modCount++;
        return null;
    }
    int cmp;
    Entry<K,V> parent;
    // split comparator and comparable paths
    Comparator<? super K> cpr = comparator;
    if (cpr != null) {
        do {
            parent = t;
            cmp = cpr.compare(key, t.key);
            if (cmp < 0)
                t = t.left;
            else if (cmp > 0)
                t = t.right;
            else
                return t.setValue(value);
        } while (t != null);
    }
    else {
        if (key == null)
            throw new NullPointerException();
        Comparable<? super K> k = (Comparable<? super K>) key;
        do {
            parent = t;
            cmp = k.compareTo(t.key);
            if (cmp < 0)
                t = t.left;
            else if (cmp > 0)
                t = t.right;
            else
                return t.setValue(value);
        } while (t != null);
    }
    Entry<K,V> e = new Entry<>(key, value, parent);
    if (cmp < 0)
        parent.left = e;
    else
        parent.right = e;
    fixAfterInsertion(e);
    size++;
    modCount++;
    return null;
}
         TreeSet在存储元素时,过滤重复元素,自动排序:
            Demo:

public class TreeSetDemo {
    public static void main(String[] args) {
        Set set = new TreeSet();//创建TreeSet集合
        set.add("Jack");
        set.add("Eve");
        set.add("Rose");
        set.add("Eve");
        Iterator it = set.iterator();
        while(it.hasNext()){
            System.out.print(it.next()+",");
        }
    }
}
             注:想要对集合中的元素进行排序,元素类就必须实现Comparable接口。JDK中大部分类都实现了Comparable接口,拥有了接口中的CompareTo()方法,如Integer、Double和String等。
        当向TreeSet中放入自定义类对象时,此类必须实现Comparable接口。
            Demo:

public class TreeSetDemo_1 {
    public static void main(String[] args) {
        Set set = new TreeSet();
        set.add(new Person("jack",24));
        set.add(new Person("rose",18));
        set.add(new Person("tom",32));
        set.add(new Person("rose",18));
        System.out.println(set);
    }
}
class Person implements Comparable {
    String name;
    int age;
    public Person(String name, int age){
        this.name = name;
        this.age = age;
    }
    public String toString(){
        return name+"-"+age;
    }
    public int compareTo(Object obj) {
        Person p = (Person)obj;
        if(this.age > p.age){
            return 1;
        }
        if(this.age == p.age){
            return this.name.compareTo(p.name);
        }
        return -1;
    }
}
        注:程序中,首先按照年龄排序,年龄相同时按照姓名排序。
        自定义排序规则:
            有时,定义的类没有实现Comparable接口或者实现了Comparable接口,但是不想按照定义的CompareTo()方法进行排序,这是可以使用自定义比较器实现对TreeSet集合中的元素排序,即实现Comparator接口,在创建TreeSet集合对象时指定比较器。
        Demo:

public class CustomComparatorDemo {
    public static void main(String[] args) {
        Set set = new TreeSet(new MyComparator());//创建TreeSet集合
        set.add("jsaon");
        set.add("Eve");
        set.add("Rose");
        set.add("Eve");
        Iterator it = set.iterator();
        while(it.hasNext()){
            System.out.print(it.next()+",");
        }
    }
}
class MyComparator implements Comparator {
    public int compare(Object obj1, Object obj2) {
        String s1 = (String)obj1;
        String s2 = (String)obj2;
        return s1.length() - s2.length();
    }    
}
        注:在创建TreeSet集合时,将MyComparator比较器对象传入,当向集合中添加元素时,比较器对象的compare()方法就会自动被调用,从而使存入TreeSet集合中的字符串按照长度进行排序。


5.Map接口

    a.概述
        Map接口是一种双列集合,他的每个元素都包含一个键对象Key和一个值对象Value,键和值对象之间存在一种对应关系,称为映射。从Map集合中访问元素时,只要指定了Key,就能找到对应的Value。
                                                        Map集合常用方法
        --------------------------------------------------------------------------------------------------------------------------------------
                            方法声明                                                                        功能描述
            void put(Object key,Object value)                        将指定的值与此映射中的指定键关联,即存储元素
            Object get(Object key)                                         返回指定键所映射的值,没有则返回null
            boolean containsKey(Object key)                       判断集合中是否包含指定键的元素
            boolean containsValue(Object value)                 判断集合中是否包含指定值的元素
            Set keySet()                                                           返回此映射中包含的键的Set视图
            Collection<V> values()                                         返回此映射中包含的值的Collection视图
            Set<Map.Entry<E,V>> entrySet()                        返回此映射中包含的映射关系的Set视图
        --------------------------------------------------------------------------------------------------------------------------------------
    b.HashMap集合
        HashMap集合是Map接口的一个实现类,它用于存储键值映射关系,但必须保证不出现重复的键。
        Demo:

public class HashMapDemo {
    public static void main(String[] args) {
        Map map = new HashMap();
        map.put("1", "jack");
        map.put("2", "rose");
        map.put("3", "tom");
        map.put("2", "mary");
        System.out.println(map.get("1"));
        System.out.println(map);
    }
}
        注:结果为{3=tom, 2=mary, 1=jack},Map集合中的键具有唯一性,当向集合中添加同样键值"2"的元素时,"mary"取代了"rose"。即如果存储了相同的键,后存储的值则会覆盖原有的值。
        取出Map中所有的键和值:
            1)keySet
                Demo:

public class HashMapDemo {
    public static void main(String[] args) {
        Map map = new HashMap();
        map.put("1", "jack");
        map.put("2", "rose");
        map.put("3", "tom");
        Set set = map.keySet();
        Iterator it = set.iterator();
        while(it.hasNext()){
            String key = (String)it.next();
            System.out.println(key+"-"+map.get(key));
        }
    }
}
                注:Map对象的keySet()方法,获得存储Map中所有键的Set集合。
            2)entrySet
                Demo;

public class HashMapDemo {
    public static void main(String[] args) {
        Map map = new HashMap();
        map.put("1", "jack");
        map.put("2", "rose");
        map.put("3", "tom");
        Set entrySet = map.entrySet();
        Iterator it = entrySet.iterator();
        while(it.hasNext()){
            Map.Entry entry = (Map.Entry)it.next();
            System.out.println(entry.getKey()+"-"+entry.getValue());
        }
    }
}
                 注:Map对象的entrySet()方法获得存储在Map中所有映射的Set集合,这个集合中存放了Map.Entry类型的元素(Entry是Map接口内部类),每个Map.Entry对象代表Map中的一个键值对。
            3)values
                Demo:

public class HashMapDemo {    
    public static void main(String[] args) {
        Map map = new HashMap();
        map.put("1", "jack");
        map.put("2", "rose");
        map.put("3", "tom");
        Collection values = map.values();
        Iterator it = values.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
    }
}
                 注:Map的values()方法获取包含Map中所有值的Collection集合。
        java中提供了LinkedHashMap类,它是HashMap的子类,和LinkedList一样也使用双向链表来维护内部元素的关系,是Map元素  迭代的顺序与存入的顺序一致。
            Demo:

public class HashMapDemo {    
    public static void main(String[] args) {
        Map map = new LinkedHashMap();
        map.put("1", "jack");
        map.put("2", "rose");
        map.put("3", "tom");
        Collection values = map.values();
        Iterator it = values.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
    }
}
       
    c.TreeMap集合
        TreeMap是Map接口的一个实现类。TreeMap集合时用来存储键值映射关系,其中不允许出现重复的键。在TreeMap中是通过二叉树的原理来保证键的唯一性,这与TreeSet集合的存储原理一样,因此TreeMap中所有的键是按照某种顺序排列的。
        Demo:

public class HashMapDemo {    
    public static void main(String[] args) {
        Map map = new TreeMap();
        map.put("1", "jack");
        map.put("2", "rose");
        map.put("3", "tom");
        Set set = map.keySet();
        Iterator it = set.iterator();
        while(it.hasNext()){
            Object key = it.next();
            Object value = map.get(key);
            System.out.println(key+"-"+value);
        }
    }
}
    d.properties集合
        Map接口中海油一个实现类HashTable,它和HashMap十分相似,去呗在于HashTable是线程安全的。HashTable存取元素时速度很慢,目前基本上被HashMap类取代。但HashTable类有一个子类Properties在实际应用中非常重要,Properties主要用来存储字符串类型的键和值,在实际开发中,经常使用Properties集合来存取应用的配置项。
        Demo:

public class ProperitiesDemo {
    public static void main(String[] args) {
        Properties p = new Properties();
        p.setProperty("Backgroup-color", "red");
        p.setProperty("Font-size", "14px");
        p.setProperty("Language", "chinese");    
        Enumeration en = p.propertyNames();
        while(en.hasMoreElements()){
            String key = (String)en.nextElement();
            String value = p.getProperty(key);
            System.out.println(key+"---"+value);
        }
    }
}


6.泛型

    java中引入了"参数化类型(parameterized type)"这个概念,他可以限定方法操作的数据类型,在定义集合类时,使用"<参数化类型>"的方式制定该类中方法操作的数据类型,格式:ArrayList<参数化类型> list = new ArrayList<参数化类型>();
    Demo:

public class Demo {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        list.add("list");
        list.add("collection");
        for(String str : list){
            System.out.println(str);
        }
    }
}
     注:在使用泛型后每次遍历集合元素时,可以制定元素类型为String,而不是Object,这样就避免了在程序中进行强制类型转换。

7.Collections和Arrays工具类

    a.Collections
                                                            Collections常用方法
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                        方法声明                                                                                                                            功能描述
        static <T> boolean addAll(Collection<? super T> c,T... elements)            将所有指定元素添加到指定的collection中
        static void reverse(List list)                                                                            反转指定List集合中元素的顺序
        static void shuffle(List list)                                                                              对List集合中的元素进行随机排序
        static void sort(List list)                                                                                  根据元素的自然顺序对List集合中元素进行排序
        static void swap(List list,int i,int j)                                                                 将指定List集合中i处元素和j处元素进行交换
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        static int binarySearch(List list,Object key)                                  使用二分法搜索指定对象在List集合中的索引(集合元素必须有序)             static Object max(Collection c)                                                     根据元素的自然顺序,返回给定集合中最大的元素
        static Object min(Collection c)                                                      根据元素的自然顺序,返回给定集合中最小的元素
        static boolean replaceAll(List list,Object oldVal,Object newVal)            用一个新的newVal替换List集合中所有的旧值oldVal
    ---------------------------------------------------------------------------------------------------------------------------------------
    b.Arrays
                                                            Arrays常用方法
    ---------------------------------------------------------------------------------------------------------------------------------------
                        方法                                                                                           功能描述
        sort()                                                                                   对数组进行排序
        binarySearch(Object[] a,Object key)                               二分法查找元素(只针对排序后的数组进行查找)
        copyOfRange(int[] original,int from,int to)                       将数组中指定范围的元素复制到一个新的数组中
        fill(Object[] a,Object val)                                                    用一个值替换数组中所有元素
        toString(int[] arr)                                                                 将数组转化为字符串
    ---------------------------------------------------------------------------------------------------------------------------------------

    
    
----------------------ASP.Net+Unity开发.Net培训、期待与您交流! ----------------------
    

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值