日期:2014-05-20 浏览次数:21000 次
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个位置