日期:2014-05-20  浏览次数:20770 次

菜鸟求教
题目:
用Math类中的随机函数产生10000个随机数并存于数组。
  从键盘接收一个数x,然后用多线程并发查找x在数组中的所有下标位置。
  查找线程的构造函数形参如下所示:
  数组名, 查找范围(起始下标、结束下标)
  每个线程体在查找范围内顺序查找,并将该范围内所有找到的x的下标记录到共享的一个内存缓冲区。
  创建上述线程类的四个实体对象,用四个线程将数组分成不重叠的4段进行查找,如果查找失败,则内存缓冲区为空,输出相应信息;否则顺序输出缓冲区中的所有下标位置(注意:这些下标位置并不是由小到大顺序排列的)。  
我的程序:
import java.util.Random ;
import java.util.Scanner;
class search extends Thread
{
public int a[] = new int[100];
public int n;
public int kaishi;
public int jiesu;
public search(String name,int kaishi,int jiesu){
super(name);
this.kaishi = kaishi;
this.jiesu = jiesu;
}
public void run()
{
for (;kaishi<jiesu ;kaishi++ )
{
if(a[kaishi] == n)
System.out.println(""+getName()+"要查找的元素在"+(kaishi+1)+"个位置");
}
}
}
public class gan3_2{
public static void main(String args[]){
Random r = new Random() ;
/* for(int j=0;j<100;j++){
System.out.print(r.nextInt(10)+ "\t") ;
}**/
int a[] = new int[100];
for(int j=0;j<100;j++){
a[j] = r.nextInt(10);
}
for(int j=0;j<100;j++){
System.out.print(a[j]+ "\t") ;}
System.out.println("请输入要查找的数:");
Scanner in = new Scanner(System.in);
int n = in.nextInt();
search s0 = new search("查找线程1正在查找 ",0,25);
search s1 = new search("查找线程2正在查找 ",25,50);
search s2 = new search("查找线程3正在查找 ",50,75);
search s3 = new search("查找线程4正在查找 ",75,100);
s0.start();
s1.start();
s2.start();
s3.start();
/* for (int i = 0;i<100 ;i++ )
{
if(a[i]==n){
System.out.println("要查找的元素在"+(i+1)+"个位置");
}
}**/
}
};



------解决方案--------------------
Java code

import java.util.Random;
import java.util.Scanner;

class search extends Thread {
    public int a[] = new int[100];//对象值呢?

    public int n;//对象值呢?

    public int kaishi;

    public int jiesu;

    public search(String name, int kaishi, int jiesu,int n,int[] a) {
        super(name);
        this.kaishi = kaishi;
        this.jiesu = jiesu;
        this.n = n;
        this.a = a;
    }

    public void run() {
        for (int i=kaishi; i < jiesu; i++){//这里也改了,还是改下好
            if (a[i]==n)
                System.out.println("" + getName() + "要查找的元素在" + (i + 1)
                        + "个位置");
        }
    }
}

public class gan3_2 {
    public static void main(String args[]) {
        Random r = new Random();
        /***********************************************************************
         * for(int j=0;j<100;j++){ System.out.print(r.nextInt(10)+ "\t") ; }
         **********************************************************************/
        int a[] = new int[100];
        for (int j = 0; j < 100; j++) {
            a[j] =Math.abs(r.nextInt()%10);
        }
        int count=0;
        for (int j = 0; j < 100; j++) {
            System.out.print(a[j] + "\t");
            count++;
            if(count%10==0){//格式搞好一点。。。
                System.out.println();
            }
        }
        System.out.println("请输入要查找的数:");
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        search s0 = new search("查找线程1正在查找 ", 0,25,n,a);//参数要传全
        search s1 = new search("查找线程2正在查找 ", 26,50,n,a);//参数要传全
        search s2 = new search("查找线程3正在查找 ", 51,75,n,a);//参数要传全
        search s3 = new search("查找线程4正在查找 ", 76,100,n,a);//参数要传全
        s0.start();
        s1.start();
        s2.start();
        s3.start();
        /***********************************************************************
         * for (int i = 0;i<100 ;i++ ) { if(a[i]==n){
         * System.out.println("要查找的元素在"+(i+1)+"个位置"); } }
         **********************************************************************/
    }
};
8    9    1    7    5    3    0    0    3    4    
0    9    8    0    0    9    8    0    4    2    
5    2    0    4    0    9    1    3    0    2    
5    1    0    7    9    9    2    3    4    6    
8    5    0    5    2    5    5    2    6    7    
2    4    6    3    5    1    4    3    0    1    
5    0    6    3    4    2    2    6    7    0    
7    2    8    9    3    5    2    0    0    4    
9    0    2    3    1    8    6    3    8    1    
1    9    7    3    4    0    4    4    8    7    
请输入要查找的数:
1
1
查找线程1正在查找 要查找的元素在3个位置
查找线程4正在查找 要查找的元素在85个位置
查找线程4正在查找 要查找的元素在90个位置
查找线程4正在查找 要查找的元素在91个位置
查找线程3正在查找 要查找的元素在56个位置
查找线程3正在查找 要查找的元素在60个位置
查找线程2正在查找 要查找的元素在27个位置
查找线程2正在查找 要查找的元素在32个位置