1、java.lang.Integer
構(gòu)造方法
將基礎(chǔ)類型包裝成類,并且提供一系列操作
并且此時(shí)都是存放在堆上的。
Integer(int value)
Integer(String s)
2、練習(xí)題反思
將String類型“1,2,3;4,5,6,;7,8,9,0”分成整型的二維數(shù)組,
代碼很快就寫好了,但是出現(xiàn)了NullPointerException,一直不明白為什么?
看了一下自己的代碼,定義了整型數(shù)組的引用,但是并沒有申請(qǐng)空間,就往里面存數(shù)據(jù)。所以就出錯(cuò)了。
下次一定要記住,定義了引用要申請(qǐng)了空間,才能往空間里面存放數(shù)據(jù)。
然后在把String 類型的數(shù)轉(zhuǎn)變整型數(shù)據(jù)的時(shí)候最好要處理NUmberFormatException這個(gè)異常。
public class Case1 {
public static void main(String[] args) {
// TODO 自動(dòng)生成的方法存根
String src = "1,2;3,4,5;6,7,8";
String[] sp1 = src.split(";");
Integer in[][] = new Integer[sp1.length][];
for(int i=0; i<sp1.length; i++) {
String[] sp2 = sp1[i].split(",");
in[i] = new Integer[sp2.length];
for(int j=0; j<sp2.length; j++) {
String temp = sp2[j];
try{
in[i][j] = Integer.parseInt(temp);
}catch (NullPointerException e) {
System.out.println("空指針!");
}
}
}
//System.out.println(in[1][0]);
for(int i=0; i<in.length; i++) {
for(int j=0; j<in[i].length; j++) {
System.out.print(in[i][j]+" ");
}
System.out.println();
}
}
}