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
  • 相关阅读:
    hiho 1483 区间计数问题+二分答案
    Educational Codeforces Round 17 D dp
    Educational Codeforces Round 17 C 二分
    hiho 1461 暴力+随机
    (转)重置Mac OS X管理员密码
    解析Xcode把应用程序打包成ipa---解决打包完新版本itunes提示不是有效应用程序的问题
    SVN权限修复
    MAC安装SVNServer
    设置UIScrollView只可以水平或者竖直滚动
    (转)android之Fragment(官网资料翻译)
  • 原文地址:https://www.cnblogs.com/duanxiansen/p/12133827.html
Copyright © 2011-2022 走看看