您的位置:首页 > 编程语言 > Java开发

Java 数组 异常 exception 及其练习

2017-03-23 17:36 309 查看
学习心得
一、专业课
1数组
1.1、数组可以看成是多个相同数据类型数据的组合,对这些数据的统一管理。
1.2、数组变量属引用类型,数组也可以看成是对象,数组中的每个元素相当于该对
象的成员变量。
1.3、数组中的元素可以是任何类型,包括基本类型和引用类型



2.一维数组
//数组
int[]arr = {1,2};
//引用类型对象
//==比较堆地址
/**
* equals
来自Object默认也是比较
* public boolean equals(Object
obj) {
return (this ==obj);
}
但我们可以重写hashCode与equals,使得equals可以比较数组里
的内容
hashCode相等不一定equals返回TRUE
equals不相等,hashCode不一定相等,因为重写之后我们可以让
它们一方返回固定的值这么狡猾
*/
Personperson =
new Person("dd", 12);
Personperson2 =
new Person("dd", 12);
System.out.println(person== person2);
System.out.println(person.equals(person2));
Objectobject =
new Object();
Objectobject2 =
new Object();
System.out.println(object.equals(object2));
System.out.println(person.hashCode());
System.out.println(person2.hashCode());
之下结果为重写了hashCode与equals
false
true
false
4533
4533
3.二维数组
3.1arr.clone();可以实现数组克隆但是值得注意的是
int[][]arr = new int[2][];
int[]a=
arr[1].clone();
这时一维数组需要先声明空间
不然NullPointerException

Arrays.deepEquals(arr,a);两个都必须是二维或以上数组

Arrays.equals(a,a2);当两个都是一维数组时,才能准确比较出数组内容是否相同

System.arraycopy(src,srcPos,dest,destPos,length);可以实现数组拷贝

4.异常





4.1Throwable有两个重要的子类:Exception(异常)和Error(错误)

Error(错误):是程序无法处理的错误,表示运行应用程序中较严重问题。

Exception(异常):是程序本身可以处理的异常。

