I want to create linked list of type LinkedList<LinkedList<Integer>>.

I plan to take integer values from user (via console), create a linkedlist by adding the values. Create multiple such linked lists and then create a master linkedlist which will have individual linked lists as its elements.

Example:
User types 1 and 2. It creates linked list LL1 with elements 1 and 2. LL1 is added as first element in master linked list MLL.
User types 3 and 4. It creates linked list LL2 with elements 3 and 4. LL2 is added as second element in master linked list MLL.

After this, I print elements extracted using get method of LinkedList class.

I expect to see elements 1, 2, 3, 4 (in that order). But instead output is 3, 4, 3, 4.
Second element overwrites previous first element.

Please refer to the following code. Please help me correct it to get the expected behaviour.

import java.io.*;
import java.util.*;

public class CreateLinkedListOfCollection
{			
  public CreateLinkedListOfCollection()
  {
  }

  public LinkedList getTestCases()
  {
    LinkedList<LinkedList<Integer>> listOfTestCases = new LinkedList<LinkedList<Integer>>();

    LinkedList<Integer> testcaseValues = new LinkedList<Integer>();

    Scanner sc = new Scanner(System.in);

    System.out.println("Enter the no of testcases to be created:");

    BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
			 
    int numTestCases = sc.nextInt();

    for(int i=0; i<numTestCases; i++)
    {
      testcaseValues.clear();

      System.out.println("Enter 1st input Value:");
				
      try
      {
        String input = br.readLine();
        Integer inputInt = Integer.parseInt(input);
        testcaseValues.add(inputInt);
      }
      catch (IOException ioe)
      {
        System.out.println("\nGenerated IOException");
      }	

      System.out.println("Enter 2nd input Value:");

      try
      {
        String input = br.readLine();
        Integer inputInt = Integer.parseInt(input);
        testcaseValues.add(inputInt);
      }
      catch (IOException ioe)
      {
        System.out.println("\nGenerated IOException");
      }	

      System.out.println("Values of current testcase are:");

      for(int l=0; l<testcaseValues.size(); l++)
      {
        System.out.println(testcaseValues.get(l));	
      }

      listOfTestCases.addLast(testcaseValues);//------------THIS LINE IS GIVING PROBLEMS-------------

      System.out.println("As of now, values for test cases in list are:");

//      int sizeOfLinkedList = listOfTestCases.size();

      for(int testcaseCount=0; testcaseCount<listOfTestCases.size(); testcaseCount++)
      {
        LinkedList<Integer> listElementCur = listOfTestCases.get(testcaseCount);

	for(int valueCount=0; valueCount<listElementCur.size(); valueCount++)
        {
          Integer valueCur = listElementCur.get(valueCount);
          System.out.println(valueCur);
        } 
      }
    }  			

    System.out.println("At the end of creation, values for test cases in list are:");

//    int sizeOfCreatedLinkedList = listOfTestCases.size();

    for(int testcaseCount=0; testcaseCount<listOfTestCases.size(); testcaseCount++)
    {
        LinkedList<Integer> listElementFinal = listOfTestCases.get(testcaseCount);

	for(int valueCount=0; valueCount<listElementFinal.size(); valueCount++)
        {
          Integer valueFinal = listElementFinal.get(valueCount);
          System.out.println(valueFinal);
        } 
    }

    return listOfTestCases;
  }			

  public static void main(String[] args)
  {
    CreateLinkedListOfCollection letsCreate = new CreateLinkedListOfCollection();

   LinkedList output = letsCreate.getTestCases();
  }						
}

testcaseValues always indicate the same object. Final effect you can see.
In the line 26 you should create new object
Put line 14 in place of line 26

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.