1

I am trying to grasp the concept. I have never worked with ArrayLists before (just arrays).

What I have is:

ArrayList<ArrayList<String>> movies = new ArrayList<ArrayList<String>>();

What this will look like or the way I picture it is:

[[Ratatouille, A Bug's Life], [Tangled, Zootopia, Finding Dory], [Harry Potter]]

And say the userInput = 2; then I would subtract 1 from the user input (because Array's and ArrayList's index at 0 that much I know) so userInput= 1; (based on their multiple choice selection, not very important).

Then what I want to do is take the index 1 so [Tangled, Zooptopia, Finding Dory] and loop through that index and add it to an ArrayList (not ArrayList of an ArrayList).

1
  • Try to split your problem into smaller ones, like: (1) how to get element from list at specified index (2) how to iterate over all elements in list (3) how to add new element to list. After answering those smaller questions you can combine them like get list from movies at position 1; iterate over each element from received list; add each iterated element to another list;. Commented Mar 3, 2018 at 19:42

6 Answers 6

1

No need to loop - You can access an ArrayList by an index, and then use the addAll method to add all the elements of the ArrayList in that position to your result:

result.addAll(movies.get(userInput - 1));
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, but stupid question: what is the "result" is that a part of the arraylist vocabulary? And this works for an arraylist of an arraylist?
@LLgood the ArrayList you said you wanted to add them to. Supposedly List<String> result = new ArrayList<>();.
1

following with following code you can iterate through an arrayList

private ArrayList<String> myArrayList = new ArrayList<>();
for(int i=0;i<myArrayList.size();i++){
myArrayList.get(i);
// Perform whatever operation here
}

Let me know if it doesn't work. And also what's the error given

Comments

0

The ArrayList.get(int index) method can help you out.

ArrayList myList = fullList.get(1); would give you your desired list and you can iterate over it like:

for (String currString: myList){ //Do things with currString }

I hope I explained it well :)

Comments

0

You should learn how to use Java Lambdas. you can use a lambda to iterate through an ArrayList since ArrayLists are Iterable.

ArrayList<ArrayList<String>>arrayListofarrayList  = new ArrayList<ArrayList<String>>();
arrayListofarrayList.forEach((arrayList) -> {System.out.println(arrayList.get(1));});

or

ArrayList<ArrayList<String>>arrayListofarrayList  = new ArrayList<ArrayList<String>>();
arrayListofarrayList.forEach((arrayList) -> {\*code here!*\});

if you're trying nest iterations you can:

ArrayList<ArrayList<String>>arrayListofarrayList  = new ArrayList<ArrayList<String>>();
arrayListofarrayList.forEach((arrayList) -> {
arrayList.forEach((item) -> {
    System.out.println(item);
    });
});

Comments

0

Declare your variable as following

private ArrayList<String> arrayList = new ArrayList<>();

Then in a method add following

for(int j=0; j < arrayList.size() ; j++){
arrayList.get(i);
 // Your code goes here
}

This should work

Comments

-1

You can use either for-each loop or simple for loop to print elements in ArrayList of ArrayList. For example,

ArrayList<Integer> list1 = new ArrayList<>();
list1.add(1);list1.add(2);
ArrayList<Integer> list2 = new ArrayList<>();
list2.add(3);list2.add(4);
ArrayList<Integer> list3 = new ArrayList<>();
list3.add(5);list3.add(6);

ArrayList<ArrayList<Integer>> listOfList = new ArrayList<>();
listOfList.add(list1);
listOfList.add(list2);
listOfList.add(list3);

// Printing elements using for-each loop
for(ArrayList<Integer> eachList : listOfList){
  for(Integer elementInList : eachList){
    System.out.print(elementInList + "\t");
  }
  System.out.println();
}

// Printing elements using for loop
for(int i = 0;i < listOfList.size();i++){
  ArrayList<Integer> eachList = listOfList.get(i);
  for(int j = 0;j < eachList.size();j++){
    System.out.print(eachList.get(j) + "\t");
  }
  System.out.println();
}

Output:

enter image description here

Comments

Your Answer

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.