Java的异常(包括Exception和Error)分为可查的异常(checkedexceptions)和不可查的异常(uncheckedexceptions

 运行时异常:都是RuntimeException类及其子类异常

5.捕获异常
try{
//可能会发生异常的程序代码
}catch(Type1id1){
/捕获并处置try抛出的异常类型Type1
}
catch(Type2id2){
//捕获并处置try抛出的异常类型Type2
}

一旦某个catch捕获到匹配的异常类型,将进入异常处理代码。一经处理结束,就意味着整个try-catch语句结束。其他的catch子句不再有匹配和捕获异常类型的机会

上方的异常类型不能比上方的异常

trycatch-finally语句

try{
//可能会发生异常的程序代码
}catch(Type1id1){
/捕获并处置try抛出的异常类型Type1
}
catch(Type2id2){
//捕获并处置try抛出的异常类型Type2
}finally{

//无论是否发生异常,都将执行的语句块
}



二、小组PK
1.我方题目
1.以下代码若能运行,请输出运行结果,若不能运行,请说明原因:
publicclass Demo01 {
publicstatic void main(String[] args) {
Demo02demo = new Demo02(3);
Demo03demo1 = new Demo03(3);
demo.method();
demo1.method();
demo1.method(3);
System.out.println(demo.a);
System.out.println(demo1.a);
}
}
classDemo02{
inta;
publicDemo02(int a){ this.a = a;}
finalvoid method(){
for(inti=0;i<10;i++){
a=i;
}
}
}
classDemo03 extends Demo02{
inta;
publicDemo03(int a){ this.a = a;}
finalvoid method(int i){
for(intj=0;j<i;j++){
a+=j;
}
}
}

答案:编译报错,Demo03在实例化对象时,在它自身的构造方法的第一条语句默认
调用super();
这里父类Demo02已经定义了带参数的构造方法,不会自动生成无参构造方法,编译
报错。

2.若能运行,请输出运行结果,若不能运行,请说明原因
importjava.util.Arrays;
publicclass Test1 {
publicstatic void main(String[] args) {
int[]is = {1,2,3,4,5};

int[]is1 = is.clone();

is[1]= 10;

System.out.println(Arrays.deepEquals(is,is1));
Bugb = new Asd();
Asdc = new Asd();
b.fun1(2);
try{
c.fun("c");
}catch (Exception e) {
e.printStackTrace();
}

}
}
classBug{

Stringname;
intid;

privatevoid fun(String name) throws RuntimeException{
System.out.println("time:"+ this.name);
}

publicvoid fun1(int a){
System.out.println(a++);
}

}
classAsd extends Bug{
privateint a;

publicvoid fun1(int c){
System.out.println(c+a);
}
voidfun(String name) throws Exception{
System.out.println("time:"+ this.name);
}
}

答案:编译错误,deepEquals方法的参数是二维数组所以编译错误,父类的fun()
方法是私有的,所以子类的fun()方法不算重写不会报错

3、以下代码若能运行,请输出运行结果,若不能运行,请说明原因:
publicclass Test {
staticFloat f;
publicTest(Float f){
this.f= f++;
}
static{
f= 3.0f;
}
publicstatic void main(String[] args) {

Stringstr = "ABCDCDG";
char[]ch = str.toCharArray();
inti = 0;
for(;i >= 0.0 && i<(ch.length/2);i++){
if(ch[i]!= ch[ch.length-1-i]){
System.out.print(ch[i]);
continue;
}else{
System.out.print(ch[ch.length-2-i]);
break;
}
}
Testt1 = new Test(3.3f);
Testt2 = new Test(4.3f);
System.out.println(ch[i++]);
System.out.println(f);

}
}

答案:
ABDC
4.3
解析:0在计算机中和0.0相等,计算机中输出float类型的值不会输出后缀f。

4.以下程序能否运行
如果不能请指出错在那里,
如果可以请输出程序结果:
publicclass TestA {
TestAbA = new TestA();
static{
System.out.println("static");
}

publicTestA() {
System.out.println("TestA");
}

publicstatic void main(String[] args) {
System.out.println("main");
TestAa = new TestA();
}

}

答案:
static
main
《程序死循环》//然后程序进入死循环,因为TestA的成员变量TestA
bA =new TestA();从新再生成的类中又有成员变量TestAbA = new TestA();再次生成新的TestA,如此循环

5.程序是否能够运行,如果能运行结果是什么?不能请说明理由
注意输出格式
publicclass Test04 {
staticint a;
publicstatic void main(String[] args) {
try{
System.out.println(fun(a));
}catch (Exception e) {
System.out.println(9);
}
}
static{
System.out.println(a);
}
publicstatic int fun(int a) throws Exception{
int[]arr = {1,2};
try{
for(inti=0;true;i+=3){
arr[i]/=i;
}
}catch (ArithmeticException e) {
System.out.println(2);
return3;
}catch(NullPointerException e) {
System.out.println(4);
return5;
}catch (ArrayIndexOutOfBoundsException e) {
System.out.println(6);
return7;
}finally{
System.out.println(8);
for(inti=0; i<arr.length;i++){
System.out.print(arr[i]);
}
}
}
}

答案:
0
2
8
123

2、对方题目
1、packagecom.day12;

publicclass Test extends Subject{
publicint b;
publicstatic void main(String arg[]){
Subjects = new Subject();
Testt = new Test();
Subjects1 = new Subject(1, 2);
int K;
//在此处插入选项中的的代码

}
}
classSubject {
privateint a;
int b=100;

public Subject(int a, int b) {
super();
this.a= a;
this.b= b;
}

publicSubject() {
super();
}

publicint test(int m){return m;}
}
//下面哪些放在“在此处插入选项中的的代码?”行是正确的?BC
E
//A,K=
s.a;
//B,K = s1.b;
//C,K = s.test(30);
//D,K = m;
//E,K = t.b;
答案:BCE

2、classE1
extends Exception{}
classE2 extends E1{}
classTestParent{
publicvoid fun(boolean f) throws E1{}
}
public class Testzi extends TestParent{

//here
}

不定项选择
以下方法可以放在“//here”位置,而且通过编译的是(AD)
A:publicvoid fun(boolean f) {}

B:publicvoid fun(boolean f) throws Exception{}

C:publicvoid fun(boolean f) throws E1{}

D:publicvoid fun(boolean f) throws E1,E2{}

E:publicvoid fun(boolean f) throws E2{}
正确答案
ACDE

3.publicclass Demo03 {
publicstatic void main(String[] args) {
intn = 9;
int[][]arr = new int

;
for(int i = 0; i < n-1; i++) {
arr[i][0]= 1;
1. System.out.print(_arr[i][0]_+" "_______________);

2. for (int _j=0_______;___j<i_&& if(i == j){ arr[i][j] =1;System.out.print(arr[i][j] + " ");}___ ;j++ ) {

arr[i][j]= arr[i-1][j-1] + arr[i-1][j];
System.out.print(arr[i][j]+ " ");
}
System.out.println();
}
}
}

用二维数组的方式将杨辉三角形打印出来,请补充1,2两处_____________(注意两个数间“”空格隔开)
1
11
12 1

13 3 1
14 6 4 1
15 10 10 5 1

intn = 9;
// int[][]arr = new int

;
// for(int i = 0; i < n-1; i++) {
// arr[i][0]= 1;
// System.out.print(1+"");
// for(int j = 1; j < i+1; j++) {
// arr[i][j]= arr[i-1][j-1] + arr[i-1][j];
// System.out.print(arr[i][j]+ " ");
// }
// System.out.println();
// }

4.
publicstatic void main(String[] args) {
int[][]a = {{1,2},{3,4},{5,6}};

a[1]= a[0];
a[2]= a[1].clone();
a[0]= a[2];
a[0][0]=0;
for(inti = 0; i < 3; i++){
for(intj = 0 ; j < 2; j++){
System.out.print(a[i][j]);
}
System.out.println();
}
}

运行结果:
02
12
02

5.
publicclass Test {

publicstatic void main(String[] args) {
inta;
intj = 2;
inti = 0;
do{
a= j++;
}while(i++<2);
System.out.println(a);
}

}

运行结果?如果发生错误,请指出错在哪里?
4

学习心得:
1.不要强求不可知,要从已知推未知。

黄品民
2017323
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 数组 异常 exception
相关文章推荐