[]
数组名 = new 数据类型[长度];
[]
数组名 = new 数据类型[]
{值列表}
;public class Class001_Array {
public static void main(String[] args) {
// 声明
int[] arr1;
//动态初始化
arr1 = new int[3];
//静态初始化
double[] arr2 = new double[]{1.1,2.2,3.3,4.4};
String[] arr3 = {"你好","中国"};
//为数组负债
arr1[0] = 101;
arr1[1] = 102;
arr1[2] = 103;
//获取数组中的数据
System.out.println(arr1[0]);
System.out.println(arr1[1]);
System.out.println(arr1[2]);
System.out.println(arr2[0]);
System.out.println(arr2[1]);
System.out.println(arr2[2]);
System.out.println(arr2[3]);
System.out.println(arr3[0]);
System.out.println(arr3[1]);
//变量重新赋值, arr1引用指向的不同的数组
arr1 = new int[5];
arr1 = new int[]{1,2,3};
//arr1 = {1,2,3};
}
}
数组的遍历 :
普通for循环
条件i是索引,需要根据索引操作数组中的数据
增强for循环|for…each
for(数据类型 变量名:数组名|集合名){
变量名 : 存储数组中的每一个数据
}
public class Class002_Each {
public static void main(String[] args) {
char[] arr = {'a','b','c','d'};
//for : i作为索引
for(int i=0;i<=arr.length-1;i++){
System.out.println(arr[i]);
}
//for..each
for(char ch:arr){
System.out.println(ch);
}
}
}
二维数组,是数组中放数组,
声明
数据类型[][] 数组名; -> 推荐
数据类型 数组名[][];
数据类型[] 数组名[];
初始化
操作数组中的数据:
public class Class006_Array {
public static void main(String[] args) {
//动态初始化
int[][] arr1 = new int[3][2];
//为二维数组赋值
arr1[0][0] = 1;
arr1[0][1] = 2;
arr1[1][0] = 3;
arr1[1][1] = 4;
arr1[2][0] = 5;
arr1[2][1] = 6;
//获取
System.out.println(arr1[0][0]);
System.out.println(arr1[0][1]);
System.out.println(arr1[1][0]);
System.out.println(arr1[1][1]);
System.out.println(arr1[2][0]);
System.out.println(arr1[2][1]);
double[][] arr2 = new double[2][];
arr2[0] = new double[2];
arr2[0][0] = 0.1;
arr2[0][1] = 0.2;
arr2[1] = new double[]{0.3,0.4,0.5};
System.out.println(arr2[0][0]);
System.out.println(arr2[0][1]);
System.out.println(arr2[1][0]);
System.out.println(arr2[1][1]);
System.out.println(arr2[1][2]);
//静态初始化
//char[][] arr3 = new char[][]{{'1','2','3'},{'4','5'},{'6'}};
char[][] arr3 = {{'1','2','3'},{'4','5'},{'6'}};
System.out.println(arr3[0][0]);
System.out.println(arr3[0][1]);
System.out.println(arr3[0][2]);
}
}
public class Class007_Array {
public static void main(String[] args) {
int[][] arr = {{3,2,1},{7,8},{9}};
//普通for嵌套增强
//i 作为二维数组的索引
for(int i=0;i< arr.length;i++){
for(int num:arr[i]){
System.out.println(num);
}
}
//增强for嵌套增强
for(int[] array:arr){
for(int i:array){
System.out.println(i);
}
}
System.out.println(arr[3]);
}
}
程序运行时,发生的不被期望的事件,它阻止了程序按照程序员的预期正常执行,这就是异常。“程序生病了”。
异常体系 :
Throwable
/
Error Exception
/
Runtime Checked
Exception Exception
常见的运行时异常:
1.空指针异常 NullPointerException
2.数组索引越界异常 ArrayIndexOutOfBoundsException
字符串索引越界异常 StringIndexOutOfBoundsException
索引越界异常 IndexOutOfBoundsException
3.数学异常 ArithmeticException
4.类型转换异常 ClassCastException
5.数字转格式异常 NumberFormatException
注意:
public class Class001_Exception {
public static void main(String[] args) {
//System.out.println(5/0);
String str = null;
if(str!=null){
System.out.println(str.length());
}
str = "123abc";
System.out.println(Integer.valueOf(str));
//System.out.println(str.charAt(5));
int[] arr = {1,2,3};
System.out.println(arr[3]);
}
}
异常处理方案 :
1.异常抛出 throws
把异常抛出到上一层处理
2.异常捕获
try {
有可能出现异常的代码段;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
} ...
catch (Exception e) { --> 接盘侠
e.printStackTrace();
} finally{
一般为资源的关闭等等
}
public class Class002_Exception {
public static void main(String[] args){
System.out.println("main方法开始");
try {
System.out.println("try开始");
haha();
System.out.println("try结束");
} catch (FileNotFoundException e) {
System.out.println("出现文件未找到异常喽,处理了");
e.printStackTrace(); //打印异常的提示信息
} finally {
System.out.println("最后的最后,我们都要离开....");
}
System.out.println("main方法结束");
}
public static void haha() throws FileNotFoundException {
test();
}
public static void test() throws FileNotFoundException {
InputStream is = new FileInputStream("D://xixi.txt");
}
}
public class Class003_Exception {
public static void main(String[] args){
User user = new User();
user.setName("棒棒糖的爱");
int age = 25;
//运行时
/*if(age>=18 && age<=150){
user.setAge(age);
}else{
user.setAge(18);
}*/
//编译时
try {
user.setAge(15);
} catch (AgeException e) {
e.printStackTrace();
try {
user.setAge(18);
} catch (AgeException ageException) {
ageException.printStackTrace();
}
}
System.out.println(user);
System.out.println("main方法结束了...");
}
}
//年龄不合法异常
//class AgeException extends RuntimeException{ //运行时异常
class AgeException extends Exception{ //编译时异常
public AgeException() {
}
public AgeException(String message) {
super(message);
}
}
class User{
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
public User() {
}
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<18 || age>150){
//制造一个异常
throw new AgeException(age+"不合法了...");
}
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}