find String of an Array that is in an array

白昼怎懂夜的黑 提交于 2021-02-11 11:55:00

问题


Ok, so I have a array

String[] sound = new String[2]
sound[0] = nameSound;
sound[1] = routeSound;

and this Array is in an Array

String[] Sounds[]= new String[1][]; 

Sounds[0]=sound;

I just made an Array, but I will have a lot of arrays

And if the users write nameSound "name", It has to be compared to the nameSound of every vector called sound, when it is found, I want something like

 System.out.print(nameSound);

Any ideas are appreciated :)


回答1:


The simplest solution is to use just 2 for loops. But if the arrays are realy large you must find some more performance optimated way.

for(String[] sound : Sounds)
{
    for(String soundName : sound)
    {
        if(soundName.equals(searchString))
        {
             System.out.print(soundName);
        }
    }
}



回答2:


You could of course loop over all nested arrays, the code itself isn't hard. But aren't you actually looking for the functionality of a map where you store the name as key and route as value? Then you could easily find the routeSound by entering the nameSound, while you still have access to the data.

pseudocode:

Map<String, String> sounds = new HashMap<String, String)();
sounds.put(nameSound, routeSound);

String foundRouteSound = sounds.get(inputNameSound);


来源:https://stackoverflow.com/questions/36677224/find-string-of-an-array-that-is-in-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!