ArrayList集合存储基本数据类型---Java

如果希望向集合ArrayList当中存储基本类型数据,必须使用基本类型对应的“包装类”。

基本类型 包装类(引用类型,包装类都位于java.lang包下)
byte        Byte
short       Short
int         Integer     【特殊】
long        Long
float       Float
double      Double
char        Character   【特殊】
boolean     Boolean
JDK 1.5+开始,支持自动装箱、自动拆箱。

自动装箱:基本类型 --> 包装类型
自动拆箱:包装类型 --> 基本类型

 1 public class Demo05ArrayListBasic {
 2 
 3     public static void main(String[] args) {
 4         ArrayList<String> listA = new ArrayList<>();
 5         // 错误写法!泛型只能是引用类型,不能是基本类型
 6 //        ArrayList<int> listB = new ArrayList<>();
 7 
 8         ArrayList<Integer> listC = new ArrayList<>();
 9         listC.add(100);
10         listC.add(200);
11         System.out.println(listC); // [100, 200]
12 
13         int num = listC.get(1);
14         System.out.println("第1号元素是:" + num);
15     }
16 
17 }
存储基本数据类型


 
原文地址:https://www.cnblogs.com/zeon/p/13339983.html