2021-06-22--BUG集

JAVA–BUG集

1、数组下标越界异常

java.lang.ArrayIndexOutOfBoundsExceptio
--------数组下标越界异常

错误原因:
访问了不属于当前数组的下标值,下标值超出数组最大下标

修改方法:
修改for循环变量i的最大值

问题代码:

public class TestMonth {
	public static void main(String[] args) {
		int[] a = {31,28,31,30,31,30,31,31,30,31,30,31};
		for(int i =0;i<=a.length;i++) {//错误处
			System.out.println((i+1) + "月有"+a[i]+"天");
		}
	}
}

改正代码:

public class TestMonth {
	public static void main(String[] args) {
		int[] a = {31,28,31,30,31,30,31,31,30,31,30,31};
		for(int i =0;i<=a.length-1;i++) {//改正处
			System.out.println((i+1) + "月有"+a[i]+"天");
		}
	}
}

2、数组下标越界异常

Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException
----------数组下标越界异常

错误原因:
访问了不属于当前数组的下标值,下标值超出数组最大下标

问题代码:

public class TestMaopao {
    public static void main(String[] args) {
        int[] a = {27,96,73,25,21};
        for(int i =1;i<= a.length-1; i++){
            //数组下标
            for(int j = 0;j<=a.length-1;j++){//错误点
                if (a[j] > a[j + 1]) {
                    int t;
                    t = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = t;
                }
            }
        }
        System.out.println(Arrays.toString(a));
    }
}

改正代码

public class TestMaopao {
    public static void main(String[] args) {
        int[] a = {27,96,73,25,21};
        for(int i =1;i<= a.length-1; i++){
            //数组下标
            for(int j = 0;j<a.length-i;j++){//修改点
                if (a[j] > a[j + 1]) {
                    int t;
                    t = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = t;
                }
            }

        }
        System.out.println(Arrays.toString(a));
    }
}

3.想要循环输入,但是结果却循环打印输出

需求:
猜数字游戏:一个类Guess有一个成员变量v,有一个初值100。有一个 toGuess方法,给方法传一个参数,对Guess类的成员变量v进行猜。如果大了则提示大了,小了则提示小了。等于则提示猜测成功。写一个测试类,让用户来输入数字猜数。

显示错误:
你的答案猜小了
你的答案猜小了
你的答案猜小了

问题代码:

class Guess{
    int v ;
    public void toGuess(){
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入:");
        int num = scan.nextInt();
        for(int i = 1;i<=100;i++){
            if(v >num){
                System.out.println("你的答案猜小了");
            }else if(v<num){
                System.out.println("你的答案猜大了");
            }else{
                System.out.println("恭喜你,猜对了");
                break;
            }
        }
    }
}

改正代码:

class Guess{
    int v ;
    public void toGuess(){
        Scanner scan = new Scanner(System.in);
        for(int i = 1;i<=100;i++){
            System.out.println("请输入:");//要把输入放在循环里面
            int num = scan.nextInt();
            if(v >num){
                System.out.println("你的答案猜小了");
            }else if(v<num){
                System.out.println("你的答案猜大了");
            }else{
                System.out.println("恭喜你,猜对了");
                break;
            }
        }
    }

4、非序列化异常

java.io.NotSerializableException
---------非序列化异常

错误原因:
需要被序列化的类没有创建Serializable接口

改正方法:
在被序列化的类加入接口(implements +接口名)

5、空指针异常

java.lang.NullPointerException
-------------- 空指针异常
问题代码

if(value == 0){//错误处
                map.put(key,1);
            }else {
                map.put(key,value+1);
            }

改正代码:

