how to add a list in a list java

how to add a list in a list java

To add a list to a list in Java, you can use the add method of the java.util.List interface. This method adds an element to the end of the list.

Here's an example of how you could use the add method to add a list to a list:

import java.util.ArrayList;
import java.util.List;

public class Main {
  public static void main(String[] args) {
    List<String> list1 = new ArrayList<>();
    list1.add("Apple");
    list1.add("Banana");

    List<String> list2 = new ArrayList<>();
    list2.add("Carrot");
    list2.add("Potato");

    List<List<String>> listOfLists = new ArrayList<>();
    listOfLists.add(list1);
    listOfLists.add(list2);

    System.out.println(listOfLists); // [[Apple, Banana], [Carrot, Potato]]
  }
}
Sour‮w:ec‬ww.lautturi.com

In this example, the list1 and list2 lists are created using the ArrayList implementation of the List interface. The list1 list contains two strings, and the list2 list contains two other strings.

The listOfLists list is also created using the ArrayList implementation, and it is used to store the two lists.

Created Time:2017-11-01 12:05:09  Author:lautturi