java - 迭代对象列表并跳过一个索引并稍后再次读取它

标签 java list for-loop arraylist iterator

我有一个ArrayList,其中包含字符串名称、字符串描述、字符串url。 我正在遍历列表

我有网站列表

    名称 -- 域

   - Apple  www.apple.com 
   - Bing   www.bing.com 
   - Google www.google.com 
   - Hilton www.hiltonhotels.com 
   - Ink    www.ink.com

在迭代上面的列表时,我想跳过“Hilton”一次,并让“Ink”在本次迭代中先走。

List<Domain> dmn = new ArrayList<Domain>();
for(Domain names:dmn) {
    if(dmn.get(0).getName().equalsIgnoreCase("Hilton")) {
        int index = 0;
        dmn.subList(index, index+1);                       
    }
}

基本上我想将希尔顿的索引更改 1 个值。我怎样才能使用列表来实现。

最佳答案

也许我低估了/简单化了这个问题,但可能的解决方案:

    List<Domain> dmn = new ArrayList<Domain>();
    Iterator<Domain> it = dmn.iterator();
    while(it.hasNext()){
        Domain dom = it.next();
        if(dom.getName().equalsIgnoreCase("Hilton") && it.hasNext()) { // prevents problems if "Hilton" is the last element
        {
            Domain domAfter = it.next();
            System.out.println(domAfter); //do something with the next after Hilton first
            System.out.println(dom);  //do something with Hilton then
        }
        else System.out.println(dom); //else do something with the current element
    }

我在这个问题中遗漏的是你与ArrayList的元素有什么关系。这是一种不太优雅的方式,您不需要使用另一个列表。

关于java - 迭代对象列表并跳过一个索引并稍后再次读取它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25495622/

相关文章:

vb.net - For循环为您创建新变量-VB.Net

python - 在 python 的 for 循环中创建唯一名称列表

java - 使用 libgdx Actor 类和 SpriteBatch 绘制网格几何体

java - Java 将 double 拆分为数组

java - 如何播放从 RingtonePreference 中选择的铃声

python - 将两个单独的列表附加到列表列表

list - Haskell 范围表示法生成列表。意外输出

java - 我如何制作链接的 CharBag (Eclipse Collections)?

python - Python 列表有等同于 dict.get 的东西吗?

matlab - while 循环内有 for 循环