Experiment:
Source:
package exercise;
public class ArrayIndexOutOfBoundsException {
public static void main(String args[]) {
int a[] = null; //定义数组,初始化为null
a = new int[5]; //设置数组大小
System.out.println("程序开始运行"); //程序开始运行
try {
for (int i = 1; i < 10; i++) { //使数组越界
a[i - 1] = i - 2;
}
} catch (Exception e) { //异常类型匹配
System.out.println(e);
System.out.println("数组越界"); //输出类型
} finally {
System.out.println("程序正常结束"); //程序正常结束
}
}
}
Run shot:
experimental ideas: because in the past had problems with an array of cross-border experiment to achieve this question is quite simple, the main treatment is given its own unusual enough.
Source:
~~~
Package Exercise;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class DangerEexception extends Exception {
String massage = null;
public DangerException() {
massage = "bibibi 危险物品:";
}
public String toshow() {
return massage;
}
}
class Goods {
String name;
boolean isDanger = true;
public Goods(String name) {
this.name = name;
}
}
public class Machine {
static ArrayList list;
public Machine(ArrayList list) {
this.list = list;
}
public void ListAdd(String name) {
list.add(name);
}
public void ListRemove(String name) {
if (list != null) {
list.remove(name);
}
}
void cheakBag(Goods goods) throws Myexception {
if (goods.isDanger) {
throw new Myexception();
}
}
public static void main(String args[]) {
String n = null;
int flag = 0;
list = new ArrayList();
Machine machine = new Machine(list);
Goods goods = new Goods(n);
machine.ListAdd("打火机"); //添加初始危险物品
machine.ListAdd("管制刀具");
machine.ListAdd("汽油");
machine.ListAdd("化妆品");
Scanner sc = new Scanner(System.in);
System.out.println("1.添加危險物品");
System.out.println("2.刪除危險物品");
System.out.println("3.物品檢查");
System.out.print("请输入操作类型:");
flag = sc.nextInt();
if (flag == 1) {
System.out.print("请输入添加的物品:" + " ");
String x = sc.next();
machine.ListAdd(x);
System.out.println("开始检查:");
System.out.print("待检查物品:" + " ");
n = sc.next();
if (list.contains(n)) {
try {
machine.cheakBag(goods);
} catch (Myexception e) {
System.out.println(e.toshow() + n);
}
} else {
System.out.println("安全通过");
}
}
if (flag == 2) {
System.out.print("请输入删除的物品:" + " ");
String x = sc.next();
machine.ListRemove(x);
System.out.println("开始检查:");
System.out.print("待检查物品:" + " ");
n = sc.next();
if (list.contains(n)) {
try {
machine.cheakBag(goods);
} catch (Myexception e) {
System.out.println(e.toshow() + n);
}
} else {
System.out.println("安全通过");
}
}
if (flag == 3) {
System.out.println("开始检查:");
System.out.print("待检查物品:" + " ");
n = sc.next();
if (list.contains(n)) {
try {
machine.cheakBag(goods);
} catch (Myexception e) {
System.out.println(e.toshow() + n);
}
} else {
System.out.println("安全通过");
}
}
}
}
~~~
Run Screenshot:
Add:
Delete:
check:
thoughts: The subject teacher in the class I said, but when I got the topic, read a bit, and up to now I still do not know what use goods, if it is just a to determine isDanger is true, then can be defined as String name instead of Goods goods, then Goods class definition, but also saved a name and a isDanger, == not quite understand, should be a low level of knowledge, but still basically achieve the requirements of the subject. As well as in the use of the ArrayList class, the beginning I was directly instantiate a list of the queue, then list.add () added, list.remove () to delete, and then thought, required if to add, and then modify the directly defines a ArrayList lsit property, users can achieve the added and deleted at the end, the code, the flag judgment, emm, the code a bit redundant, but still own optimization slowly, because I think the problem a little trouble, then need to think it over and then make changes or because the technology does not work, we have to strengthen training.
summarize: