为什么我的所有对象数组都会覆盖自己,但字符串数组不是 - java

时间:2017-07-01 15:16:30

标签: java arrays loops object

所以我有一些java代码,用于一堆通风口的集线器系统。但是,VentNode对象将所有活动值写入与列表插入值相同。我创建了一个String array来测试它是否会执行相同操作,但String array通常会执行'。

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class ThreadedEchoServer {

    private static final int port = 50103;
    private static final int timeout = 500;
    private VentNode[] nodes = new VentNode[256];
    private String[] ips = new String[256];

    public static void main(String args[]) {
        ThreadedEchoServer server = new ThreadedEchoServer();
        Scanner scanner = new Scanner(System.in);
        System.out.println("Vent system 10,000");
        server.scan();
        server.list();
        while(true){
            System.out.print("$ ");
            String msg = scanner.next();
            if(msg.equals("scan")){
                server.scan();
            }else if(msg.equals("on")){
                server.command(msg);
            }else if(msg.equals("off")){
                server.command(msg);
            }else if(msg.equals("list")){
                server.list();
            }else if(msg.equals("shutdown")){
                System.out.println("Shutting down");
                break;
            }else{
                System.out.println("Unknown command: " + msg);
            }
        }
        scanner.close();
    }
    public void command(String cmd){
        for (VentNode node : nodes) {
            if (node != null ){
                Socket s;
                try {
                    s = new Socket(node.getSocket(),port);
                    new EchoThread(s,cmd);
                } catch (UnknownHostException e) {
                    // TODO Auto-generated catch block
                    System.out.println("Error connecting to node " + node.getId());
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    System.out.println("Error connecting to node " + node.getId());
                    e.printStackTrace();
                }

            }

        }
    }

    public void list(){
        for (String node : ips) {
            if (node != null )
                System.out.println("Socket " + node + ": " + node);
        }       
        for (VentNode node : nodes) {
            if (node != null )
                System.out.println("Socket " + node.getId() + ": " + node.getSocket());
        }
    }



    public void scan(){
        System.out.println("Scanning for vents. This will take a minute.");
        try {
            String currentIP = InetAddress.getLocalHost().toString();
            String subnet = getSubnet(currentIP);
                for (int i=13;i<42;i++){
                    Socket socket;
                    String host = subnet + i;
                    if (InetAddress.getByName(host).isReachable(timeout)){
                        //System.out.println(host + " is reachable");
                        try {
                            socket = new Socket(host, port);
                            System.out.println("Adding vent: " + i);
                            new EchoThread(socket);
                            nodes[i] = new VentNode(host,i,0,0);
                            ips[i] = host;
                        }catch(Exception s){
                            //System.out.println("failed to connect to " + host + " on port " + port);
                        }
                    }

                }
        }catch(Exception e){
            System.out.println(e);
        }
    }
    private String getSubnet(String currentIP) {
        int firstSeparator = currentIP.lastIndexOf("/");
        int lastSeparator = currentIP.lastIndexOf(".");
        return currentIP.substring(firstSeparator+1, lastSeparator+1);
    }
}

,输出如下:

Vent system 10,000
Scanning for vents. This will take a minute.
Adding vent: 14
Adding vent: 41
Socket x.x.x.14: x.x.x.14
Socket x.x.x.41: x.x.x.41
Socket 41: x.x.x.41
Socket 41: x.x.x.41
$ 

正如你在前两个输出中所看到的那样,它为通风口14和通风口41写了41个。我可以辛苦工作几个小时试图弄清楚为什么会发生这种情况。我想我会停下来问专家。

谢谢!

1 个答案:

答案 0 :(得分:1)

  

正如你在前两个输出中看到的那样,它为通风口14和通风口41写了41。

此时它只是一个有根据的猜测,但很可能你在类ventNode 静态中声明了这些字段,这意味着它们的内容在该类的所有实例之间共享。