我又回来了一个类似的问题.是否有可以返回其特定合作伙伴的DataType?例如:
ExampleType<String,String> test = new ExampleType<String,String>();
test.put("hello","hi");
Run Code Online (Sandbox Code Playgroud)
如果我输入test.get("hi"),它将返回"hello",如果我输入test.get("hello"),它将返回"hi".
我对此的唯一猜测可能是二维数组,但我不确定如何实现它.截至目前,我能理解的唯一方法是创建两个不同的哈希映射并交换每个哈希映射中的密钥.(显然这不是非常有效/有效).
感谢您的帮助!
我对编程很陌生,所以如果这是一个愚蠢的问题,我很抱歉.我想知道是否有一种类型的数据可以返回(可能)每个键多个答案.例如:
TestType<int,String> test = new TestType<int,String>();
因此,如果你键入test.getKey(1),你可以获得{"hello","this","is","a","test"}.
基本上,是否有一种数据可以返回多个答案,有点像HashMap和List组合?
我正在尝试制作一个播放声音的简单应用程序.我有一个名为sound.wav的声音文件位于我的java项目中(使用eclipse btw).我不确定如何导航到声音文件.问题是我不知道如何通过代码导航到声音文件.我现在正在运行的是抛出空指针异常,即.该文件不存在.到目前为止,这是我的代码:
private static Sound sound;
public static void main(String[] args) {
JFrame j = new JFrame("Sound");
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setSize(300, 150);
sound = new Sound("/Users/Chris/Desktop/Workspace/Sound/sound.wav");
//this is the problem line
JButton play = new JButton("Play");
play.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sound.play();
}
});
j.add(play,BorderLayout.SOUTH);
j.setVisible(true);
}
Run Code Online (Sandbox Code Playgroud)
这是我的声音类的代码:
private AudioClip clip;
public Sound(String fileName) {
try {
clip = Applet.newAudioClip(Sound.class.getResource(fileName));
}
catch (Exception e) {
e.printStackTrace();
}
}
public void play() {
try {
new Thread(){
public void run() …Run Code Online (Sandbox Code Playgroud)