首先New了一个String类型的数组
String basicprice = new String[3];然后在setprice()中给它赋值,同时调用paint()函数
//获取当天的涨跌停价格
  public void setprice(String[] temp) {
    System.arraycopy(temp,0,basicprice,0,3);
    System.out.println(basicprice.toString());//这里输出:[Ljava.lang.String;@1687e7c    System.out.println(basicprice[0]);//这里输出:21.65
    
    repaint();
  }
在paint()中,我调用basicprice数组
public void pain(){
    System.out.println("I'm here---"+basicprice.toString());//这里输出:I'm here---[Ljava.lang.String;@76e8a7    System.out.println("inside of paint---"+basicprice[0]);//这里输出:inside of paint---null同一个数组,在两个函数中的数据竟然是不一样的显然,看起来他们的reference是不一样的,但是我绝对只定义了一个这样的数组,绝对只单纯的在这两个函数中使用,一个赋值,一个使用却出现了这样的现象····哪位高手解答·····困惑我好几个小时了···多谢指点}
非常奇怪的发现

解决方案 »

  1.   

    是null啊,肯定没有赋值啊,引用都不一样了。
    完整代码看看吧。
      

  2.   

    主要是因为作用域的问题吧,还 有,你的代码并没有完全帖出来
    String basicprice = new String[3];你这句话也是错的
      

  3.   

    我很不明白这句:String basicprice = new String[3];为什么能够通过
      

  4.   

    刚才试着做了一下
    String[] a = new String[3];

    System.out.println(a.toString()+"=>"+a[0]);
    String[] b = new String[]{"a","b","c"};
    System.arraycopy(b,0,a,0,3);
    System.out.println(a.toString()+"=>"+a[0]);
    输出的结果很正常
    [Ljava.lang.String;@35ce36=>null
    [Ljava.lang.String;@35ce36=>a
    然后在下面加了一条
    a = new String[3];
    System.out.println(a.toString()+"=>"+a[0]);
    显示的是
    [Ljava.lang.String;@757aef=>null
    也就是说你的basicprice被new 了两次
      

  5.   

    OK 我把代码贴上来 麻烦大家了··
    class OptimizedDoubleBufferedCanvas extends Canvas {     public void update(Graphics g) {
             Graphics offgc;
             Image offscreen = null;
             Rectangle box = g.getClipRect();         // create the offscreen buffer and associated Graphics
             offscreen = createImage(box.width, box.height);
             offgc = offscreen.getGraphics();
             // clear the exposed area
             offgc.setColor(getBackground());
             offgc.fillRect(0, 0, box.width, box.height);
             offgc.setColor(getForeground());
             // do normal redraw
             offgc.translate(-box.x, -box.y);
             paint(offgc);
             // transfer offscreen to window
             g.drawImage(offscreen, box.x, box.y, this);
         }
     } /**
      此类用来画走势图与瞬间量
      */public class Draw_tam
        extends OptimizedDoubleBufferedCanvas {
      int x = 30, y = 10;
      int width, height;
      //画图用的的变量
      int divide, hdivide, moment_maxheigth, moment_startpoint, moment_add;
      //时间轴数据
      String[] time = {
          "9:00", "9:30", "10:00", "10:30", "11:00", "11:30", "12:00", "12:30",
          "13:00", "13:30"};
      //保存当天的涨跌停价,这里定义
      String[] basicprice = new String[3];
      public void paint(Graphics g) {
        //画坐标轴
        g.setColor(Color.PINK);
        g.drawLine(30, 415, 600, 415);
        g.drawLine(30, 315, 600, 315);
        g.drawLine(30, 415, 30, 0);
        //时间
        g.drawString("8:45", 30, 425);
        for (int i = 0; i < 10; i++) {
          g.drawString(time[i], 60 + i * 60, 425);
        }
        //画涨跌价
        //这里使用
        System.out.println("I'm here---"+basicprice.toString());
        System.out.println("inside of paint---"+basicprice[0]);
        //g.drawString(ibasic_info[0],30,315);
        //System.out.println(ibasic_info[0]);
        //g.drawString(ibasic_info[0], (width / 10) - 45, hdivide);
        //g.drawString(ibasic_info[2], (width / 10) - 45, 2 * hdivide);
        //查价线    g.drawLine(x,0,x,415);
        g.setColor(Color.white);
        g.drawString("最高价",450,15);
        g.setColor(Color.red);
        g.drawString(String.valueOf(x+10),500,15);
        g.setColor(Color.white);
        g.drawString("成交价",450,30);
        g.setColor(Color.blue);
        g.drawString(String.valueOf(x),500,30);
        g.setColor(Color.white);
        g.drawString("最低价",450,45);
        g.setColor(Color.green);
        g.drawString(String.valueOf(x-10),500,45);
      }
      //保持唯一性
      private static Draw_tam draw_tam = null;  public static Draw_tam get_draw_tam() {
        if (draw_tam == null) {
          draw_tam = new Draw_tam();
        }
        return draw_tam;
      }  public Draw_tam() {
        try {
          jbInit();
        }
        catch (Exception e) {
          e.printStackTrace();
        }
      }  private void jbInit() throws Exception {
        this.addMouseMotionListener(new Draw_tam_this_mouseMotionAdapter(this));
        this.setBackground(Color.black);
        this.setBounds(200, 200, 600, 500);
        //每半小时时间的长度
        divide = (width - width / 10) / 10;
        //
        hdivide = (height - height / 10) / 3;  }  void this_mouseDragged(MouseEvent e) {
        x = e.getX();
        y = e.getY();
        repaint();
      }  //获取当天的涨跌停价格 这里赋值
      public void setprice(String[] temp) {
        System.arraycopy(temp,0,basicprice,0,3);
        System.out.println(basicprice.toString());
        System.out.println(basicprice[0]);
        //System.out.println(basicprice[1]);    repaint();
      }  //测试
      public static void main(String args[]) {
        Draw_tam draw_tam = new Draw_tam();
        JFrame frame = new JFrame();
        JPanel contentPane = (JPanel) frame.getContentPane();
        contentPane.add(draw_tam, BorderLayout.CENTER);
        frame.setTitle("Test Canvas");
        frame.setBounds(200, 200, 600, 500);
        frame.pack();
        frame.show();    frame.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }
        });
      }
    }class Draw_tam_this_mouseMotionAdapter
        extends java.awt.event.MouseMotionAdapter {
      Draw_tam adaptee;  Draw_tam_this_mouseMotionAdapter(Draw_tam adaptee) {
        this.adaptee = adaptee;
      }  public void mouseDragged(MouseEvent e) {
        adaptee.this_mouseDragged(e);
      }}
    我只定义了一个这样的数组,只单纯的在这两个函数中使用,一个赋值,一个使用楼上的说我New 了两次···,我检查了下,应该没有啊
      

  6.   

    你的 setprice() 在哪里调用的?上面的程序中没有看到。如果是在别处调用的,那里用的是哪个 Draw_tam 对象?如果你是单独创建了一个 Draw_tam 对象,那就对上茬了,每个 Draw_tam 对象有自己的 basicprice 数组。你的 Draw_tam 类好像使用了 Singleton 模式,但你的构造方法却是 public 的,这会有问题的。
      

  7.   

    两次数组输出分别为:
    [Ljava.lang.String;@1687e7c
    [Ljava.lang.String;@76e8a7内存地址都不一样,这两个数组不是同一个,肯定重新生成过 ^_^
      

  8.   

    多谢楼上的回复setprice()是在别处调用的我没有单独创建一个Draw_tam 对象,是使用Draw_tam get_draw_tam()获得此对象,然后调用
    setprice()的你所说的这句话:“你的 Draw_tam 类好像使用了 Singleton 模式,但你的构造方法却是 public 的,这会有问题的。”有啥问题?··请指教····
      

  9.   

    你所说的这句话:“你的 Draw_tam 类好像使用了 Singleton 模式,但你的构造方法却是 public 的,这会有问题的。”有啥问题?··请指教····可能在多线程时,我不喜欢Singleton
      

  10.   

    > setprice()是在别处调用的
    > 我没有单独创建一个Draw_tam 对象,是使用Draw_tam get_draw_tam()获得此对象,
    > 然后调用setprice()的
    > 你所说的这句话:“你的 Draw_tam 类好像使用了 Singleton 模式,但你的构造
    > 方法却是 public 的,这会有问题的。”
    > 有啥问题?··请指教····Singleton 的意思是,让所有的调用者都只能通过 getInstance() 使用这个类的唯一的一个对象。但如果这个类的构造方法是 public 的,那么,调用者除了用 getInstance() 之外,还可以选择 new 一个对象出来,于是,Singleton 的初衷就无法保证了。就像你上面的程序,在 main() 里是 new 出来的一个 Draw_tam 对象,而在程序的另一处,你很可能通过 Draw_tam.get_draw_tam() 得到了一个“所谓的”Singleton 对象,那么,事实上两处在使用的就是两个不同的 Draw_tam 对象,那么自然也就是两个不同的 basicprice 喽。