2

I'm trying to do an multi-dimensional ArrayList, so I've come up with the method of doing:

int NumberInColumns = (int) Math.floor(listAnswers.size() / NUMBER_OF_COLUMNS);
// make higher number of answers on the right
if (listAnswers.size() % NUMBER_OF_COLUMNS > 0)
        NumberInColumns++;
ArrayList<HolderAnswer> listAnswers = getAnswers();
ArrayList<ArrayList<HolderAnswer>> listAnswersSorted = new ArrayList<ArrayList<HolderAnswer>>();

int k=0;
for (HolderAnswer answer : listAnswers) {
    ArrayList<HolderAnswer> temp = new ArrayList<HolderAnswer>();
    temp.add(answer);   
    if (k % NumberInColumns == 0 ) { 
            listAnswersSorted.add(temp);            
        }
        else { 
            if (k == listAnswers.size()) { 
                listAnswersSorted.add(temp);
            }
        }
        k++;
}

The problem I'm having is that with an ArrayList.size() of 7, I'm only getting the first and fifth records when I try and get the information out of the array. What am I missing?

Sami Eltamawy
  • 9,874
  • 8
  • 48
  • 66
Kristy Welsh
  • 7,828
  • 12
  • 64
  • 106
  • I didn't get you. but in your `for-each` loop, the condition `if (k == listAnswers.size())` will never be `true` as loop will be iterated for `listAnswers.size()` times... – Gopal Gopi Mar 11 '14 at 18:01
  • if (k == listAnswers.size()) is definitely reached when I step through the code. – Kristy Welsh Mar 11 '14 at 18:10

2 Answers2

0

So instantiating a multidimensional (I pick 3D) arraylist could look like this:

List< List <List <Some Type Here>>> my3DList= 
new ArrayList< ArrayList < ArrayList <Some Type Here >() >() >();

So first lets look at how to get data out of this...

ArrayList secondDemension = my3DList.get(0);
ArrayList thirdDemension = secondDemension.get(0);
SomeTypeHere value = thirdDemsion = 3D.List.get(0);

Now lets look at how to iterate over all data:

for(List<List<SomeType>> secondDemension in my3DList){
    for(List<sometype> thirdDemsion in secondDemension){
        for(YourType value in thridDemsion){
           System.out.println(value);
        }
    }

}
MGot90
  • 2,422
  • 4
  • 15
  • 31
0

Trying to sort an array to be displayed in table rows

ArrayList<HolderAnswer> listAnswers = getAnswers();
ArrayList<ArrayList<HolderAnswer>> listAnswersSorted =
new ArrayList<ArrayList<HolderAnswer>>();

// initialize the ArrayLists in listAnswersSorted
int numRows = listAnswers.size() / numColumns + 1;
for (int i = 0; i < numRows; i += 1) {
  listAnswersSorted.add(new ArrayList<HolderAnswer>());
}

// calculate column index where the "step" happens
int step = listAnswers.size() % numColumns;

// loop through and add elements to listAnswersSorted
int index = 0;
int row = 0;
int col = 0;
while (index < listAnswers.size()) {
  listAnswersSorted.get(row).add(listAnswers.get(index));

  int rows = col < step ? numRows : numRows - 1;
  row += 1;
  if (row == rows) {
    row = 0;
    col += 1;
  }
  index += 1;
}

// flatten the ArrayList<ArrayList> into a single ArrayList
ArrayList<HolderAnswer> newList = new ArrayList<HolderAnswer>();
for (ArrayList<HolderAnswer> list : listAnswersSorted) {
   newList.addAll(list);
 }
Community
  • 1
  • 1
Kristy Welsh
  • 7,828
  • 12
  • 64
  • 106