Java 如何使用迭代器迭代二维ArrayList?

Java 如何使用迭代器迭代二维ArrayList?,java,arrays,arraylist,iterator,Java,Arrays,Arraylist,Iterator,我想使用迭代器遍历二维ArrayList,其中包括String对象。我还希望通过使用boolean值来选择是首先水平(行)迭代还是垂直(列)迭代。如何在java中实现这一点 到目前为止我都试过了 public class IterateThis implements Iterator<String>{ ArrayList<ArrayList<String>> array; public IterateThis(){ array = new Array

我想使用迭代器遍历二维
ArrayList
,其中包括
String
对象。我还希望通过使用
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?
公共类迭代器此实现迭代器{
数组列表数组;
公共迭代器this(){
数组=新的ArrayList();
add(新的ArrayList());
add(新的ArrayList());
add(新的ArrayList());
数组.get(0).add(“1”);
数组。获取(0)。添加(“2”);
数组。获取(0)。添加(“2”);
数组。获取(1)。添加(“4”);
数组。获取(1)。添加(“5”);
数组。获取(1)。添加(“6”);
}
迭代器it=array.Iterator();//这给了我一个错误……为什么?

但是我不知道如何实现
布尔值。

可能需要实现两个版本,其中
布尔值决定使用哪个循环:

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";
            }
        }
    }
}
public void iterate(布尔horizantalFirst){
if(水平优先){
对于(int i=0;i为什么不试试这个:

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();
  }
}
import java.util.ArrayList;
公共类迭代
{
私有数组列表数组;
公共迭代()
{
数组=新的ArrayList();
add(新的ArrayList());
数组.get(0).add(“000”);
数组.get(0).add(“001”);
数组.get(0).add(“010”);
add(新的ArrayList());
数组.get(1).添加(“100”);
数组.get(1).add(“101”);
数组.get(1).add(“110”);
数组.get(1).add(“111”);
iterateRowWise();
System.out.println(“\n\n”);
iterateColumnWise();
}
public void iterateRowWise()
{
//这在场景后面使用迭代器。
for(ArrayList行:数组)
{
for(字符串元素:行)
{
系统输出打印(元素+“”);
}
System.out.println();
}
}
public void iterateColumnWise()
{
int arraySize=array.size();
int maxColumns=getMaximumListSize();
对于(int c=0;c
iterateRowWise()
方法使用迭代器进行迭代,但它是在幕后进行的。

iterateColumnWise()
方法不使用迭代器,但使用起来很安全。

行式迭代很简单,如@Awfully Awesome答案所示

尝试了一个列式迭代,假设列表中总是有
m个交叉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;
        }
    }
}
public static void IterateThis(){
ArrayList数组=新的ArrayList();
add(新的ArrayList());
add(新的ArrayList());
数组.get(0).add(“1”);
数组。获取(0)。添加(“2”);
数组。获取(0)。添加(“2”);
数组。获取(1)。添加(“4”);
数组。获取(1)。添加(“5”);
数组。获取(1)。添加(“6”);
迭代器it=array.Iterator();
int-topLevelIteratorResetCounter=0;
int noOfIteratorNextRequired=1;
int size=array.size();
while(it.hasNext()){
ArrayList strList=it.next();
if(noOfIteratorNextRequired>strList.size())
打破
迭代器itString=strList.Iterator();
int numtimes=0;
字符串str=null;
while(numtimes!=NoofiteratorExtrarequired){
str=itString.next();
numtimes++;
}
系统输出打印项次(str);
numtimes=0;
topLevelIteratorResetCounter++;
如果(topLevelIteratorResetCounter==size){//as列计数等于列大小
it=array.iterator();//重置迭代器
NoofiteratorExtrarequired++;
topLevelIteratorResetCounter=0;
}
}
}

答案使用迭代器。

我在您尝试的内容上没有看到布尔值…您在那里发现了什么类型的错误?请在这里提及错误。“数组”是ArrayList的列表。因此,当您创建迭代器时,它应该是同一类型的列表。“这给了我一个错误…为什么?”因为
array.Iterator()
不是
迭代器
而是
迭代器
。由于您的类实现了迭代器,因此必须为类IterateThis实现迭代器方法,并将二维迭代的逻辑放在该方法中。抱歉,我刚刚意识到我没有使用任何
迭代器
无论如何,我更喜欢这种方式;-)在学校,我们必须使用迭代器来完成这个任务。因此,循环的
for
对我来说是没有选择的。rowwise很简单,suggest columnwise!!添加了iterateColumnWise()方法