这是独家新闻。我有一个程序循环遍历ArrayList并检查值是否等于输入的关键字(inputArray[0]
)
我想添加默认操作,因为inputArray[0]
不等于keyList
else if
是我遇到问题的地方。我希望我的循环遍历keyList
中的所有值,然后才能使用“最后的手段” - 一个else语句。现在我的问题是,在第一个if语句中,它看到inputArray[0]
不等于keyList[x]
,并且它转到else语句而不经过另一个循环运行。
正如你所看到的,我尝试使用else if语句,如果我的循环计数器x大于keyList的大小,那么它将在里面执行代码,但这看起来不起作用。我还在else语句中添加了continue;
以确保它正在通过循环,根据控制台,它是。 (我知道因为循环开头的System.out
语句。)
public static void wikiInit(ArrayList keyList, ArrayList nameList, ArrayList domainList, ArrayList softwareList, String[] inputArray, EntityPlayer player)
{
System.out.println("These are the current lists:");
System.out.println("Key List: " + keyList);
System.out.println("Name List: " + nameList);
System.out.println("Domain List: " + domainList);
System.out.println("Software List: " + softwareList);
// KEY PARSER
for(int x = 0; x < keyList.size(); x++)
{
System.out.println("Starting the loop");
if((keyList.get(x)).equals(inputArray[0]))
{
//getWikiName = wikiNameArray[x]
//getWikiDomain = wikiDomainArray[x]
//getWikiSoftware = wikiSoftwareArray[x]
StringBuilder hyperlinkBuilder = new StringBuilder();
for(int y = 1; y < inputArray.length; y++)
{
hyperlinkBuilder.append(inputArray[y] + " ");
}
if((softwareList.get(x)).equals("MEDIAWIKI"))
{
String hyperlink = "http://" + domainList.get(x) + "/index.php?search=" + hyperlinkBuilder.toString();
System.out.println("Searching for " + hyperlinkBuilder.toString() + " on the " + nameList.get(x));
player.addChatMessage("Searching for " + hyperlinkBuilder.toString() + " on the " + nameList.get(x));
BrowserHandler.browserInit(hyperlink.replace(" ", "+"), player);
System.out.println("Opening " + hyperlink.replace(" ", "+"));
break;
}
}
else if(x > keyList.size())
{//LAST RESORT
}
else
{
continue;
}
}
}
答案 0 :(得分:4)
而不是循环使用
if(keyList.contains(inputArray[0])){
int x = keyList.indexOf(inputArray[0]);
StringBuilder hyperlinkBuilder = new StringBuilder();
for(int y = 1; y < inputArray.length; y++)
...
}
else { // last resort code
}
答案 1 :(得分:2)
如果默认操作只应在检查完所有元素后发生,则应在循环外进行。你可以通过使用变量来发出这种情况来发出信号:
boolean found = false;
for(int x = 0; x < keyList.size(); x++)
{
System.out.println("Starting the loop");
if((keyList.get(x)).equals(inputArray[0]))
{
found = true;
...
}
}
if (!found) {
//The value was never found, do something special.
}
话虽如此,在这种情况下使用keyList.contains
要容易得多,就像bellabax的回答一样。
答案 2 :(得分:1)
我们通常会这样做,我们首先搜索,然后将代码放在稍后处理找到的代码。
我还把一部分从循环中取出,因为它不需要在那里。它也可能会出现在代码的“找到”部分,但我喜欢让它更容易使代码更具可读性。
此外,MEDIAWIKI的测试留在循环中(与我之前的版本不同)。感谢@paxdiablo。这也是其他一些答案的失败(截至目前)。
StringBuilder hyperlinkBuilder = new StringBuilder(); // lift this out of the loop
for(int y = 1; y < inputArray.length; y++) {
hyperlinkBuilder.append(inputArray[y] + " ");
}
int found = -1;
for(int x = 0; x < keyList.size(); x++)
{
System.out.println("Starting the inside of the loop");
if((keyList.get(x)).equals(inputArray[0])) {
if((softwareList.get(x)).equals("MEDIAWIKI"))
found = x;
break;
}
}
}
if (found >= 0) {
int x = found;
//getWikiName = wikiNameArray[x]
//getWikiDomain = wikiDomainArray[x]
//getWikiSoftware = wikiSoftwareArray[x]
String hyperlink = "http://" + domainList.get(x) + "/index.php?search=" + hyperlinkBuilder.toString();
System.out.println("Searching for " + hyperlinkBuilder.toString() + " on the " + nameList.get(x));
player.addChatMessage("Searching for " + hyperlinkBuilder.toString() + " on the " + nameList.get(x));
BrowserHandler.browserInit(hyperlink.replace(" ", "+"), player);
System.out.println("Opening " + hyperlink.replace(" ", "+"));
} else {
//LAST RESORT ... fill in 'not found' code
}
答案 3 :(得分:1)
一种方法是在循环之前简单地将found
变量设置为false,如果找到密钥匹配则在循环内将其设置为true。
然后循环:
if (!found)
complainBitterly();
答案 4 :(得分:1)
尝试使用布尔值。在for循环之前将其设置为false,如果inputArray[0]
等于keyList[x]
,则将boolean设置为true(在if语句中)。
然后在for循环之后有一个if语句,如果bool仍然是假的话,它将执行你的最后一个案例。
答案 5 :(得分:1)
好消息是你可以通过做2次更改来简化这一过程。
首先,提取您引用的4个单独列表,并将它们组合为一个对象列表,其中包含每个列表的字段,以及代码中的ParameterTuple in the code. Second, you can track loop exit status with another variable,
foundMediaWikiKey`。
/**
* Not sure of a better name for this class, you'll need to look at in the larger sense.
* Also, in production you probably want to use getters for these, rather than final
* public and the constructor
*/
public class ParameterTuple {
public ParameterTuple(String key, String name, String domain, String software) {
this.key = key;
this.name = name;
this.domain = domain;
this.software = software;
}
public final String key;
public final String name;
public final String domain;
public final String software;
}
public static void wikiInit(ArrayList<ParameterTuple> paramList, String[] inputArray, EntityPlayer player) {
System.out.println("These are the current lists:");
System.out.println("List: " + paramList);
// Variable to keep track of how we exited the loop.
boolean foundMediaWikiKey = false;
// KEY PARSER
for(ParameterTuple param : paramList)
{
System.out.println("Starting the loop");
if(param.key.equals(inputArray[0])) {
StringBuilder hyperlinkBuilder = new StringBuilder();
for(int y = 1; y < inputArray.length; y++) {
hyperlinkBuilder.append(inputArray[y] + " ");
}
if(param.software.equals("MEDIAWIKI")) {
String hyperlink = "http://" + param.domain + "/index.php?search=" + hyperlinkBuilder.toString();
System.out.println("Searching for " + hyperlinkBuilder.toString() + " on the " + param.name;
player.addChatMessage("Searching for " + hyperlinkBuilder.toString() + " on the " + param.name;
BrowserHandler.browserInit(hyperlink.replace(" ", "+"), player);
System.out.println("Opening " + hyperlink.replace(" ", "+"));
// Keep track of how we exited the loop
foundMediaWikiKey = true;
break;
}
}
}
// When we exit, check to see how we did so.
if (!foundMediaWikiKey) {
// Last Resort
}
}
答案 6 :(得分:0)
我希望我的循环遍历keyList中的所有值 度假“最后的手段” - 一个别的声明
else if(some condition)
{
if(x!=keylist.size()-1) // USE IT HERE
{ continue; }
//LAST RESORT
}