day07-常用类-ArrayList-基本类型的存储方式_自动拆装箱
This commit is contained in:
35
day07/src/com/inmind/arraylist03/Demo09.java
Normal file
35
day07/src/com/inmind/arraylist03/Demo09.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package com.inmind.arraylist03;
|
||||
/*
|
||||
ArrayList_基本类型的存储方式_转换为包装类
|
||||
|
||||
自动装箱:int 型的值 -----装箱成Integer类型的对象
|
||||
自动拆箱:Integer类型的对象-----拆箱为int型的值
|
||||
4类8种的基本数据类型不能存放到集合中,因为集合只能存放引用数据类型
|
||||
|
||||
在java中使用包装类,来对基本数据类型进行包装,包装成引用数据类型
|
||||
|
||||
主要记忆int和char的包装类
|
||||
*/
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Demo09 {
|
||||
public static void main(String[] args) {
|
||||
ArrayList<Integer> lists = new ArrayList<>();
|
||||
//以下代码的含义:lists.add(new Integer(1));
|
||||
lists.add(1);//自动装箱:内容为1的Integer对象
|
||||
lists.add(2);
|
||||
lists.add(3);
|
||||
System.out.println(lists);
|
||||
|
||||
Integer i = lists.get(1);
|
||||
int sum = i+10;//自动拆箱:i是Integer对象,拆箱为整型int 值,再参与运算
|
||||
System.out.println(sum);//12
|
||||
//创建一个保存字符的集合
|
||||
ArrayList<Character> list2 = new ArrayList<>();
|
||||
list2.add('a');
|
||||
list2.add('b');
|
||||
list2.add('c');
|
||||
System.out.println(list2);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user