【问题标题】:Why are all my object arrays overwriting themselves but the string array is not - java为什么我所有的对象数组都覆盖了自己,但字符串数组却没有 - java
【发布时间】:2017-07-01 15:16:29
【问题描述】:

所以我有这段 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。我可以辛勤工作几个小时试图弄清楚为什么会发生这种情况。我想我会停下来问问专家。

谢谢!

【问题讨论】:

标签: java arrays loops object


【解决方案1】:

正如您在最后两个输出中看到的那样,它为通风口 14 和通风口 41 写入 41。

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

【讨论】:

  • 不幸的是,这不是问题所在。 private VentNode[] nodes = new VentNode[256];
  • @Squirrel-Moose:我的疑问不在于数组...请显示您班级的代码VentNode
  • 所以你是对的。我查看了我的 ventNode 类,所有变量都是静态的……我改变了它们,现在代码可以工作了!
猜你喜欢
  • 2015-01-20
  • 1970-01-01
  • 2011-07-08
  • 1970-01-01
  • 2017-06-24
  • 1970-01-01
  • 2014-02-21
  • 2018-03-29
  • 1970-01-01
相关资源
最近更新 更多