簡體   English   中英

如何使用迭代器迭代二維ArrayList?

[英]How to iterate through two dimensional ArrayList using iterator?

我想使用迭代器遍歷包含String對象的二維ArrayList 我還想以一種方式迭代,讓我選擇是否要通過使用boolean值水平(行)或垂直(列)迭代。 我怎樣才能在java中實現它?

到目前為止我嘗試過的。

public class IterateThis implements Iterator<String>{
ArrayList<ArrayList<String>> array;

public IterateThis(){
    array = new ArrayList<ArrayList<String>>();
    array.add(new ArrayList<String>());
    array.add(new ArrayList<String>());
    array.add(new ArrayList<String>());
    array.get(0).add("1");
    array.get(0).add("2");
    array.get(0).add("2");
    array.get(1).add("4");
    array.get(1).add("5");
    array.get(1).add("6");
}

Iterator<String> it = array.iterator(); //This gives me an error...why?

我不知道如何實現boolean值。

也許您需要實現兩個版本,使用boolean來決定使用哪個循環:

public void iterate(boolean horizantalFirst){

    if(horizontalFirst){
        for(int i=0; i<array.size(); i++){              // first iterate through the "outer list"
            for(int j=0; j<array.get(i).size(); j++){   // then iterate through all the "inner lists"
                 array.get(i).get(j)="1";
            }
        }
    }else{ 
        int j=0;                            // index to iterate through the "inner lists"
        for(; j<array.get(j).size(); j++){   //dangerous, you need to be sure that there is a j-th element in array
            for(int i=0; i<array.size(); i++){  // iterate here through the outer list, by always working on the j-th element                
                array.get(i).get(j)="1";
            }
        }
    }
}

為什么不試試這個:

import java.util.ArrayList;

public class Iteration
{
  private ArrayList<ArrayList<String>> array;

  public Iteration()
  {
    array = new ArrayList<>();

    array.add(new ArrayList<String>());
    array.get(0).add("000");
    array.get(0).add("001");
    array.get(0).add("010");

    array.add(new ArrayList<String>());
    array.get(1).add("100");
    array.get(1).add("101");
    array.get(1).add("110");
    array.get(1).add("111");

    iterateRowWise();
    System.out.println("\n\n");

    iterateColumnWise();
  }

  public void iterateRowWise()
  {
    // This uses iterator behind the scene.
    for (ArrayList<String> row : array)
    {
      for (String element : row)
      {
        System.out.print(element + " ");
      }
      System.out.println();
    }
  }

  public void iterateColumnWise()
  {
    int arraySize = array.size();
    int maxColumns = getMaximumListSize();
    for (int c = 0; c < maxColumns; c++)
    {
      for (int r = 0; r < arraySize; r++)
      {
        ArrayList<String> rowList = array.get(r);
        if (c < rowList.size())
        {
          System.out.print(rowList.get(c) + " ");
        }
      }
      System.out.println();
    }
  }

  private int getMaximumListSize()
  {
    int maxListSize = 0;
    for (ArrayList<String> rowList : array)
    {
      if (maxListSize < rowList.size())
        maxListSize = rowList.size();
    }

    return maxListSize;
  }

  public static void main(String[] args)
  {
    new Iteration();
  }
}

iterateRowWise()方法使用迭代器進行迭代,但它在場景后面進行迭代。
iterateColumnWise()方法不使用迭代器,但可以安全使用。

行式迭代很簡單,如@Awfully Awesome答案所示。

試圖進行逐列迭代,假設List總是有m cross n元素,其中m=n

public static void IterateThis() {
    ArrayList<ArrayList<String>> array = new ArrayList<ArrayList<String>>();
    array.add(new ArrayList<String>());
    array.add(new ArrayList<String>());

    array.get(0).add("1");
    array.get(0).add("2");
    array.get(0).add("2");
    array.get(1).add("4");
    array.get(1).add("5");
    array.get(1).add("6");

    Iterator<ArrayList<String>> it = array.iterator();

    int topLevelIteratorResetCounter = 0;
    int noOfIteratorNextRequired = 1;

    int size = array.size();

    while (it.hasNext()) {

        ArrayList<String> strList = it.next();
        if (noOfIteratorNextRequired > strList.size())
            break;
        Iterator<String> itString = strList.iterator();
        int numtimes = 0;
        String str = null;
        while (numtimes != noOfIteratorNextRequired) {
            str = itString.next();
            numtimes++;
        }
        System.out.println(str);
        numtimes = 0;
        topLevelIteratorResetCounter++;
        if (topLevelIteratorResetCounter == size) { //as column count is equal to column size
            it = array.iterator();  //reset the iterator
            noOfIteratorNextRequired++;
            topLevelIteratorResetCounter = 0;
        }
    }
}

答案使用Iterator。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM