zoukankan      html  css  js  c++  java
  • ArrayList学习-动态数组队列

    //定义长度为10的数组
            ArrayList<Integer> arrayList = new ArrayList<Integer>(10);
            //添加元素
            for (int i = 0; i < 10; i++) {
                arrayList.add(i);
            }
            //将ArrayList实例的容量设置为集合的当前大小,最小化ArrayList的容量
            arrayList.trimToSize();
            System.out.println("原始数组:"+arrayList);
            Collections.reverse(arrayList);
            System.out.println("反转数组:"+arrayList);
            Collections.sort(arrayList);
            System.out.println("升序数组:"+arrayList);
            //定制排序的用法
            Collections.sort(arrayList, new Comparator<Integer>() {
                @Override
                public int compare(Integer o1, Integer o2) {
                    return o2.compareTo(o1);
                }
            });
            System.out.println("定制排序数组:"+arrayList);
            //迭代器遍历
            Iterator<Integer> iterator = arrayList.iterator();
            while (iterator.hasNext()) {
                Integer value = iterator.next();
                System.out.println(value);
            }
            //通过索引值遍历数据
            for (int i = 0; i < arrayList.size(); i++) {
                System.out.println(arrayList.get(i));
            }
            //for遍历
            for (Integer integer : arrayList) {
                System.out.println(integer);
            }
            //按顺序返回此集合内所有元素,由于toArray内部调用了copyOf()方法,实际是将整个集合重新复制了一份进行返回
            Object[] integers = arrayList.toArray();
            System.out.println("数组1:"+arrayList);
            //返回数组
            Integer[] integers2 = new Integer[arrayList.size()];
            arrayList.toArray(integers2);
            System.out.println("数组2:"+arrayList);
            //在指定位置插入元素,ArrayList内部插入元素前会调用arraycopy()方法,将index后所有元素复制并进行移位操作
            arrayList.add(2, 999);
            System.out.println("在指定位置插入元素:"+arrayList);
            //删除指定位置的对象
            arrayList.remove(2);
            System.out.println("在指定位置删除元素:"+arrayList);
            //删除指定对象
            arrayList.remove((Object)0);
            System.out.println("删除对象:"+arrayList);
            //获取对象所处位置,ArrayList内部会从头到尾开始遍历元素获取位置
            System.out.println("获取对象所处位置:"+arrayList.indexOf(5));
            //获取对象所处位置,ArrayList内部会从尾到头开始遍历元素获取位置
            System.out.println("获取对象所处位置:"+arrayList.lastIndexOf(5));
            //判断集合是否包含指定元素
            System.out.println("是否包含对象:"+arrayList.contains((Object)0));
            //清空整个集合
            arrayList.clear();
            //判断集合是否为空
            System.out.println("判断集合是否为空:"+arrayList.isEmpty());
    View Code
  • 相关阅读:
    ini_set /ini_get函数功能-----PHP
    【转】那个什么都懂的家伙
    word 2007为不同页插入不同页眉页脚
    August 26th 2017 Week 34th Saturday
    【2017-11-08】Linux与openCV:opencv版本查看及库文件位置等
    August 25th 2017 Week 34th Friday
    August 24th 2017 Week 34th Thursday
    August 23rd 2017 Week 34th Wednesday
    August 22nd 2017 Week 34th Tuesday
    August 21st 2017 Week 34th Monday
  • 原文地址:https://www.cnblogs.com/duanxiansen/p/12133827.html
Copyright © 2011-2022 走看看