java 二维数组_Java中二维数组和异常的内容及应用
一、二維數組
數組中嵌套數組就是二維數組
二維數組的聲明
數據類型[][] 數組名; --推薦
數據類型 數組名[][];
初始化:
動態:
數據類型[][] 數組名 = new 數據類型[一維的長度][二維的長度];--每一個第二位的小數組長度相同
數據類型[][] 數組名 = new 數據類型[一維的長度][];--第二位的每一個小數組的長度可以不同,第二位的小數組還沒有創建
每個數組 arr[外層數組索引]=new 數據類型[長度] |...一維數組創建方式
靜態:
數據類型[][] 數組名 = new 數據類型[][]{{1,2},{1},{1,2,3}...};
數據類型[][] 數組名 = {{1,2},{1},{1,2,3}...};
二維數組的遍歷:
雙重for循環嵌套
1、例子
public class ArrayDemo02 {
public static void main(String[] args) {
//二維數組的聲明
int[][] arr;
//動態初始化1
arr=new int[2][3];
//賦值
arr[0][0]=1;
arr[0][1]=2;
arr[0][2]=3;
arr[1][0]=4;
arr[1][1]=5;
arr[1][2]=6;
//根據索引獲取值
System.out.println(arr[1][2]);
//動態初始化2
String[][] arr2=new String[3][];
arr2[0]=new String[2];
arr2[0][0]="英";
arr2[0][1]="雄";
arr2[1]=new String[]{"教案"};
arr2[2]=new String[]{"哈哈","呵呵","嘻嘻"};
//arr2[2]={"哈哈","呵呵","嘻嘻"}; 不能使用這種簡易方式創建第二維數組
System.out.println("------遍歷1----------");
//i是外層第一維數組的索引
for(int i=0;i<arr2.length;i++){
for(String s:arr2[i]){
System.out.println(s);
}
}
System.out.println("---------遍歷2-----------");
for(String[] a:arr2){
for(String s:a){
System.out.println(s);
}
}
System.out.println("---------通過遍歷賦值arr數組-----------");
int num=101;
/*for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
arr[i][j]=num;
num++;
}
}*/
for(int[] i:arr){
for(int j=0;j<i.length;j++){
i[j]=num++;
}
}
System.out.println("---------遍歷剛剛賦值的數組arr-----------");
for(int[] a:arr){
for(int s:a){
System.out.println(s);
}
}
//靜態初始化1
int[][] arr3 = new int[][]{{1,2},{1},{1,2,3}};
for(int[] a:arr3){
for(int s:a){
System.out.println(s);
}
}
//靜態初始化2
int[][] arr4 ={{1,2},{1},{1,2,3}};
print(new int[][]{{1,2},{1},{1,2,3}});
}
public static void print(int[][] arr){
for(int[] a:arr){
for(int s:a){
System.out.println(s);
}
}
}
}
二、異常
1、異常:
程序生病了
Throwable
/
Error Exception
/
CheckedException RuntimeException
Error:這類錯誤一般是虛擬機生產或脫出的,程序員無法控制,不需要管
Exception:
檢查時異常|編譯時異常 CheckedException:如果遇到,必須進行處理,否則程序無法運行,發生在編譯期
運行時異常 RuntimeException:程序運行才知道是否有異常 增強程序的健壯性處理 if..
常見的一些運行時異常:
1.空指針異常NullPointerException
2.數組下標越界 ArrayIndexOutOfBoundsException
3.負數異常 NegativeArraySizeException
4.數學異常 ArithmeticException
5.字符串索引越界異常 StringIndexOutOfBoundsException
1、代碼
public class ExceptionDemo04 {
public static void main(String[] args) {
String str=null;
//增強程序的健壯性
if(str!=null){
str.charAt(3);
}else{
str="haha";
str.charAt(6);
}
//ArrayIndexOutOfBoundsException
/*int[] arr=new int[3];
System.out.println(arr[5]);*/
int[] arr=new int[-3];
System.out.println(5/0);
//編譯時異常
InputStream is=new FileInputStream("D:/test.txt");
}
}
2、throw :
制造異常
異常處理方式:
拋出異常: throws 把異常拋到上一層
捕獲異常:
try{
可能會出現異常的代碼
}catch(ClassNotFoundException e){ //= new ClassNotFoundException();
如果出現ClassNotFoundException類型的異常,執行這里的代碼....
}catch(NullPointerException e){
如果出現NullPointerException異常,執行這里的代碼
}...
catch(Exception e){
接收除了以上的其他的異常,執行這里的代碼
}finally{
無論try中的代碼是否出現異常,finally中這里的內容一定會執行
}
注意:
1.try后面可以接一個到多個catch,捕獲不同類型的異常
2.把大范圍的catch寫在最后,小范圍寫在前面,否則永遠執行不到
3.如果try中一旦出現異常,try中的后面的代碼都不會執行,執行對應catch中的代碼
(1)代碼
public class ExceptionDemo05 {
public static void main(String[] args) {
try{
String[] s=null;
System.out.println(s[1]);
InputStream is=new FileInputStream("D:/test.txt");
System.out.println(5/0);
System.out.println("沒有出現異常");
}catch(NullPointerException e){
e.printStackTrace();
System.out.println(e.getMessage());
System.out.println("出現了空指針啦");
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("出現了文件未找到啦");
} catch(Exception e){
e.printStackTrace();
System.out.println("出現異常啦");
} finally{
System.out.println("一定會執行的代碼");
}
//如果代碼一旦出現異常,后面的代碼執行不了
System.out.println("哈哈");
System.out.println("哈哈");
System.out.println("哈哈");
System.out.println("哈哈");
System.out.println("哈哈");
System.out.println("哈哈");
new Demo().hehe(4);
}
}
class Demo{
void test() throws ClassNotFoundException{
throw new ClassNotFoundException();
}
void haha() throws NullPointerException, ClassNotFoundException{
test();
}
void hehe(int a){
try {
if(a==5){
return;
}
String[] s=null;
System.out.println(s[1]);
} catch (NullPointerException e) {
e.printStackTrace();
}finally{
System.out.println("一定會執行的代碼");
}
System.out.println("123235435464576");
}
}
//如果存在方法的重寫, 子類重寫方法拋出的異常<=父類的方法拋出的異常
class Fu{
void test() throws ClassNotFoundException{
}
}
class Zi extends Fu{
void test() throws ClassNotFoundException{
}
}
3、自定義異常:
除了java提供的異常類以外,可以自定義異常
學習異常:
1.異常分類,特點
2.異常處理 ***
編譯時異常:1)throws 2)try..catch
運行時異常:1)增強程序健壯性 2)throws 3)try..catch
3.自定義異常,使用
(1)代碼
public class DefinedException {
public static void main(String[] args) {
Person p=new Person();
p.setName("彭于晏");
try{
p.setAge(-35);
}catch(AgeException e){
System.out.println(e.getMessage());
e.printStackTrace();
}
System.out.println(p);
}
}
//自定義異常 繼承異常類 Exception-->編譯時異常 繼承自RuntimeException-->運行時異常
class AgeException extends Exception{
//class AgeException extends RuntimeException{
private String message;
public AgeException() {
// TODO Auto-generated constructor stub
}
public AgeException(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
}
class Person{
private String name;
private int age;
public Person() {
// TODO Auto-generated constructor stub
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) throws AgeException {
if(age>=1 && age<=150){
this.age = age;
}else{
throw new AgeException(age+"年齡不合法啦");
}
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
總結
以上是生活随笔為你收集整理的java 二维数组_Java中二维数组和异常的内容及应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java下拉菜单_Web前端和Java开
- 下一篇: java 下载二进制文件_使用Java从