Skip to content

Instantly share code, notes, and snippets.

@edwardyi
Last active February 12, 2019 07:04
Show Gist options
  • Save edwardyi/88d2772df5b85e208f42473e75d09a50 to your computer and use it in GitHub Desktop.
Save edwardyi/88d2772df5b85e208f42473e75d09a50 to your computer and use it in GitHub Desktop.
Java ArrayList vs Integer Array
// 沒有加會有小紅線提示
import java.util.ArrayList;
public class Test{
public static void test1{
int[] arr = new int[10];
arr[0] = 2;
arr[1] = 3;
for(int i=0; i< arr.length; i++){
System.out.println(arr[i]);
}
}
public static void main(String[] args){
// 沒有指定型別的話會出現小黃線
ArrayList<Integer> arr = new ArrayList<Integer>();
arr.add(1);
arr.add(3);
arr.add(5);
arr.remove(1); //移除第二個元素
for(int i=0; i<arr.size(); i++){
// 使用get來取得陣列的值
System.out.println(arr.get(i));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment