title: Can you have an ArrayList that contains Singly Linked Lists?
body: <p>Im trying to create an ArrayList that contains a Singly Linked List in each index.
  The outputList I'm trying to create is going to hold singly linked lists which contain Strings that are sorted (right now the sorting doesn't matter if I cant get variables into the Singly Linked lists and the SLL into the ArrayList). Im curious if singly linked lists can be added into ArrayLists.</p>
  <pre><code>SinglyLinkedList sList = new SinglyLinkedList();
  ArrayList&lt;String&gt; outputList = new ArrayList&lt;String&gt;();
              int size = inputList.size();
              for(int i = 0;i &lt; (size-1);i++) {
                  if(i==0) {
                      outputList.add(sList.addNode(inputList.get(i)));
                  }
              }
  </code></pre>
  <p>inputList is an ArrayList that holds Strings.</p>
  <p>SinglyLinkedList is defined by the code below.</p>
  <pre><code>public class SinglyLinkedList {
  
  //From: https://www.javatpoint.com/java-program-to-create-and-display-a-singly-linked-list
  class Node{
      String data;
      Node next;
      
      public Node(String data) {
          this.data = data;
          this.next = null;
      }
  }
  
  public Node head = null;
  public Node tail = null;
  
  public void addNode(String data) {
      
      Node newNode = new Node(data);
      
      if(head == null) {
          head = newNode;
          tail = newNode;
      }
      else {
          tail.next = newNode;
          tail = newNode;
      }
  }
  
  public void display() {
      Node current = head;
      
      if(head == null) {
          System.out.println(&quot;List is Empty/Null&quot;);
          return;
      }
      
      System.out.println(&quot;Nodes of this singly linked list are: &quot;);
      while(current != null) {
          System.out.print(current.data + &quot; &quot;);
          current = current.next;
      }
      System.out.println();
  }
  </code></pre>
  <p>As suggested by an answer I used;</p>
  <pre><code>List&lt;List&lt;String&gt;&gt; outputList = new ArrayList&lt;&gt;();
  List&lt;String&gt; singleList = (List&lt;String&gt;) new SinglyLinkedList();
  </code></pre>
  <p>However the second line gives a runtime error,</p>
  <pre><code>Exception in thread &quot;main&quot; java.lang.ClassCastException: class SinglyLinkedList cannot be cast to class java.util.List (SinglyLinkedList is in unnamed module of loader 'app'; java.util.List is in module java.base of loader 'bootstrap')
      at SinglyLinkedList.main(SinglyLinkedList.java:89)
  </code></pre>