Linked list in array

  1. HowTo
  2. Java Howtos
  3. Array of Linked List in Java

Array of Linked List in Java

Java Java Linked List Java Array

Created: July-24, 2021 | Updated: October-19, 2021

A LinkedList is the sequential and linear data structure that stores elements in a defined order. The data structure is a part of the Collection interface and is present in the java.util package.

The linked list also has elements stored in the node part. Each node has a data part for element storage and the pointer to keep the address of the next node. The elements in the list are not present in contiguous memory locations.

Demonstrate the Linked List Array Using a Traditional Array in Java

Below is the code block that creates an array of linked lists using loops.

import java.util.LinkedList; public class Main { public static void main[String[] args] { LinkedList[] list = new LinkedList[5]; for [int i = 0; i < 5; i++] { if [list[i] == null] { list[i] = new LinkedList[]; int temp = i; for [int j = 0; j < temp + 1; j++] { list[i].add[j]; } } System.out.print[list[i]]; } } }

In the block above, the new LinkedList[5] statement creates a linked list. The new keyword calls the public constructor of the class linked list. The value 5 demonstrates the size of the array, so we create an array of 5 linked lists.

Over the list variable, the loop runs to instantiate a new linked list over each node. So a for loop gets applied with a condition of integer value less than 5 starts running. Internally, it checks the condition if the value in the node is null; otherwise, a new linked list node instantiates.

Again, a for loop gets used to fill the elements in the list. The add method is used to append the elements at the end of the list. The method is from the LinkedList class and returns the boolean value. It returns true if the add function gets successfully done, else false.

Similarly, the iteration continues, and each nodes value gets filled with the linked list. The same is also printed inside the loop to check the elements present in the nodes.

Output:

[0][0, 1][0, 1, 2][0, 1, 2, 3][0, 1, 2, 3, 4]

Demonstrate the Linked List Array Using the Constructor in Java

Below is the code block that uses Java 8 features and functions to create an array of linked lists.

import java.util.ArrayList; import java.util.LinkedList; public class main { public static void main[String[] args] { LinkedList list1 = new LinkedList[]; list1.add["l1_value1"]; list1.add["l1_value2"]; LinkedList list2 = new LinkedList[]; list2.add["l2_value1"]; list2.add["l2_value2"]; LinkedList list3 = new LinkedList[]; list3.add["l3_value1"]; list3.add["l3_value2"]; ArrayList arrayList = new ArrayList[]; arrayList.add[list1]; arrayList.add[list2]; arrayList.add[list1]; arrayList.forEach[System.out::println]; System.out.println["Classname: " + arrayList.get[0].getClass[]]; } }

In the code block above, the main method holds the actual logic for the code execution.

Firstly, a linked list is created using the new keyword. The keyword invokes the public constructor of the LinkedList class. The string values get inserted into the list. Similarly, another two lists are created.

Finally, an array list gets instantiated. The list variables formed are added in the array list instance. The iteration of the list instance happens using the forEach function. Additionally, a print statement with the method reference operator, ::, is added to show the name of instance type present in the array list.

The output of the program above is shown below.

[l1_value1, l1_value2] [l2_value1, l2_value2] [l1_value1, l1_value2] Classname: class java.util.LinkedList
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Java Linked List

  • Doubly Linked List in Java

    Related Article - Java Array

  • Pass an Array to a Method in Java
  • Increase an Array Size in Java
    • The break Statement in Java
    • Create Unsigned Int in Java
    report this ad

    Video liên quan

    Chủ Đề