我在字符串向量上迭代如下:
for(String s : st){
while(s.equals("a")) //just an example, not exactly this required
{
//go to next element : How to do this?
}
System.out.println(s);
}如何在for(:)循环中迭代下一个元素?
编辑:
正如许多人所问的那样,
字符串的向量基本上包含一个句子的单个单词,而我必须在句子中折叠名词短语,例如,如果有一个句子,如“罗伯特·西格威克要回家”。所以现在st有"Robert“,st1有"Sigwick”。经过处理后,我得做“罗伯特·西格威克”。
所以我的代码有点像:
for(String s : st){
string newEntry = "";
while(getPOS(s).equals("NNP"))
{
newEntry += s;
// HERE I WANT THE HELP : something like s = getNext();
}
if(!newEntry.equals(""))
result.add(newEntry);
}发布于 2014-03-10 19:11:46
使用循环标签并继续
OUTER:
for(String s : st){
while(s.equals("a")) //just an example, not exactly this required
{
//go to next element : How to do this?
continue OUTER;
}
System.out.println(s);
}注意:只有在有嵌套循环时,循环标签才是必需的。如果时间应该是If -语句,那么一个简单的continue;就可以工作了。
也,如果它是if -语句,可能会有更好的方法。考虑:
for(String s : st){
if(!s.equals("a")) //just an example, not exactly this required
{
System.out.println(s);
}
}这里的问题是,整个方法的层次更深。这是一种偏好。
更多关于循环标签的信息:"loop:" in Java code. What is this, why does it compile?
发布于 2014-03-10 19:12:01
for(String s : st){
if(s.equals("a")) //just an example, not exactly this required
{
//go to next element : How to do this?
continue;
}
System.out.println(s);
}发布于 2014-03-10 19:27:24
您需要解释为什么需要while循环。您不能这样转到下一个元素。
https://stackoverflow.com/questions/22309136
复制相似问题