JAVA中,有没有这么一个容器,就是可以把产生的值按编号放入这个容器中。。。
比如我产生了50个值
for (int i=0;i<50;i++)
{
System.out.println(Math.random());
}
想把这个值按i的编号放入一个容器中,调用的时候可以通过编号调用,如果编号相同的,则覆盖,不同的,则加入,容器自动按编号大小排列。
比如
public class asdf2 extends Thread
{
double sum;
public void run()
{
for (int j=0;j<10;j++)
{
for (int i=0;i<50;i++)
{
sum+=Math.random();
}
//把sum结果按j加入一个容器
}
}
public static void main(String[] args)
{
for (int i=0;i<10;i++)
{
new asdf2().start();
//把sum按j的结果打印出来
}
}
}
帮你UP
You could use Vector (as in following example). Be aware that the values (the sums) are not sorted but their corresponding index are.
package test;
import java.util.Vector;
public class asdf2
extends Thread {
static Vector container_ = new Vector();
public void run() {
for (int j = 0; j < 10; j++) {
double sum = 0;
for (int i = 0; i < 50; i++) {
sum += Math.random();
}
synchronized (container_){
container_.add(j, new Double(sum));
}
}
}
public static void main(String[] args) {
int numberOfThreads = 10;
asdf2[] threads = new asdf2[numberOfThreads];
for (int i = 0; i < numberOfThreads; i++) {
threads[i] = new asdf2();
threads[i].start();
}
for (int i = 0; i < numberOfThreads; i++) {
try {
threads[i].join();
} catch (java.lang.InterruptedException ex){
ex.printStackTrace();
}
}
for (int i = 0; i < numberOfThreads; i++) {
System.out.println(container_.get(i));
}
}
}