Java ArrayList小程式理解

來源:互聯網
上載者:User

標籤:

package Collection;import java.util.ArrayList;import java.util.Iterator;//import javax.xml.crypto.AlgorithmMethod;/* 1. add方法的參數 類型是Object,以便於接收任意類型的對象 2. 集合中儲存的都是對象的引用(地址)  迭代器:就是集合取出元素的方式*/public class ClloectionDemo{/** * @param args */public static void main(String[] args){// TODO Auto-generated method stub// method2();method_get();}public static void method_get(){ArrayList<String> al = new ArrayList<String>();al.add("java1");al.add("java2");al.add("java3");// sop(al);// iterator():返回Iterator介面的子物件// 介面型引用只能指向自己介面的子類對象,該對象是從集合對象中的方法 new 出來的//Iterator it = al.iterator();/* Onewhile (it.hasNext()){sop(it.next());}*//* Twofor (int i = 0; i < al.size(); i++){sop(it.next());}*/ // Threefor(Iterator itt = al.iterator();itt.hasNext(); ){sop(itt.next());}}public static void method2(){ArrayList<String> al = new ArrayList<String>();al.add("java1");al.add("java2");al.add("java3");ArrayList<String> a2 = new ArrayList<String>();a2.add("java1");a2.add("java5");a2.add("java6");// 取交集,取相同的元素//al.retainAll(a2);    al.removeAll(a2);sop("al: "+al);sop("a2: "+a2);}public static void base_method(){//建立一個集合容器,使用Collection 介面的子類。ArrayListArrayList<String> al = new ArrayList<String>();// 添加al.add("heh");al.add("dhf");al.add("fdjkgd");// 列印sop("原集合:"+al);    //刪除al.remove("heh");sop(al);// 清空集合al.clear();// 是否為空白sop("是否為空白:"+al.isEmpty());// 判斷元素sop("heh是否存在:"+al.contains("heh"));// 擷取集合長度sop("size: "+al.size());}public static void sop(Object obj){System.out.println(obj);}}

  

Java ArrayList小程式理解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.