【问题标题】:how to add data to an array in while loop java如何在while循环java中将数据添加到数组中
【发布时间】:2014-03-22 08:31:16
【问题描述】:

如何将str,str1,str2,str22,name,randNo添加到String数组中,loop(while)中的数据每次都可能发生变化。 当连接建立后,用户向下面的程序发送数据,它需要将它存储在一个数组中。

   String s[][]=new s[100][100];
   ServerSocket ss=new ServerSocket(9999);
   while(true)
   {

    Socket s=ss.accept();
    DataInputStream dis=new DataInputStream(s.getInputStream());
    int str=dis.readInt();
    System.out.println("message= "+str);
    int str1=dis.readInt();
    System.out.println("message= "+str1);
    int str2=dis.readInt();
    System.out.println("message= "+str2);
    int str22=dis.readInt();
    System.out.println("message= "+str22);
    String name=dis.readUTF();
    System.out.println("message= "+name);
    int randNo=dis.readInt();
    System.out.println("Unique no:"+randNo);
    s[][]={{str,str1,str2,str22,name,randNo}};
    }

【问题讨论】:

    标签: java arrays loops networking


    【解决方案1】:

    使用一些索引值i,j来指定String数组的索引。每次客户端连接建立时,数据都会被追加。如果你想要一些动态的,自动递增的数据结构,那就去ArrayList(java.实用程序)。

    【讨论】:

      猜你喜欢
      • 2020-08-24
      • 2012-04-15
      • 1970-01-01
      • 2011-08-13
      • 1970-01-01
      • 2012-10-02
      • 2016-08-25
      • 1970-01-01
      • 2019-10-26
      相关资源
      最近更新 更多