Basic knowledge exception handling and arrays

Exception handling, and basic knowledge of the array

Throwable

Error

Programmers do not need this kind of attention is usually generated by the virtual machine and prolapse

Exception

abnormal

RuntimeException: anomaly, an exception occurs during operation, there is no compile-time run-time problems can be dealt with if by enhancing the robustness of the program ... else

Common runtime exception:

空指针异常NullpointerException:当对象无法正常存在使用,会发生空指针异常
类型转换异常ClassCastException:intansceof可以解决
数组下标越界
数组长度负数异常
数学异常ArithmeticException
数字转换异常NumberFormatException

What is unusual

Program can not be executed properly completed

Unusual classification

During operation | during compilation

Abnormal characteristics of different categories:

Exception handling

throws an exception is thrown thrown on a layer of processing, not currently processing

try ... catch exception caught

try{
   		可能会出现异常的代码;
   	}catch(NullPointerException e){ //捕获空指针类型的对象,或者多态
   		如果出现这个异常,执行的代码...
   	}catch(FileNotFoundException e){
   		如果出现这个异常,执行的代码...
   	}....
   	 catch(Exception e){
   		任意类型的异常都可以捕获
   	}finally{
   		无论try中是否出现异常,都会执行finally中的代码
   	}

try in the event of abnormality, the latter will not continue to try code, corresponding directly to the catch, the catch if there are multiple, is determined from top to bottom.

1 may now try a back to a plurality of catch, at least one of

If you have multiple catch, catch the exception types range from small to large to

public class Abnormal {
	public static void main(String[] args) {
		//try中一旦遇到异常try中后面的代码都不会执行
		try {
			System.out.println(5/0);
			System.out.println(Integer.valueOf("123a"));
			InputStream is=new FileInputStream("E:\\test\\code");
		}catch(ArithmeticException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}catch(FileNotFoundException e) {
			e.printStackTrace();
		}catch (Exception e) {
			// TODO: handle exception
			System.out.println("异常处理");
		}finally {
			System.out.println("一般资源的关系都要放在finally中");
		}
		System.out.println("main方法结束");
	}
}

Custom exception

Directly or indirectly inherited from Exception

Runtime exceptions: RuntimeException Inheritance

Compile-time exception: Exception exception subclass compile-time or a certain

public class CustomException {
	public static void main(String[] args) {
		Student stu=new Student();
		stu.setName("小强");
//		try {
//			stu.setAge(-18);
//		} catch (AgeException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
			int age=15;
		if(age>0 && age<100) {
			stu.setAge(age);
		}else {
			stu.setAge(18);
		}
		stu.setWeight(50);
		System.out.println(stu);
	}
}


//class AgeException extends Exception{//编译时异常
class AgeException extends RuntimeException{//运行时异常
	public AgeException() {
		// TODO Auto-generated constructor stub
	}
	
	public AgeException(String str) {
		super(str);
	}
}

class Student{
	private String name;
	private int age;
	private double weight;
	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>0 && age<100) {
			this.age = age;
		}else {
			throw new AgeException(age+"不合法");
		}
	}
	public double getWeight() {
		return weight;
	}
	public void setWeight(double weight) {
		this.weight = weight;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + ", weight=" + weight + "]";
	}
}

Array

Variables: single data storage

Array: storing a plurality of data

It is a contiguous memory space in memory

An ordered set of data of the same data type

It features an array of

  1. A container, a reference data type is an array, the stack
  2. All data in the array requires the same data type
  3. Once the length of the immutable
  4. Orderly, according to numbers using the (index | subscript)

Creating an array

1.数组的声明
	数据类型	变量名;
	数据类型[]	数组名;
	数据类型	数组名[];	不推荐
	数据类型:数组中所有数据的数据类型
	[]	数组
	数组名:标识符
