Explanation of Solution
Given: The class named ArrayList is discovered inside the java.util package, which includes another class known as Linked List.
To find:
The information about LinkedList.
The comparison between ArrayList and LinkedList.
The methods that are similar and different between LinkedList and Arraylist.
Solution:
The implementation of LinkedList is slightly different from that of ArrayList in Java. These are:
LinkedList preserves its elements in the form of doubly-linked list whereas in case of ArrayList, it's a simple dynamic array which expands or shrinks according to the number of elements added to it.
We can also insert elements at any place in a LinkedList since it retains the track of linking between previous and next elements, whereas in case of ArrayList we can also insert elements at any position in the list but it requires shifting of all the latter elements to create space for new elements.
When huge number of elements are added to an ArrayList which if crosses its underlying size of the array, then a new array double of its size is reserved and the old array gets copied into the new one. But in case if LinkedList no such thing happens, since its underlying structure is not an array, a list of elements each of which is linked together by its previous and next memory locations.
The methods which are common to both the ArrayList and LinkedList, are given below with their workings:
add(int index, Object element) – used to add an element at particular index of the list.
add(Object element) - appends an element at the termination of the list.
addAll(Collection c) - adds a collection at the termination of the list.
addAll(int index, Collection c) - inserts a collection at a specified index of the list.
clear() - removes all the elements of the list.
clone() - copies an instance of the list to a new array and then returns that array.
contains(Object element) - checks to see that the specified element exists in the list or not. If so, it returns true else returns false.
get(int index) – used to return the element of the stated index of the list.
indexOf(Object element) – used to return an index for the first occurrence of the specific element that is stated in the list if it is found, or else it returns -1...
Want to see the full answer?
Check out a sample textbook solutionChapter 4 Solutions
Objects First with Java: A Practical Introduction Using BlueJ (6th Edition)
- In our demo program, we compared the efficiency of ArrayList and LinkedList byadding a number of items to beginning of these two kinds of lists. Which kind of list(ArrayList or LinkedList) takes less time to add? Briefly explain why is that? Youmay draw diagrams to explain.arrow_forwardImport the ArrayList and List classes from the java.util package to create a list of phone numbers and also import the HashSet and Set classes from the java.util package to create a set of unique prefixes. Create a class called PhoneNumberPrefix with a main method that will contain the code to find the unique prefixes. Create a List called phoneNumbers and use the add method to add several phone numbers to the list. List<String> phoneNumbers = new ArrayList<>(); phoneNumbers.add("555-555-1234"); phoneNumbers.add("555-555-2345"); phoneNumbers.add("555-555-3456"); phoneNumbers.add("444-444-1234"); phoneNumbers.add("333-333-1234"); Create a Set called prefixes and use a for-each loop to iterate over the phoneNumbers list. For each phone number, we use the substring method to extract the first 7 characters, which represent the prefix, and add it to the prefixes set using the add method. Finally, use the println method to print the prefixes set, which will contain all of…arrow_forwardCreate a method from the implementation perspective. Create a method where it takes in a linked chain of values and adds them in order to the front. method header:public void addToFront(ANode<T> first)arrow_forward
- What is the difference in the result of returning the words in a HashSet compared with returning them in an ArrayList?arrow_forwardHi, I am not sure what's wrong with my code. Can you please check why it is giving me an error? In the starter file is a partial implementation of a doubly-linked list in DoublyLinkedList.java. We will write three new methods in this class to provide additional functionality. Write a method addFirst that adds a new element at the beginning of a DoublyLinkedList. Write a method addLast that adds a new element at the end of a DoublyLinkedList. Write a method removeFirst that removes and returns the first element of a DoublyLinkedList. Try to keep your implementations as simple as possible. For example, recall this definition of addFirst in the (Singly) LinkedList class: public void addFirst(E value) { head = new Node(value, head); } In the DoublyLinkedList class, you will need to keep the three instance variables head, tail, and count updated in all methods. Note that addFirst and addLast will be symmetric to each other, as will removeFirst and removeLast (provided in the…arrow_forwardin java please Write a main method that adds the numbers from 1 to 10 to a LinkedList. The LinkedList is provided in java.util.LinkedList. Use iterator to print all even numbers. Explain why we use iterator instead of using index to print the numbers in the array. linked listarrow_forward
- What is the difference between the ArrayList class and the LinkedList class?arrow_forwardCould somebody assist me with me? I'd very much appreciate itarrow_forwardWhich methods from the List interface are not supported by ArrayList and LinkedList? Explain why you think such methods aren't a part of List.arrow_forward
- Please answer the question in the screenshot. The language used is Java.arrow_forwardYou will implement some methods in the UndirectedGraph class. The UndirectedGraph class contains two nested classes, Vertex and Node, you should NOT change these classes. The graph will store a list of all the vertices in the graph. Each vertex has a pointer to its adjacent vertices. To get started, import the starter file, Undirected.java into the graphs package you create in a new Java Project. Please do not change any of the method signatures in the class, but you can add any helper methods you deem necessary. Implement the methods described below. You are free to test your code however you prefer. Vertex Class (DO NOT EDIT)The vertex class holds information about the vertices in the graph. It has an int val, a Vertex next that refers to the next vertex in the list of vertices (not necessarily an adjacent vertex), and a Node edge that starts the list of adjacent vertices. Node Class (DO NOT EDIT)This is a simple class to represent an adjacency list of a vertex in the graph.…arrow_forwardYou will implement some methods in the UndirectedGraph class. The UndirectedGraph class contains two nested classes, Vertex and Node, you should NOT change these classes. The graph will store a list of all the vertices in the graph. Each vertex has a pointer to its adjacent vertices. To get started, import the starter file, Undirected.java into the graphs package you create in a new Java Project. Please do not change any of the method signatures in the class, but you can add any helper methods you deem necessary. Implement the methods described below. You are free to test your code however you prefer. Vertex Class (DO NOT EDIT)The vertex class holds information about the vertices in the graph. It has an int val, a Vertex next that refers to the next vertex in the list of vertices (not necessarily an adjacent vertex), and a Node edge that starts the list of adjacent vertices. Node Class (DO NOT EDIT)This is a simple class to represent an adjacency list of a vertex in the graph.…arrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT