• 一、集合框架(关于ArrayList,LinkedList,HashSet,LinkedHashSet,TreeSet)


    一、ArrayList

      解决了数组的局限性,最常见的容器类,ArrayList容器的容量capacity会随着对象的增加,自动增长。不会出现数组边界的问题。

    package collection;
     
    import java.util.ArrayList;
     
    import charactor.Hero;
     
    public class TestCollection {
        @SuppressWarnings("rawtypes")
        public static void main(String[] args) {
            //容器类ArrayList,用于存放对象
            ArrayList heros = new ArrayList();
            heros.add( new Hero("盖伦"));
            System.out.println(heros.size());
             
            //容器的容量"capacity"会随着对象的增加,自动增长
            //只需要不断往容器里增加英雄即可,不用担心会出现数组的边界问题。
            heros.add( new Hero("提莫"));
            System.out.println(heros.size());
             
        }
         
    }
    View Code

    二、ArrayList常用方法

    首先创建一个重写了toString的Hero类

    package charactor;
      
    public class Hero {
        public String name;
        public float hp;
      
        public int damage;
      
        public Hero() {
      
        }
      
        // 增加一个初始化name的构造方法
        public Hero(String name) {
      
            this.name = name;
        }
      
        // 重写toString方法
        public String toString() {
            return name;
        }
      
    }
    View Code

    1、add

    增加{1、直接add对象 hero.add(new Hero("demo")); 2、指定位置增加对象hero.add(3,"demo")}

    package collection;
     
    import java.util.ArrayList;
     
    import charactor.Hero;
     
    public class TestCollection {
        public static void main(String[] args) {
            ArrayList heros = new ArrayList();
     
            // 把5个对象加入到ArrayList中
            for (int i = 0; i < 5; i++) {
                heros.add(new Hero("hero " + i));
            }
            System.out.println(heros);
     
            // 在指定位置增加对象
            Hero specialHero = new Hero("special hero");
            heros.add(3, specialHero);
     
            System.out.println(heros.toString());
     
        }
     
    }
    View Code

    2、contains():判断是否存在

    3、get():获取指定位置的对象

    4、indexOf():获取对象所处的位置

    5、remove():删除

    6、set():替换

    7、size():获取大小

    8、toArray():转换为数组

    三、ArrayList和List

    ArrayList实现了接口List,常见的写法会把引用声明为接口List类型

    package collection;
      
    import java.util.ArrayList;
    import java.util.List;
     
    import charactor.Hero;
      
    public class TestCollection {
     
        public static void main(String[] args) {
            //ArrayList实现了接口List
             
            //常见的写法会把引用声明为接口List类型
            //注意:是java.util.List,而不是java.awt.List
            //接口引用指向子类对象(多态)
             
            List heros = new ArrayList();
            heros.add( new Hero("盖伦"));
            System.out.println(heros.size());
             
        }
          
    }
    View Code

    四、ArrayList遍历的方法

    1、使用for循环,通过获取ArrayList的size()来一一遍历

    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    package collection;
     
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
     
    import charactor.Hero;
     
    public class TestCollection {
     
        public static void main(String[] args) {
            List<Hero> heros = new ArrayList<Hero>();
     
            // 放5个Hero进入容器
            for (int i = 0; i < 5; i++) {
                heros.add(new Hero("hero name " + i));
            }
     
            // 第一种遍历 for循环
            System.out.println("--------for 循环-------");
            for (int i = 0; i < heros.size(); i++) {
                Hero h = heros.get(i);
                System.out.println(h);
            }
     
        }
     
    }
    View Code

    2、使用迭代器Iterator进行遍历,迭代器每次都是从一个空的位置开始,通过hasNext()来判断,当返回false时表示后面没有数据了结束遍历,获取通过next()方法获取。

    package collection;
     
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
     
    import charactor.Hero;
      
    public class TestCollection {
     
        public static void main(String[] args) {
            List<Hero> heros = new ArrayList<Hero>();
             
            //放5个Hero进入容器
            for (int i = 0; i < 5; i++) {
                heros.add(new Hero("hero name " +i));
            }
             
            //第二种遍历,使用迭代器
            System.out.println("--------使用while的iterator-------");
            Iterator<Hero> it= heros.iterator();
            //从最开始的位置判断"下一个"位置是否有数据
            //如果有就通过next取出来,并且把指针向下移动
            //直达"下一个"位置没有数据
            while(it.hasNext()){
                Hero h = it.next();
                System.out.println(h);
            }
            //迭代器的for写法
            System.out.println("--------使用for的iterator-------");
            for (Iterator<Hero> iterator = heros.iterator(); iterator.hasNext();) {
                Hero hero = (Hero) iterator.next();
                System.out.println(hero);
            }
             
        }
          
    }
    View Code

    3、常用的for循环遍历.

    不能进行ArrayList初始化,也不知道到底多少个元素在ArrayList,就是所有遍历出来

    五、ArrayList和LinkedList的区别

    ArrayList:顺序结构,插入,删除数据很慢(O(N)),查找快(O(1)),就类似于数组,跟链表的关系一样。

    LinkedList:链表结构,插入,删除数据快(O(1)),查找慢(O(N))。

    六、ArrayList和HashSet区别

    最大区别在于:

    1、是否有顺序:

    ArrayList:有顺序

    HashSet:无顺序(集合的无序性),它的具体顺序既不是按照hashcode的顺序,也不是按照插入顺序,根据在JVM的不用版本中,看到的顺序也是不同的,HashSet的顺序本身不稳定的。

    2、是否重复

    List中的数据可以重复

    Set中的数据不能重复

    重复判断的标准是:首先看hashcode是否相同,不同,则肯定是不同数据

    如果相同,在比较equals,如果equals相同,则是相同数据,否则是不同数据。

    七、HashSet,LinkedHashSet,TreeSet比较

    HashSet:无顺序

    LinkedHashSet:按照插入顺序

    TreeSet:从小到大顺序

    package collection;
    import java.util.HashSet;
    import java.util.LinkedHashSet;
    import java.util.TreeSet;
    
    public class TestCollection{
        public static void main(String[] args){
            HashSet<Integer> hashSet=new HashSet<Integer>();
            //HashSet中的数据不是按照插入顺序存放
            hashSet.add(22);
            hashSet.add(33);
            hashSet.add(55);
            System.out.println(hashSet);
            
            LinkedHashSet<Integer> linkedHashSet=new LinkedHashSet<Integer>();
            //LinkedHashSet中的数据按照插入顺序存放
            linkedHashSet.add(22);
            linkedHashSet.add(56);
            linkedHashSet.add(28);
            System.out.println(linkedHashSet);//22,56,28
            
            TreeSet<Integer> treeSet=new TreeSet<Integer>();
            //TreeSet中的数据是进行了排序的从小到大
            treeSet.add(22);
            treeSet.add(56);
            treeSet.add(28);
            System.out.println(treeSet);//22,28,56
        }
    }
    View Code
  • 相关阅读:
    上机练习3
    上机练习2
    上机练习1
    第一次作业
    第二次作业
    第一次作业
    第二次作业(4)
    第二次作业(3)
    第二次作业(2)
    第二次作业(1)
  • 原文地址:https://www.cnblogs.com/drq1/p/8482852.html
Copyright © 2020-2023  润新知