java - 将文件数组与字符串数组共同链接

标签 java arrays string

我在将文件和字符串数组链接在一起时遇到了一个小问题......我想将文件数组的每个元素与字符串数组中相应的数字相匹配,例如(将单词[1]匹配到答案[1] ])我对此不知道......感谢您的帮助!

private static File[] word = 

    { new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\audio.mp3"), 
        new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\baby.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\board.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\bomb.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\gym.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\football.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\school.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\keyboard.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\computer.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\name.mp3"), 
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\lady.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\church.mp3"), 
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\sport.mp3"), 
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\beauty.mp3"), 
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\radio.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\prince.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\hearing.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\worship.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\song.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\flower.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\water.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\nature.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\goal.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\manifest.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\election.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\number.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\sentence.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\movie.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\sound.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\teacher.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\speed.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\time.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\debate.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\video.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\music.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\phone.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\mountain.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\drink.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\market.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\broom.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\help.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\picture.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\princess.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\cake.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\river.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\dance.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\rely.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\level.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\wealth.mp3"),
    new File ("C:\\Users\\HP\\Downloads\\Word Pronunciation\\surname.mp3") };

    private String []answer = {"audio", "baby", "board", "bomb", "gym", "football", "school", "keyboard", "computer", "name" ,
            "lady", "church", "sport", "beauty", "radio", "prince", "hearing", "worship", "song", "flower", "water", "nature", "goal" ,
            "manifest", "election", "number", "sentence", "movie", "sound", "teacher", "speed", "time", "debate", "video", "music", 
            "phone", "drink", "market", "broom", "help", "picture", "princess", "cake", "river", "dance", "rely", "level", "wealth", "surname"};

JButton click = new JButton();

public class Beginner(){

click = new JButton("1");

click.addActionListener(new ActionListener(){
 public void actionPerformed (ActionEvent x){

    }
});
}
}

最佳答案

你最好使用Map .

Map<String, File> mapping = new HashMap<>();
mapping.put("audio", new File("C:\\Users\\HP\\Downloads\\Word Pronunciation\\audio.mp3");
// etc...

然后使用以下方法获取值:

File file = mapping.get("audio");

关于java - 将文件数组与字符串数组共同链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39282234/

相关文章:

java - 我可以在 String 变量中存储特殊符号吗?

java - 具有变化对象的连续线程

javascript - 按键值过滤和分组

python - 所有(2 ^ m−2)/2个可能的分区列表方式

python - 为什么 np.median() 返回多行?

python - 替换 pandas 数据框列中的一些字符串值,其值应为浮点型

java - 在 JavaFX 中设置背景颜色不起作用

java - Spring 中新线程上的 Rx java 本地化问题

java - Java的赋值操作顺序

c - 通过 char 指针输出不正确