代码之家  ›  专栏  ›  技术社区  ›  Michael Artman

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

  •  0
  • Michael Artman  · 技术社区  · 8 年前

    我有一段java代码,用于一系列通风口的集线器系统。然而 VentNode 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
    $ 
    

    正如你在最后两个输出中看到的,它为vent 14和vent 41写了41。我花了几个小时试图找出为什么会发生这种情况。我想我会停下来问问专家。

    1 回复  |  直到 8 年前
        1
  •  1
  •   Timothy Truckle Vincent Boutot    8 年前

    正如您在最后两个输出中所看到的,它为vent 14和vent 41写入41。

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