創建數組,數組轉list,在循環中刪除列表(list)元素
package test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class Test {
public static void main(String[] args) {
// 第一種方法
int[] arrInt = new int[5];// 創建長度爲5的空數組
System.out.println(arrInt.length);
// 第二種方法
int[] arrIntA = {};// 創建長度爲0的空數組
System.out.println(arrIntA.length);
// 第二種方法擴展
int[] arrIntAA = {1,2,3,4,5};// 創建長度爲5的有值的數組
System.out.println(arrIntAA.length);
// 第三種方法
int[] arrIntB = new int[]{1,2,3,4,5};// 創建長度爲5的有值的數組
System.out.println(arrIntB.length);
// 數組轉列表集合測試
System.out.println("-------------------------");
String[] Arr = {"5","7","9","3","1","0"};// 創建長度爲5的有值的數組
System.out.println("長度 : " + Arr.length);
//列表集合
List<String> list = new ArrayList<String>(Arrays.asList(Arr));
System.out.println(list);
// 查看某個數組中是否包含某個值
boolean resBoolean = Arrays.asList(Arr).contains("0");
System.out.println(resBoolean);
// 在循環中刪除列表元素
System.out.println("由Arr數組轉換後的列表 : " + list);
Iterator<String> iter = list.iterator();
while(iter.hasNext()){
String str = iter.next();
if(str.equals("0")){
iter.remove();
}
}
System.out.println("在循環中刪除元素後的列表元素 : " + list);
}
}