import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
public class arraysAsList {
public static void main(String[] args) {
String [] arrayA = {"Box","Sun","Clock","Phone"};
Integer [] arrayB = {21,27,24,7};
List listStructureA = new ArrayList();
List listStructureB = new ArrayList();
listStructureA = Arrays.asList(arrayA);
listStructureB = Arrays.asList(arrayB);
System.out.println("My first list : " + listStructureA);
System.out.println("Sun = " + listStructureA.get(1));
System.out.println("My second list : " + listStructureB);
System.out.println("24 = " + listStructureB.get(2));
}
}
我意识到 int 是一个原始类型,而 Integer 是一个类。但是在这个脚本中,当我尝试使用 int 而不是 Integer 时,我得到“索引超出范围异常”错误。我之前用int创建数组,int数组和Integer数组有什么区别?提前致谢。