gpt4 book ai didi

java - 验证搜索 arrayList

转载 作者:行者123 更新时间:2023-12-02 05:09:14 25 4
gpt4 key购买 nike

嗨,我已经创建了搜索数组列表的代码,如果这里找不到任何内容,我需要能够打印一行,这就是我所拥有的。

            for (int i = 0; i < trackNum.size(); i++) { //looping arrayList trackNum
if(trackNum.get(i).equals(trackNumber)){ //searching for matches to trackNumber
System.out.println(trackNum.get(i) + ": " + name.get(i) + " " + duration.get(i)); //printing out the matches
} else{
System.out.println("Not Tracks Found for " + trackNumber);
}
}

目前它不会打印出 else 行,它只是保持空白。

最佳答案

您应该只在迭代整个列表后打印“未找到”消息:

        boolean found = false;
for (int i = 0; i < trackNum.size(); i++) { //looping arrayList trackNum
if(trackNum.get(i).equals(trackNumber)){ //searching for matches to trackNumber
System.out.println(trackNum.get(i) + ": " + name.get(i) + " " + duration.get(i)); //printing out the matches
found = true;
break;
}
}
if (!found) {
System.out.println("Not Tracks Found for " + trackNumber);
}

关于java - 验证搜索 arrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27470665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com