Java_06 Difference between Array and ArrayList
1. The difference between the two
Array: The size of the space is fixed.
List (ArrayList): The space grows dynamically.
2. Array
public class _8数组对象 {
public static void main(String[] args) {
//数组初始化
int[] a1 = new int[4]; //a1所有的所有元素值都是0
//遍历数组
int[] arr1 = {1,2,3,4}; //创建的时候可以初始化,并指定了每个元素的值。
for (int i = 0;i<arr.length;i++)
{
System.out.println(arr1[i] + " ");
}
}
}
Results of the:
3. List
import java.util.*;
public class _48列表 {
public static void main(String[] args)
{
System.out.println("list的添加、获取和删除元素");
//ArrayList<String>肯定是List<String>的子类。
//类型 变量 实例类型
List<String> animal = new ArrayList<String>(); //List<String>为什么这么书写看一下_49泛型
animal.add("cat");
animal.add("dog");
animal.add("pig");
animal.add("horse");
animal.remove(3);//根据索引来删除
animal.remove("pig");//根据值来删除
//遍历方式一:
for(String str:animal){
System.out.println(str);
}
//遍历方式二:
System.out.println("另一种遍历:");
for (int i=0;i<animal.size();i++){
System.out.println(animal.get(i));
}
}
}
Results of the: