import java.util.*;
public class test02 implements Runnable{
private int i=0;
private int []a;
private Thread h1;
test02(){
a=new int[6];
h1=new Thread();
}
public void run(){
System.out.println("---------");
this.i++;
Random a=new Random();
this.a[this.i]=a.nextInt(9);
System.out.println("---------");
System.out.println(a);
}

public String toString(){

return a.toString();
}
public static void main(String[] args) throws InterruptedException{
new test02().h1.start();
Thread.sleep(1000*6);
System.out.println("game over!");
}
}
这个程序中怎么输不出数组a的字符串??

解决方案 »

  1.   


    package test;import java.util.Random;public class test02 implements Runnable {
    private int i = 0;
    private int[] a;
    private Thread h1; test02() {
    a = new int[6];
    h1 = new Thread(this);// 需要给线程传入实现Runnable接口的实现类
    } public void run() {
    System.out.println("---------");
    this.i++;
    Random a = new Random();
    this.a[this.i] = a.nextInt(9);
    System.out.println("---------");
    System.out.println(this.a[this.i]);// 输出数组中格的某个值。如果全输出run方法需要用循环
    } public String toString() { return a.toString();
    } public static void main(String[] args) throws InterruptedException {
    new test02().h1.start();
    Thread.sleep(1000 * 6);
    System.out.println("game over!");
    }
    }
      

  2.   

    implements Runnable改成 extends Thread试试
      

  3.   

    import java.util.*;public class Test implements Runnable {
    private int i = 0;
    private int[] a;
    private Thread h1; Test() {
    a = new int[6];
    h1 = new Thread(this);    //加上个this试试
    }  public void run() {
    System.out.println("---------");
    this.i++;
    Random a = new Random();
    this.a[this.i] = a.nextInt(9);
    System.out.println("---------");
    System.out.println(a);
    } public String toString() { return a.toString();
    } public static void main(String[] args) throws InterruptedException {
    new Test().h1.start();
    Thread.sleep(1000 * 6);
    System.out.println("game over!");
    }
    }