zoukankan      html  css  js  c++  java
  • Java——ArrayList使用Demo

    三种遍历方式

    • 通过迭代器Iterator遍历
    • 通过get(索引值)遍历
    • for循环遍历

    ArrayList使用Demo

    package list;
    import java.util.ArrayList;
    import java.util.Iterator;
    
    public class ArrayListDemo {
    
        public static void main(String[] srgs){
             ArrayList<Integer> arrayList = new ArrayList<Integer>();
    
             System.out.printf("Before add:arrayList.size() = %d
    ",arrayList.size());
    
             arrayList.add(1);
             arrayList.add(3);
             arrayList.add(5);
             arrayList.add(7);
             arrayList.add(9);
             System.out.printf("After add:arrayList.size() = %d
    ",arrayList.size());
    
             System.out.println("Printing elements of arrayList");
             // 三种遍历方式打印元素
             // 第一种:通过迭代器遍历
             System.out.print("通过迭代器遍历:");
             Iterator<Integer> it = arrayList.iterator();
             while(it.hasNext()){
                 System.out.print(it.next() + " ");
             }
             System.out.println();
    
             // 第二种:通过索引值遍历
             System.out.print("通过索引值遍历:");
             for(int i = 0; i < arrayList.size(); i++){
                 System.out.print(arrayList.get(i) + " ");
             }
             System.out.println();
    
             // 第三种:for循环遍历
             System.out.print("for循环遍历:");
             for(Integer number : arrayList){
                 System.out.print(number + " ");
             }
    
             // toArray用法
             // 第一种方式(最常用)
             Integer[] integer = arrayList.toArray(new Integer[0]);
    
             // 第二种方式(容易理解)
             Integer[] integer1 = new Integer[arrayList.size()];
             arrayList.toArray(integer1);
    
             // 抛出异常,java不支持向下转型
             //Integer[] integer2 = new Integer[arrayList.size()];
             //integer2 = arrayList.toArray();
             System.out.println();
    
             // 在指定位置添加元素
             arrayList.add(2,2);
             // 删除指定位置上的元素
             arrayList.remove(2);    
             // 删除指定元素
             arrayList.remove((Object)3);
             // 判断arrayList是否包含5
             System.out.println("ArrayList contains 5 is: " + arrayList.contains(5));
    
             // 清空ArrayList
             arrayList.clear();
             // 判断ArrayList是否为空
             System.out.println("ArrayList is empty: " + arrayList.isEmpty());
        }
    }

    【注意】

    1. java 中的length 属性是针对数组说的,比如说你声明了一个数组,想知道这个数组的长度则用到了 length 这个属性.

    2. java 中的length()方法是针对字 符串String说的,如果想看这个字符串的长度则用到 length()这个方法.

    3. .java 中的size()方法是针对泛型集合说的,如果想看这个泛型有多少个元素,就调用此方法来查看

  • 相关阅读:
    Custom Roles Based Access Control (RBAC) in ASP.NET MVC Applications
    How To Display Variable Value In View?
    How do negative margins in CSS work and why is (margin-top:-5 != margin-bottom:5)?
    Async/Await FAQ (Stephen Toub)
    Async and Await (Stephen Cleary)
    Change Assembly Version in a compiled .NET assembly
    C# Under the Hood: async/await (Marko Papic)
    Bootstrap form-group and form-control
    Infralution.Localization.Wpf
    cocos2d-x 图形绘制
  • 原文地址:https://www.cnblogs.com/MWCloud/p/11329593.html
Copyright © 2011-2022 走看看