java 有没有办法跳过迭代器中的第一个条目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5797455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
is there a way to skip the first entry in an iterator?
提问by Lostsoul
I have some java code that takes a html table and turns it into an Iterator that I use a while loop to parse and add to a database. My problem is the header of the table is causing me problems while I am going through my while look(since its not passing my data quality checks). Is there a way to skip the first row?
我有一些 java 代码,它接受一个 html 表并将其转换为一个迭代器,我使用一个 while 循环来解析并添加到数据库中。我的问题是表的标题在我查看 while 时给我带来了问题(因为它没有通过我的数据质量检查)。有没有办法跳过第一行?
Iterator HoldingsTableRows = HoldingsTableRows.iterator();
while (HoldingsTableRows.hasNext()) {
}
I could get the contents of a variable and if it matches I can break out of the loop but I'm trying to avoid hard coding anything specific to the header names because if the names of the headers change it would break my app.
我可以获得一个变量的内容,如果它匹配,我可以跳出循环,但我试图避免硬编码任何特定于标题名称的内容,因为如果标题名称更改,它会破坏我的应用程序。
please help!
请帮忙!
Thanks!
谢谢!
回答by Tom Neyland
All you need to do is call .next()
once before you begin your while loop.
您需要做的就是.next()
在开始 while 循环之前调用一次。
Iterator HoldingsTableRows = HoldingsTableRows.iterator();
//This if statement prevents an exception from being thrown
//because of an invalid call to .next()
if (HoldingsTableRows.hasNext())
HoldingsTableRows.next();
while (HoldingsTableRows.hasNext())
{
//...somecodehere...
}
回答by digitaljoel
Call next() once to discard the first row.
调用 next() 一次以丢弃第一行。
Iterator HoldingsTableRows = HoldingsTable.iterator();
// discard headers
HoldingsTableRows.next();
// now iterate through the rest.
while (HoldingsTableRows.hasNext()) {
}