2.初始化
	动态初始化:创建的时候不确定数组中的数据值,可以先创建,后续确定了数据值的时候再赋值
		数据类型[] 数组名 = new 数据类型[长度];
		默认值:
			整形	0
			小数	0.0
			boolean	false|true
			char	' '
			引用数据类型	null
	静态初始化:创建数组的同时赋值
		数据类型[] 数组名 = new 数据类型[]{值1,值2...};
		数据类型[] 数组名 = {值1,值2...};

The operation of the index data array

Array name [subscript | Index]: locate the specified spatial index position in the array, you can assign | get value

数组:1 2 3 4 5 6转置之后的数组:6 5 4 3 2 1(冒泡排序)
public class ArrayTest02 {
	
	public static void main(String[] args) {
		int[] arr=new int[] {1,2,3,4,5,6};
		array(arr);
	}
	
	public static void array(int arr[]) {
		
		for(int i=0;i<arr.length;i++) {
			int num=0;
			for(int j=i+1;j<arr.length;j++) {
				if(arr[i]<arr[j]) {
					num=arr[i];
					arr[i]=arr[j];
					arr[j]=num;
				}
			}
		}
		System.out.println(Arrays.toString(arr));
	}
	
}

The array index of the last data

Array name .length-1

Attributes

.Length data array name this array number

Iterate

Iterate: sequentially take out the data in each data operation

1.for普通for循环
2.增强for循环:for..each
  	for(数据类型  变量名 : 数组名|容器名){
 			变量名 ->代表数组中的从前到后的每一个数据
  	}
public class TestArray {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//定义
		int[] arr=new int[10];
		System.out.println("--------普通for---------");
		for(int i=0;i<arr.length;i++) {
			//赋值
			arr[i]=i;
			//打印
			System.out.print(arr[i]);
		}
		System.out.println("\n-------增强for--------");
		for(int j : arr){		
			System.out.print(arr[j]);
		}
		
	}

}

Arrays contain a lot of method of operation of the array, is a static factory

常用方法:
  		Arrays.toString(数组)  把数组中的内容以字符串的形式返回
  		Arrays.equals(arr1, arr2) 比较两个数组内容是否相等
  		--从索引为0的位置开始
  		static int[] copyOf(int[] original, int newLength)  拷贝,截取,填充的效果
  		--从索引为指定的位置开始
  		static int[] copyOfRange(int[] original, int from, int to) 从原数组的指定位置开始拷贝,拷贝到指定to位置
  			注:结束位置不包含
 		static void fill(int[] a, int val) 数组中的所有位置,使用val数值填充
 		static void fill(int[] a, int fromIndex, int toIndex, int val)  
 		static void sort(int[] a)  对指定的 int 型数组按数字升序进行排序。 
 		static int binarySearch(int[] a, int key)  查找数据出现的索引  返回值: -插入点 - 1
 			使用前提:先升序排序
import java.util.Arrays;

public class TestArrayCommon {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] arr= {3,2,1,4,5,6,7,8,9};
		int[] arr1= {3,2,1,4,5,6,7,8,9};
		System.out.println(arr==arr1);
		System.out.println(Arrays.equals(arr,arr1));
		
		System.out.println((Arrays.toString(Arrays.copyOf(arr, 3))));
		
		System.out.println((Arrays.toString(Arrays.copyOfRange(arr,1,3))));
		
		Arrays.fill(arr1,9);
		System.out.println(Arrays.toString(arr1));
		
		Arrays.fill(arr,1,5,8);
		System.out.println(Arrays.toString(arr));
		//排序默认升序
		Arrays.sort(arr);
		System.out.println(Arrays.toString(arr));
	//当超出数组长度时返回负数	假如数组长度为5,用这个方法拿第10个位置的数,那么返回的负数为-10
		System.out.println(Arrays.binarySearch(arr, 10));
	}

}
Published 13 original articles · won praise 13 · views 507

Guess you like

Origin blog.csdn.net/Rabbit_white_/article/details/103964664