private static void readLine(byte line[])throws IOException //这里就在读phone和name啊

解决方案 »

  1.   

    readLine方法仅仅是读取控制台输入的字符串,读完以后并没有赋值给name和phone啊
      

  2.   

    System.in.read()这个就是读的你的输入啊。
      

  3.   

    readLine(name);进入方法前name是空的,进入后他就被赋予了相应的地址
      

  4.   

    我想应该是这样的:
    readLine(name),这时因为name在内存中是唯一的static值,所以在传name进去的时候,穿进去的也是它的唯一引用,自然readLine(byte line[])中对line数组赋值的时候,也就是对name赋值,那么name和phone也就有了值
      

  5.   

    同意楼上的说法readLine(),是一个静态方法name和phone在这个时间就分配内存,它们是数组变量,所以是reference引用    byte[] phone=new byte[lineLength];
        byte[] name=new byte[lineLength];在静态的readLine中对byte[]的改变,直接反映在Reference上面
      

  6.   

    import java.io.*;
    class phones{
      static FileOutputStream fos;
      public static final int lineLength=81;
        byte[] phone=new byte[lineLength];
        byte[] name=new byte[lineLength];
      public static void main(String args[])throws IOException{
        int i;
        fos=new FileOutputStream("phone.numbers");
        phones p = new phones();
        while(true){
          System.err.println("Enter a name(enter 'done' to quit)");
          p.readLine(p.name);
          if ("done".equalsIgnoreCase(new String(p.name,0,0,4))){
            break;
          }
          System.err.println("Enter the phone number");
          p.readLine(p.phone);
          for (i=0;p.phone[i]!=0;i++){
            fos.write(p.phone[i]);
          }
          fos.write(',');
          for (i=0;p.name[i]!=0;i++){
            fos.write(p.name[i]);
          }
          System.out.println(new String(p.name));
          fos.write('\n');
       }
       fos.close();
      }
      private void readLine(byte line[])throws IOException{
        int i=0,b=0;
        while((i<(lineLength-1))&&((b=System.in.read())!='\n')){
          line[i++]=(byte)b;
        }
        //line[i]=(byte)(0);
      }
    }
    我把代码改成这样以后,运行结果和以前一样
      

  7.   

    我不是说了byte[],数组引用是reference引用,将会保留结果
      

  8.   

    import java.io.*;
    import java.io.*;
    class phones{
      static FileOutputStream fos;
      public static final int lineLength=81;
        byte phone ;
        byte name;
      public static void main(String args[])throws IOException{
        int i;
        fos=new FileOutputStream("phone.numbers");
        phones p = new phones();
        while(true){
          System.err.println("Enter a name(enter 'done' to quit)");
          p.readLine(p.name);
          if (1 == p.name){
            break;
          }
          System.err.println("Enter the phone number");
          p.readLine(p.phone);
    //      for (i=0;p.phone[i]!=0;i++){
    //        fos.write(p.phone[i]);
    //      }
    System.out.println(p.phone);   }
       fos.close();
      }
      private void readLine(byte line)throws IOException{
        int i=0,b=0;
        while((i<(lineLength-1))&&((b=System.in.read())!='\n')){
          line=(byte)b;
        }
        //line[i]=(byte)(0);
      }
    }
    假如你使用byte的话,你永远也不能退出了
      

  9.   

    static引用是引用内存中的唯一地址空间
    那对象引用呢,比如ClassA a = new ClassA()
    ClassA b = new ClassA();a和b也是同一个地址空间吗
      

  10.   

    你看看这个贴子,我当时花了很大时间说明这个问题http://expert.csdn.net/Expert/topic/1481/1481650.xml?temp=.9231989
      

  11.   

    ClassA a = new ClassA()
    ClassA b = new ClassA();这个不是同一地址空间,new的时间a和b分配的不同