• day06(Collection,List,ArrayList,LinkedList,iterator迭代器,增强for)


    Collection   接口   

     

           方法实现    

         boolean  add(Object o);//添加

         boolean  remove(Object o);//移除

        修改方法   让实现类自己去实现修改元素

     判断功能
    boolean contains(Object o) 是否包含元素
    boolean isEmpty()            是否为空
    转换功能
    Object[] toArray();

         增强for进行遍历   获取集合中的元素

    public class CollectionTest01 {
    	public static void main(String[] args) {
    		Collection<String> c = new ArrayList<String>();
    		/*
    		 * 增删改查
    		 */
    		// 添加
    		c.add("hello");
    		c.add("world");
    		c.add("java");
    		System.out.println(c.contains("java"));
    		System.out.println(c.isEmpty());
    		System.out.println(c);
    		// 移除
    		c.remove("java");
    		System.out.println(c);
    		// 遍历
    		for (String st : c) {
    			System.out.println(st);
    		}
    		System.out.println("--------");
    		Object[] array = c.toArray();
    		for (int i = 0; i < array.length; i++) {
    			System.out.println(array[i]);
    		}
    		
    	}
    }
    

      

    输出结果:
    true false [hello, world, java] [hello, world] hello world -------- hello world

      

    List  继承于 Collection 接口

        //私有方法

       ListIterator   ListIterator()           解决了Iterator迭代器的添加并发性

       E get(int index)                            获取集合的元素

       E set(int index,E element)           修改指定位置的所以

        int    indexOf(Object o)                    查寻元素的索引位置

        

    public class ListTest {
    	public static void main(String[] args) {
    		List<String> list=new ArrayList<String>();
    		list.add("hello");
    		list.add("world");
    		list.add("java");
    		list.add(1, "Hi");//按照索引添加
    		System.out.println(list);
    		System.out.println("-------");
    		//获取元素
    		System.out.println(list.get(0));
    		System.out.println("-------");
    		//修改元素
    		list.set(0, "Hi");
    		System.out.println(list.get(0));
    		System.out.println("-------");
    		int i = list.indexOf("java");
    		System.out.println("java的索引位置"+i);
    	}
    }
    

      

    ArrayList

      list接口的实现类   实现了所有的方法

      构造方法

          ArrayList()   构造一个初始容量为 10 的空列表。  如果超过10个  会自动扩容

        ArrayList(int initialCapacity)   构造一个具有指定初始容量的空列表。

        底层是个数组

         增

          boolean  add(Object o);

              void add(int index,E element)

     E remove(int index);

    boolean remove(Object o);

     E set(int index,E element)


     E get(E Element)

    public class ArrayListTest {
    	public static void main(String[] args) {
    		ArrayList<String> array=new ArrayList<String>();
    		array.add("hello");
    		array.add("hello");
    		array.add("hello");
    		array.add("hello");
    		array.add(0,"java");//按照索引插入
    		System.out.println(array);
    		System.out.println("---");
    		array.remove(0);//按照索引移除
    		System.out.println(array.remove("hello"));//按照元素移除
    		System.out.println(array);
    		System.out.println("------");
    		array.set(0, "PHP");//按照索引修改
    		System.out.println(array);
    		System.out.println("----");
    		System.out.println(array.get(0));//按照索引获取元素
    	}
    }
    

      

    输出结果:
    [java, hello, hello, hello, hello]
    ---
    true
    [hello, hello, hello]
    ------
    [PHP, hello, hello]
    ----
    PHP
    

      

    LinkedList

    底层是个链表格式

     

    boolean add(Object o);

    void addFrist(Object o);//添加到第一位

    void addLast();//添加到最后一位

    E   removeFrist();//移除第一位

    E   removeLast();//移除最后一位

    E  remove();//默认移除开头位

    void  remove(int index);//移除指定索引的元素

    boolean  remove(Object o);//移除指定元素

     E  set(int index,E element)//修改指定索引的元素

    E  get(int index);//获取指定索引的元素

    E  getFrist();//获取第一位索引元素

    E  getLast();//获取最后一位的索引元素

    public class LinkedListTest {
    	public static void main(String[] args) {
    		LinkedList<String> list=new LinkedList<String>();
    		list.add("2");
    		list.add("3");
    		list.add("4");
    		list.add("5");
    		list.add("6");
    		list.add("7");
    		list.add("8");
    		list.add("9");
    		list.addFirst("1");
    		list.addLast("10");
    		System.out.println(list);
    		System.out.println("---");
    		list.remove();//默认移除索引第一位
    		System.out.println(list);
    		System.out.println("---");
    		list.remove(0);//移除指定位置的索引
    		System.out.println(list);
    		System.out.println("---");
    		list.removeFirst();//移除第一位索引元素
    		list.removeLast();//移除最后一位索引元素
    		System.out.println(list);
    		System.out.println("------");
    		System.out.println(list.getFirst());//获取第一位元素
    		System.out.println(list.getLast());//获取最后一位元素
    		System.out.println(list.get(0));//获取指定所以索引的元素
    		System.out.println("---");
    		list.set(0, "0");
    		System.out.println(list);
    	}
    }
    

      

    输出结果:
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    ---
    [2, 3, 4, 5, 6, 7, 8, 9, 10]
    ---
    [3, 4, 5, 6, 7, 8, 9, 10]
    ---
    [4, 5, 6, 7, 8, 9]
    ------
    4
    9
    4
    ---
    [0, 5, 6, 7, 8, 9]
    

      

     

    iterator迭代器

        作用:遍历集合

                                       Iterator接口---------ListIterator接口

           (所有集合做为它的实现类)     (只能被实现List接口的实现类使用)

          Iterator  在遍历的过程中不能进行添加数据  否则会报异常   但是可以移除元素(使用遍历器进行移除,在移除的过程中会修改器预期值)

        ( 当方法检测到对象的并发修改,但不允许这种修改时,抛出此异常。Exception in thread "main" java.util.ConcurrentModificationException)

    public class ArrayListTest {
    	public static void main(String[] args) {
    		ArrayList<String> array = new ArrayList<String>();
    		array.add("hello");
    		array.add("hello");
    		array.add("java");
    		array.add("hello");
    		array.add("hello");
    		Iterator<String> it = array.iterator();/Iterator遍历器没有添加功能
    		while (it.hasNext()) {
    			if (it.next().equals("java")) {
    				array.add("PHP");
    			}
    		}
    
    	}
    }
    

      并发异常:预期值与实际值不相符。所以产生异常。

        ListIterator 在迭代的过程可以进行添加操作(解决了并发性异常)

    public class ArrayListTest {
    	public static void main(String[] args) {
    		ArrayList<String> array = new ArrayList<String>();
    		array.add("hello");
    		array.add("hello");
    		array.add("java");
    		array.add("hello");
    		array.add("hello");
    		ListIterator<String> it = array.listIterator();
    		while (it.hasNext()) {
    			if (it.next().equals("java")) {
    				it.add("PHP");
    			}
    		}
    		for (String str : array) {
    			System.out.println(str);
    		}
    
    	}
    }
    

      

    输出结果:
    hello
    hello
    java
    PHP
    hello
    hello

    增强for

       for(初始化;判断条件语句;控制条件语句){

             循环体;

                   }

    for(类型  变量:集合对象){

    循环体;

    }

    public class ArrayListTest {
    	public static void main(String[] args) {
    		ArrayList<String> array = new ArrayList<String>();
    		array.add("hello");
    		array.add("hello");
    		array.add("java");
    		array.add("hello");
    		array.add("hello");
    		for (String str : array) {
    			System.out.println(str);
    		}
    
    	}
    }
    

      

    输出结果:
    hello
    hello
    java
    hello
    hello
    
  • 相关阅读:
    EBS: 弹性域.说明性.段的查询
    Oracle存储过程中cursor + with用法
    EBS:如何修改AR事务处理类型名称
    虚拟化——ovirt账号被禁用或者被锁住的解决方式
    glusterfs常用命令
    程序员面试建议
    删除Windows AD域控制器
    Debian服务器设置静态IP
    linux磁盘管理df du lsblk 孙龙
    linux磁盘分区 fdisk parted gdisk 孙龙
  • 原文地址:https://www.cnblogs.com/fjkgrbk/p/Collection_List_Introduce.html
Copyright © 2020-2023  润新知