[Apples, Cheese, Eggs, Tea, Cheese, Tea, Apples, Cheese, Juice, Peppers, Tea, Apples, Cheese, Apples, Cheese, Peppers, Spinach, Tea, Apples, Cheese, Peppers, Tea, Apples, Peppers, Tea, Eggs, Peppers, Tea, Apples, Cheese, Peppers, Tea, Cheese, Eggs, Spinach, Apples, Spinach, Cheese, Spinach, Tea, Cheese, Peppers, Tea] - ArrayList items
[4, 2, 5, 2, 5, 1, 3, 3, 3, 4, 3, 2, 3, 3] - ArrayList numbers
I have these 2 separate arraylists above. I would like to add 4 of the words to a temporary arraylist, and then add that temp arraylist to a spot in a permanent arraylist. So that would be a 2D arraylist. That would be one iteration, then I would like to add 2 more words to the temp arraylist and then add the temp to the second spot in the permanent arraylist... and so on
my method returns a 2d arraylist called ListOfItems, and the method takes in 2 parameters, the arraylist of items (called items) and the arraylist of numbers (called numbers)
int itemsIndex = 0;
int numbersIndex = 0;
for (int i = 0; i < items.size(); i++) {
if (itemsIndex == numbers.get(numbersIndex)) {
ListOfItems.add(itemSet);
itemSet.clear();
numbersIndex++;
} else if (itemsIndex != numbers.get(numbersIndex)) {
itemSet.add(items.get(itemsIndex));
itemsIndex++;
}
}
return ListOfItems;