Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么赢了';我的arraylist不能添加多个对象吗?_Java_Arraylist - Fatal编程技术网

Java 为什么赢了';我的arraylist不能添加多个对象吗?

Java 为什么赢了';我的arraylist不能添加多个对象吗?,java,arraylist,Java,Arraylist,如果查看If-else块,您将看到我正在尝试将person对象添加到arraylist中。但是,它将只向arraylist添加/保留一个对象。如果我循环遍历数组并打印出所有元素,那么只有一个person对象,并且它总是最后一个实例化的person对象。有人知道为什么会这样吗?我已经在Stack Overflow上看过类似的文章,但找不到任何帮助 public class ClientThread extends Thread { static ArrayList<Person>

如果查看If-else块,您将看到我正在尝试将person对象添加到arraylist中。但是,它将只向arraylist添加/保留一个对象。如果我循环遍历数组并打印出所有元素,那么只有一个person对象,并且它总是最后一个实例化的person对象。有人知道为什么会这样吗?我已经在Stack Overflow上看过类似的文章,但找不到任何帮助

public class ClientThread extends Thread {

  static ArrayList<Person> array = new ArrayList<Person>();

  Socket socket;

  public ClientThread(Socket socket) {
    this.socket = socket;
  }

  public void run() {
    try {
      handleConnection(socket);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

  public void handleConnection(Socket server) throws IOException {
    String x = "";
    //to get input from the client
    BufferedReader in = new BufferedReader(new InputStreamReader(server.getInputStream()));
    //Out to the client -- Enable auto-flush
    PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(server.getOutputStream())), true);
    //prints "got a connection from X" on server terminal
    System.out.println("got connection from " + server.getInetAddress().getHostName() + "\n");

    while (true) {
      // get input from the client
      if((x = in.readLine()).length() != 0) {
        if (x.equals("END")) {
          System.out.println("This thread is shutting down. BYE!");
          break;
        } else if (x.equals("STORE") || x.equals("store")) {
          String storeName;
          System.out.println("Client wants to store someone's number.");
          out.println( "SERVER > Enter a name and number: ");
          storeName = in.readLine();
          array.add(new Person(storeName));
        } else {
          System.out.println("Echoing: " + x);
          out.println( "SERVER > ");
        }
      }
    }             
  }    
}

如何运行服务器?尝试使用调试器(或者至少向服务器控制台添加一些调试语句)。另外,
while(true)
可能应该是
while((x=in.readLine()).length()!=0)
如何打印
数组
?我正在终端中运行服务器。我确实有一些调试语句(ex-a for循环在数组中运行,每次添加时都打印每个元素),但是我把它们取出来是为了使这篇文章不太复杂;语句并运行它。数一数“HI”的数量,你就会知道一个新对象被添加到列表中的平均时间。
class Person {

  protected String name;

  public Person(String name) {
        this.name = name;       
  }

  //overide toString
  public String toString() {
    return name;
  }
}