我怎样才能让我的 hashmap public 从 java 中的任何地方打印出来?
How can I make my hashmap public to be printed from anywhere in java?
我希望能够在线程方法中打印散列图,但我不知道如何使其成为全局的或如何将散列图作为参数传递给函数。有人可以帮我吗?
我认为我的问题是,也许我正在从一个文件中做所有事情?是否可以像这样从一个文件中完成我需要的事情?
谢谢。
import java.util.Scanner;
import java.util.HashMap;
import java.net.*;
import java.io.*;
import java.lang.Thread;
public class Server{
public static void main(String[] args){
if (args.length < 1) return;
HashMap<String, String> Distances = new HashMap<String, String>(); // here's the map
menu mThread = new menu();
try{
InetAddress ip = InetAddress.getLocalHost();
Distances.put(ip.toString(), "0");
System.out.println(Distances);
} catch (UnknownHostException e){
e.printStackTrace();
}
int port = Integer.parseInt(args[0]);
try (ServerSocket serverSocket = new ServerSocket(port)) {
System.out.println("server is listening on port " + port);
while(true){
Socket socket = serverSocket.accept();
System.out.println("Client connected");
serverSocket.close();
System.exit(0);
}
} catch(IOException ex) {
System.out.println("exception");
ex.printStackTrace();
}
}
}
class menu implements Runnable{
menu(){
Thread m = new Thread(this);
m.run();
}
public void run(){
printMenu();
}
public void printMenu(){
Scanner input = new Scanner(System.in);
int choice;
System.out.println("1. help\n 2. routing\n 3. myPort");
while(choice != 4){
choice = input.nextInt();
if (choice == 1)
System.out.println("control c to quit, otherwise make another selection");
else if (choice == 2)
System.out.println(Distances); // here is the issue
}
}
}
首先,你应该知道HashMap不是线程安全的,所以最好使用ConcurentHashMap。实现所需行为的方法之一是创建一个 class 接受映射作为构造函数参数,这样您就可以传递它并使用它作为 运行 方法。然后你必须将 class 的实例传递给线程。这是一个非常简单的例子 - https://intellipaat.com/community/24428/how-can-i-pass-a-parameter-to-a-java-thread.
我希望能够在线程方法中打印散列图,但我不知道如何使其成为全局的或如何将散列图作为参数传递给函数。有人可以帮我吗? 我认为我的问题是,也许我正在从一个文件中做所有事情?是否可以像这样从一个文件中完成我需要的事情?
谢谢。
import java.util.Scanner;
import java.util.HashMap;
import java.net.*;
import java.io.*;
import java.lang.Thread;
public class Server{
public static void main(String[] args){
if (args.length < 1) return;
HashMap<String, String> Distances = new HashMap<String, String>(); // here's the map
menu mThread = new menu();
try{
InetAddress ip = InetAddress.getLocalHost();
Distances.put(ip.toString(), "0");
System.out.println(Distances);
} catch (UnknownHostException e){
e.printStackTrace();
}
int port = Integer.parseInt(args[0]);
try (ServerSocket serverSocket = new ServerSocket(port)) {
System.out.println("server is listening on port " + port);
while(true){
Socket socket = serverSocket.accept();
System.out.println("Client connected");
serverSocket.close();
System.exit(0);
}
} catch(IOException ex) {
System.out.println("exception");
ex.printStackTrace();
}
}
}
class menu implements Runnable{
menu(){
Thread m = new Thread(this);
m.run();
}
public void run(){
printMenu();
}
public void printMenu(){
Scanner input = new Scanner(System.in);
int choice;
System.out.println("1. help\n 2. routing\n 3. myPort");
while(choice != 4){
choice = input.nextInt();
if (choice == 1)
System.out.println("control c to quit, otherwise make another selection");
else if (choice == 2)
System.out.println(Distances); // here is the issue
}
}
}
首先,你应该知道HashMap不是线程安全的,所以最好使用ConcurentHashMap。实现所需行为的方法之一是创建一个 class 接受映射作为构造函数参数,这样您就可以传递它并使用它作为 运行 方法。然后你必须将 class 的实例传递给线程。这是一个非常简单的例子 - https://intellipaat.com/community/24428/how-can-i-pass-a-parameter-to-a-java-thread.