ArrayList類示例1

               ArrayList類示例2

import java.util.ArrayList;



public class ArrayListDemo2 {

//放入

public static void main(String[] args){

ArrayList<Integer> a1 = new ArrayList<Integer>(); //創建一個空的ArrayList對象

for(int i=0; i<10;i++){

Integer a2 = new Integer(i);//創建一個整型的包裝對象

a1.add(a2);                 //將此對象放入ArrayList當中

}

//拿出

System.out.println("數組中的元素");

for(int i=0;i<a1.size();i++){

Integer a3 = (Integer)(a1.get(i));//獲得ArrayList中索引爲i的元素

System.out.println(a3);

}

System.out.println("------------");

a1.clear();    //清空

System.out.println("數組被清空之後的情況");

System.out.println("數組的長度爲"+a1.size());

if(a1.isEmpty()){       //判斷是否爲空

System.out.println("數組現在爲空");

}else{

System.out.println("數組現在不爲空");

}

}


}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章