. Advertisement .
..3..
. Advertisement .
..4..
Knowing how to create array of linked lists in Java is an important skill for anyone working with the Java language. This tutorial will teach you how to complete this task.
The LinkedList is a linear and sequential data format that keeps elements in one specific sequence. This data format is contained in the package java.util and is a subset of the interface Collection.
The node portion of the linked lists stores elements. Each has its own pointer to store the subsequent node’s address and a data portion for storing elements. Keep in mind that the list’s elements do not exist in adjacent memory regions.
Method 1: Utilize An Array To Create The Array Of Linked Lists In Java
The code below uses loops to build a linked lists array.
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]);
}
}
}
The new LinkedList[5]
command creates the linked list in the code above. Here, the keyword new calls the class LinkedList’s public constructor. The 5 value emphasizes the array size, meaning you will get an array containing 5 linked lists.
The loop iterates across the list variable, creating a fresh linked list for every node. As a result, a loop for is used, and it begins to execute with the condition that the int value is smaller than 5. The loop checks whether the node’s value is null and, if not, creates an additional node.
Once more, a loop for is utilized to populate the elements of a list. The list’s final elements are added using the add function. This method is part of the class LinkedList and gives the value boolean. Suppose the add method is completed; it will return the value true. Otherwise, it will return false.
The iteration proceeds similarly, while a linked list is utilized to fill the value of every node. The identical is also written within the loop to verify the nodes’ elements exist.
Output:
[0][0, 1][0, 1, 2][0, 1, 2, 3][0, 1, 2, 3, 4]
Method 2: Utilize A Construction To Create The Array Of Linked Lists In Java
You can find the code that generates a linked lists array using Java 8 functions and features below.
import java.util.ArrayList;
import java.util.LinkedList;
public class main {
public static void main(String[] args)
{
LinkedList list1 = new LinkedList<String>();
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<LinkedList> arrayList = new ArrayList<LinkedList>();
arrayList.add(list1);
arrayList.add(list2);
arrayList.add(list1);
arrayList.forEach(System.out::println);
System.out.println("Classname: " + arrayList.get(0).getClass());
}
}
In this case, the main function contains the logic for code execution. First, the keyword new is used to construct the linked list. This keyword calls the class LinkedList’s public constructor. In the list, the strings are added. Two more lists are made in a similar manner.
After that, a list of arrays is created. These constructed list variables are appended to the list instance. You can iterate this instance by utilizing the forEach method. To display the instance type name shown in your array list, a statement print using the method reference in Java, ::, is appended.
Output:
[l1_value1, l1_value2]
[l2_value1, l2_value2]
[l1_value1, l1_value2]
Classname: class java.util.LinkedList
The Bottom Line
Above are the methods you can use to create array of linked lists in Java. It is up to you to decide which one you want to rely on.Bonus: if you want to learn more Java skills, check out this guide on putting arrays in an array list.
Leave a comment