我才学多线程,有很多东西不懂,下面的程序不知道错在哪里,请高手指点一下,谢谢!
public class paixu
{ public int[] text;
public int n=text.length; public paixu(int[] text)
{
this.text=text;
}
class insert extends Thread //插入排序
{ int i,j,temp;
private int[] text;
public insert(int[] text)
{
this.text=text;
}
public void run()
{ for(i=0;i<n-1;i++)
{ temp=text[i+1];
j=1;
while(j>-1&&temp<=text[j])
{ text[j+1]=text[j];
j--;
}
text[j+1]=temp;
}
for(int i=0;i<n;i++)
System.out.println(text[i]+"");
}
}
class maopao extends Thread //冒泡排序
{
private int[] text;
int i,j,flag=1;
int temp;
public maopao(int[] text)
{
this.text=text;
}
public void run()
{ for(i=1;i<n&&flag==1;i++)
{ flag=0;
for(j=0;j<n-i;j++)
{ if(text[j]>text[j+1])
{ flag=1;
temp=text[j];
text[j]=text[j+1];
text[j+1]=temp;
}
}
}
for(int i=0;i<n;i++)
System.out.println(text[i]+"");
}
}
public static void main(String args[])
{ int[] text={20,50,12,2,30};
new paixu(text);
(new insert(text)).start();
(new maopao(text)).start();
}
}
public class paixu
{ public int[] text;
public int n=text.length; public paixu(int[] text)
{
this.text=text;
}
class insert extends Thread //插入排序
{ int i,j,temp;
private int[] text;
public insert(int[] text)
{
this.text=text;
}
public void run()
{ for(i=0;i<n-1;i++)
{ temp=text[i+1];
j=1;
while(j>-1&&temp<=text[j])
{ text[j+1]=text[j];
j--;
}
text[j+1]=temp;
}
for(int i=0;i<n;i++)
System.out.println(text[i]+"");
}
}
class maopao extends Thread //冒泡排序
{
private int[] text;
int i,j,flag=1;
int temp;
public maopao(int[] text)
{
this.text=text;
}
public void run()
{ for(i=1;i<n&&flag==1;i++)
{ flag=0;
for(j=0;j<n-i;j++)
{ if(text[j]>text[j+1])
{ flag=1;
temp=text[j];
text[j]=text[j+1];
text[j+1]=temp;
}
}
}
for(int i=0;i<n;i++)
System.out.println(text[i]+"");
}
}
public static void main(String args[])
{ int[] text={20,50,12,2,30};
new paixu(text);
(new insert(text)).start();
(new maopao(text)).start();
}
}
解决方案 »
- 基本数据类型转换
- 关于软件基础技术选型问题
- Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
- 接口的多态跟回调事怎么回事啊?
- URLConnection的方法中getContentEncoding()方法返回Null时,如何获得该连接对应的网页的编码方式?
- 这个程序是通过点击“扩大”来放大另外一个容器中的图,请问高手存在什么问题?
- 使用Calendar类时——疑惑
- JVM 环境变量加载问题
- 在线等待,关于james的邮件备份.
- 100分请教:SL275教材的电子版!
- 请帮我看看这个问题啊!
- JTextArea中添加滚动条
public int n=text.length;text数组还没有初始化,执行n=text.length会报空!
因此n=text.length需要放到:public paixu(int[] text)
{
this.text=text;
n=text.length;
}
里面(2)insert,maopao作为inner class,他们都不是Static(静态),因此,
实例创建时需要主类的实例来创建,如下paixu thread = new paixu(text);
( thread.new insert(text)).start();
( thread.new maopao(text)).start();thread.new X(X为inner class);其他的你再调试吧~