       if(value == null){//把0改为null
                map.put(key,1);
            }else {
                map.put(key,value+1);
            }

6、无法从静态上下文中引用非静态 变量 s

Non-static field ‘s’ cannot be referenced from a static context
----------无法从静态上下文中引用非静态 变量 s
错误原因:
静态资源只能被静态资源修饰,所以需要把成员变量String修饰为静态

问题代码:

public class Pra{
    String s="One World One Dream";
    public static void main(String args[]){
        System.out.println(s);
    }
}

改正代码:

public class Pra{
    static String s="One World One Dream";
    public static void main(String args[]){
        System.out.println(s);
    }
}

7、方法

(单选题)为AB类的一个无形式参数无返回值的方法method书写方法头,使得使用类名AB作为前缀就可以调用它,该方法头的形式为()
A.static void method( )
B.public void method( )
C.final void method( )
D.abstract void method( )
【正确答案】A
【答案解析】1、public修饰的方法可被其它类访问或引用。 2、abstract修饰的方法是抽象方法,抽象方法没有方法体,要使用抽象方法,必须先实现此抽象方法。 3、final修饰的方法不能被继承。 4、static修饰的方法为静态方法,静态方法不需要类的实例化就可以被类直接调用。 故要使得类名AB可以直接调用method()方法,则必须在method()前用static来修饰。

8、线程

(多选题)有关线程的哪些叙述是对的( )
A.一旦一个线程被创建,它就立即开始运行。
B.使用 start()方法可以使一个线程成为可运行的,但是它不一定立即开始运行。
C.当一个线程因为抢先机制而停止运行,它被放在可运行队列的前面。
D.一个线程可能因为不同的原因停止并进入就绪状态。
【正确答案】B,D
【答案解析】线程被创建后不可能立即开始运行。 不同优先级的线程间是抢先式的,同级线程间是轮转式的。 即使线程因为抢先而停止也不一定就进入可运行队列的前面。 而同级线程是轮换式的,它的运行可能就是因为轮换,而它因抢占而停止后只能在轮换队列中排队而不能排在前面。

9、线程wait()和sleep()

(单选题)代码执行后输出的结果是( )

public class Z{
  public static void main(String[] args){
    new Z();
  }
  Z(){
    Z alias1 = this;
    Z alias2 = this;
    synchronized (alias1){
      try{
        alias2.wait();
        System.out.println("DONE WAITING");
      } catch (InterruptedException e) {
        System.out.println("IN TEEEUPTED");
      } catch (Exception e) {
        System.out.println("OTHER EXCEPTION");
      } finally {
        System.out.println("FINALLY");
      }
    }
    System.out.println("ALL DONE");
  }
}

A.The application compiles but doesn’t print anything.
B.The application compiles and print “DONE WAITING”.
C.The application compiles and print “FINALLY”.
D.The application compiles and print “ALL DONE”.
【正确答案】A
【答案解析】wait方法和sleep方法很类似,都是处于等待状态,但是不同的是,wait方法执行后会释放锁对象,所以此段代码中wait方法后面的语句不会被执行,而是一直处于等待状态中。

10、线程

(单选题)下列有关线程的说法正确的是( )
A.启动一个线程是调用start()方法,是线程所代表的虚拟处理机处于可运行状态,这意味着线程此时就会立即运行。
B.notify()方法可以确切的唤醒某个处于等待状态的线程。
C.wait()方法可以使一个线程处于等待状态,但不会释放所持有对象的锁。
D.sleep()方法使一个正在运行的线程处于睡眠状态,是一个静态方法,调用此方法时,需要捕捉InterruptedException异常。
【正确答案】D
【答案解析】线程调用start方法后不会立即运行此线程,而是将该线程处于就绪状态;notify方法不能确切唤醒某个整处于等待状态的线程,而是需要通过其他线程来调用该等待线程中的notify方法来唤醒该线程;wait方法会释放掉锁。

11、sleep()和 wait()

(单选题)关于 sleep()和 wait(),以下描述错误的一项是( )
A.sleep 是线程类(Thread)的方法,wait 是 Object 类的方法
B.sleep 不释放对象锁,wait 放弃对象锁
C.sleep 暂停线程、但监控状态仍然保持,结束后会自动恢复
D.wait 后进入等待锁定池,只有针对此对象发出 notify 方法后获得对象锁进入运行状态
【正确答案】D
【答案解析】sleep 是线程类(Thread)的方法,导致此线程暂停执行指定时间,给执行机会给其他线程,但是监控 状态依然保持,到时后会自动恢复。调用 sleep 不会释放对象锁。 wait 是 Object 类的方法,对此对象调用 wait 方法导致本线程放弃对象锁,进入等待此对象的等待锁定池,只有针对此对象发出 notify 方法(或 notifyAll)后本线程才进入对象锁定池准备获得对 象锁进入运行状态。

12、类转换异常

java.lang.ClassCastException-------类转换异常
错误原因
Animal类不能转换成Cat类
错误代码:

import java.util.ArrayList;
import java.util.List;

public class Cats {
    public static void main(String args[]) {
        List<Cat> cats = new ArrayList<Cat>();
        cats.add(new Cat());
        Animal b = new Animal();
        Cat a = null;
        if(b instanceof Animal)
            a = ()b;
        if(a != null)
            cats.add(a);
        System.out.println(cats.size() +"cats");
    }
}
class Animal {}
class Cat extends Anima

改正代码:

import java.util.ArrayList;
import java.util.List;

public class Cats {
    public static void main(String args[]) {
        List<Cat> cats = new ArrayList<Cat>();
        cats.add(new Cat());
        Animal b = new Animal();
        Cat a = null;
        if(a != null)
            cats.add(a);
        System.out.println(cats.size() +"cats");
    }
}
class Animal {}
class Cat extends Animal{}

13、.split()–数组下标越界异常

ArrayIndexOutOfBoundsException------数组下标越界异常

问题代码:

public class TestHtml {
    public static void main(String[] args) {
        String url = "http://127.0.0.1:8848/cgb2105/test5.html?name=123&age=12&sex=1&hobby=1&hobby=2&edu=1&date=2021-07-04&submit=%E6%8F%90%E4%BA%A4";
        //问题处(转义字符前不能有空格)
        String[] splitAddress = url.split(" \\?");
        System.out.println(splitAddress[1]);

改正代码:

public class TestHtml {
    public static void main(String[] args) {
        String url = "http://127.0.0.1:8848/cgb2105/test5.html?name=123&age=12&sex=1&hobby=1&hobby=2&edu=1&date=2021-07-04&submit=%E6%8F%90%E4%BA%A4";
        //改正:把空格去掉
        String[] splitAddress = url.split("\\?");
        System.out.println(splitAddress[1]);

14、在这里插入图片描述
问题在于数据库的字符集不是utf-8
所以,只需要把字符集更改成utf-8就可以解决
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值