Java 从ArrayList到Hashmap
我是Java新手,正在尝试使用下面的工作代码,并将我的ArrayList转换为Hashmap。我的困惑是因为他们根本不同。由于hashmap使用键/值对,我不太理解,所以考虑到我已经在使用的程序,我会这么做。对不起,如果这是一个愚蠢的问题,我想我对我需要做什么感到困惑 这是我使用ArrayList的类: 谢谢你的帮助Java 从ArrayList到Hashmap,java,arraylist,hashmap,Java,Arraylist,Hashmap,我是Java新手,正在尝试使用下面的工作代码,并将我的ArrayList转换为Hashmap。我的困惑是因为他们根本不同。由于hashmap使用键/值对,我不太理解,所以考虑到我已经在使用的程序,我会这么做。对不起,如果这是一个愚蠢的问题,我想我对我需要做什么感到困惑 这是我使用ArrayList的类: 谢谢你的帮助 import java.util.ArrayList; import java.util.Scanner; //new class ArrayMessage public cla
import java.util.ArrayList;
import java.util.Scanner;
//new class ArrayMessage
public class ArrayMessage {
//new method shoutOutCannedMethod returning a String
public String shoutOutCannedMessage() {
// create some variables
int arraySize = 10;
String displayUserMessage = "";
String userMessage = "";
String goAgain = "yes";
// setup scanner to store input
Scanner input = new Scanner(System.in);
// create arrayList
ArrayList<String> message = new ArrayList<String>();
// start loop
while (!goAgain.equals("no")) {
// clear out arrayList
message.clear();
// ask the user for 10 messages as long as the counter is less than
// the size of the array
for (int counter = 0; counter < arraySize; counter++) {
System.out.printf(counter + 1 + ": Please enter a message: ");
// save user message to userMessage
userMessage = input.nextLine();
// add users message to arraylist
message.add(userMessage);
}
// ask the user if they want to to load different messages into
// arraylist
System.out.print("Messages have been loaded? Would you like to reload? Type 'yes' or 'no': ");
goAgain = input.nextLine(); // store input
}
// ask the user to choose which message they want displayed
System.out.print("Please enter the number of the message you would like displayed: ");
userMessage = input.nextLine();
// store users message into variable to be used later
displayUserMessage = message.get(Integer.parseInt(userMessage) - 1);
input.close();
// return userMessage
return displayUserMessage;
}
}
我假设您使用ArrayList中元素的索引作为键,使用数组中的实际元素作为HashMap中的值
希望有帮助 我假设您将使用ArrayList中元素的索引作为键,使用数组中的实际元素作为HashMap中的值
希望有帮助 顾名思义,ArrayList是元素列表(内部使用数组,有时会调整大小) 另一方面,HashMap是元素和键之间的映射。这意味着:您没有列表,而是一个键列表,每个键指向一个元素(内部表示有点困难)。因此,这不是一个简单的列表,不应用于列出某些内容,而是创建对象之间的链接 例如,如果要将游戏的玩家对象指定给玩家的聊天名称,可以创建一个哈希图,其中包含字符串作为键,玩家作为对象,如下所示:
Ann -> Player instance of Ann
Bob -> Player instance of Bob
Clara -> Player instance of Clara
...
例如,当您创建以播放器为目标的聊天命令时,这非常有用:
/teleportto Clara
现在您可以像下面这样获得Clara的player对象:
Ann -> Player instance of Ann
Bob -> Player instance of Bob
Clara -> Player instance of Clara
...
playerHashMap.get(“Clara”)
其中Clara是聊天的参数,此调用将返回Clara的player实例
因此,不要用映射替换列表,它们是不同的数据结构。顾名思义,ArrayList是元素列表(内部使用数组,有时会调整大小) 另一方面,HashMap是元素和键之间的映射。这意味着:您没有列表,而是一个键列表,每个键指向一个元素(内部表示有点困难)。因此,这不是一个简单的列表,不应用于列出某些内容,而是创建对象之间的链接 例如,如果要将游戏的玩家对象指定给玩家的聊天名称,可以创建一个哈希图,其中包含字符串作为键,玩家作为对象,如下所示:
Ann -> Player instance of Ann
Bob -> Player instance of Bob
Clara -> Player instance of Clara
...
例如,当您创建以播放器为目标的聊天命令时,这非常有用:
/teleportto Clara
现在您可以像下面这样获得Clara的player对象:
Ann -> Player instance of Ann
Bob -> Player instance of Bob
Clara -> Player instance of Clara
...
playerHashMap.get(“Clara”)
其中Clara是聊天的参数,此调用将返回Clara的player实例
因此,不要将列表替换为映射,它们是不同的数据结构。在尝试在两个正交数据结构之间进行转换之前,您应该回答为什么要这样做的问题。我不知道你想通过这样做来完成什么。不知道你为什么要这样做,但这似乎是让你担心的关键,价值观。。。看看HashSet。它就像一个HashMap,但没有键,只有值。你是在问我们你想做什么吗?我必须为学校做这件事,这样我才能学习HashMaps。否则,事情就是这样。这就是我试图理解的原因/@shmosel,我在问怎么做。在你尝试在两个正交数据结构之间转换之前,你应该回答为什么要这样做的问题。我不知道你想通过这样做来完成什么。不知道你为什么要这样做,但这似乎是让你担心的关键,价值观。。。看看HashSet。它就像一个HashMap,但没有键,只有值。你是在问我们你想做什么吗?我必须为学校做这件事,这样我才能学习HashMaps。否则,事情就是这样。这就是我试图理解的原因/@shmosel,我在问怎么做。