6

I would like to iterate through two dimensional ArrayList which includes String objects using iterator. I also would like to iterate in a way that let me choose whether I want to iterate horizontally(row) first or vertically(column) by using a boolean value. How can I implement this in java?

What I've tried so far.

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?

I don't know how I can implement the boolean value though.

asked Feb 24, 2014 at 12:50
4
  • I see no boolean value on what you're tried... Commented Feb 24, 2014 at 12:54
  • What type of error you find the there? Please mention the Error here. and "array" is the list of ArrayList<String>. so when you create a Iterator. it should be same type of list. Commented Feb 24, 2014 at 12:54
  • "This gives me an error...why?" Because array.iterator() is not an Iterator<String> but an Iterator<List<String>>. Commented Feb 24, 2014 at 12:56
  • since your class implements Iterator, you have to implement the iterate method for class IterateThis, and put the logic of 2-dimensional iteration in that method. Commented Feb 24, 2014 at 13:02

3 Answers 3

3

Maybe you need to implement two versions, with a boolean that decides which loop to use:

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";
 }
 }
 }
}
answered Feb 24, 2014 at 13:05

3 Comments

sorry, I just realized I didn't use any Iterator
I prefer this way, anyway ;-)
We had to use Iterator for this task at school. So for loopis no option for me.
2

Why not try this:

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();
 }
}

The iterateRowWise() method iterates using the iterator, but it does so behind the scene.
The iterateColumnWise() method doesn't use iterator, but its safe to use.

answered Feb 24, 2014 at 13:15

2 Comments

rowwise is simple, suggest columnwise!!
Added iterateColumnWise() method. ;)
1

Row-wise iteration is simple as shown in the @Awfully Awesome answer.

Tried a columnwise iteration with assumption that List will always have m cross n elements where 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;
 }
 }
}

The answer uses Iterator.

answered Feb 24, 2014 at 14:29

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.