HashMap hashMap = new HashMap();
//赋值
for(***)
{
    Integer key = new Integer(j);
    Double value = new Integer(sum);
    hashMap.put(key, value);
}//取值
for(***)
{
    Integer key = new Integer(i);
    Double value = (Double)hashMap.get(key);
    System.out.println(value);
}

解决方案 »

  1.   

    顶一下,呵呵,粗一看问题挺简单的,但做的时候发现这个问题不简单
    楼上的方法没作用,你自己看他的程序,是多线程,用HashMap解决不了啊!!
      

  2.   

    试了一下
    也不行,
    关注ING
      

  3.   

    关注的应该是Object.hashcode()的实现吧。实现原理《Thank in Java》里面有。
      

  4.   

    public class NumberProduce extends Thread
    {
        public static void main(String[] args)
        {
            new NumberProduce().start();
            while (true)
            {
                int count = Thread.activeCount();
                /**
                 * System.out.println("The active thread count is :" + count);
                 */
                if (count == 1)
                {
                    break;
                }
            }        for (int i = 0;i < 10;i++)
            {
                Integer j = new Integer(i);
                Double d = (Double)hashMap.get(j);
                System.out.println("(To print)The sum is : " + d.doubleValue());
            }
        }    double sum;
        static HashMap hashMap = new HashMap();    public void run()
        {
          for (int j = 0;j < 10;j++)
          {
              sum = 0;
              for (int i = 0;i < 50;i++)
              {
                sum += Math.random();
              }          System.out.println("The produced sum is : " + sum);
              Integer i = new Integer(j);
              Double d = new Double(sum);
              hashMap.put(i, d);
           }
        }
    }
      

  5.   

    to asdmonster(努力学习VC,讨回失去的信誉分),有,那你把这个问题解一下
      

  6.   

    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));
        }
      }
    }