Objects First with Java: A Practical Introduction Using BlueJ (6th Edition)
Objects First with Java: A Practical Introduction Using BlueJ (6th Edition)
6th Edition
ISBN: 9780134477367
Author: David J. Barnes, Michael Kolling
Publisher: PEARSON
Expert Solution & Answer
Book Icon
Chapter 4, Problem 53E

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...

Blurred answer
Students have asked these similar questions
For both ArrayList and LinkedList, which methods do not exist in the List interface? Just why do you think these methods are left out the List?
You are familiar with ArrayList and have used it in different capacities. The most prominent feature of ArrayList is that it expands automatically as elements are added to it. It can be traversed using a for loop, for each loop and iterator( which you have implemented in Lab 2).In this lab you are going to implement your own ArrayList and its functionalities.Task 1.1: Create a class Generic ArrayList with attributes.Task 1.2: Implement a constructor for your ArrayList class.Task 1.3: Implement add() method for adding elements to ArrayList. Remember, ArrayList adds elements dynamically. It doesn't depend on user defined size.Task 1.4: Implement remove() method for removing an element from a specific index. Remember, ArrayList stores elements in a consecutive sequence. If an element is removed from middle you have to re-adjust the elements in the correct sequence. Note: Break down your implementation by defining helper methods for different tasks, rather than implementing your logic in a…
Which methods do ArrayList and LinkedList have that are not defined in the List interface? Why do you think that these methods are not included in List?

Chapter 4 Solutions

Objects First with Java: A Practical Introduction Using BlueJ (6th Edition)

Ch. 4 - Write a method call to add the object held in the...Ch. 4 - Write a method call to remove the third object...Ch. 4 - Suppose that an object is stored at index 6 in a...Ch. 4 - Add a method called checklndex to the...Ch. 4 - Write an alternative version of checkIndex called...Ch. 4 - Rewrite both the 1istFi1e and removeFi1e methods...Ch. 4 - Prob. 17ECh. 4 - Prob. 18ECh. 4 - We know that the first file name is stored at...Ch. 4 - Prob. 20ECh. 4 - Create a MusicOrganizer and store a few file names...Ch. 4 - Create an ArrayList<String> in the Code Pad by...Ch. 4 - If you wish, you could use the debugger to help...Ch. 4 - Challenge exercise The for-each loop does not use...Ch. 4 - Prob. 25ECh. 4 - Prob. 26ECh. 4 - Prob. 27ECh. 4 - Write out the header of a for-each loop to process...Ch. 4 - Suppose we express the first version of the key...Ch. 4 - Write a while loop (for example, in a method...Ch. 4 - Write a while loop to add up the values 1 to 10...Ch. 4 - Write a method called sum with a while loop that...Ch. 4 - Challenge exercise Write a method isPrime (int n)...Ch. 4 - In the findFirst method, the loop's condition...Ch. 4 - Prob. 35ECh. 4 - Have the MusicOrganizer increment the play count...Ch. 4 - Prob. 37ECh. 4 - Prob. 38ECh. 4 - Prob. 39ECh. 4 - Prob. 40ECh. 4 - Complete the numberOfMembers method to return the...Ch. 4 - Prob. 42ECh. 4 - Prob. 43ECh. 4 - Prob. 44ECh. 4 - Challenge exercise Write a method to play every...Ch. 4 - Prob. 46ECh. 4 - Prob. 47ECh. 4 - Add a close method to the Auction class. This...Ch. 4 - Add a getUnsold method to the Auction class with...Ch. 4 - Suppose the Auction class includes a method that...Ch. 4 - Rewrite getLot so that it does not rely on a lot...Ch. 4 - Prob. 52ECh. 4 - Prob. 53ECh. 4 - Prob. 54ECh. 4 - Prob. 55ECh. 4 - Open the products project and complete the...Ch. 4 - Implement the findProduct method. This should look...Ch. 4 - Implement the numberInStock method. This should...Ch. 4 - Prob. 59ECh. 4 - Challenge exercise Implement a method in...Ch. 4 - Java provides another type of loop: the do-while...Ch. 4 - Prob. 85ECh. 4 - Prob. 86ECh. 4 - Find out about Java's switch-case statement. What...
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
  • Text book image
    EBK JAVA PROGRAMMING
    Computer Science
    ISBN:9781337671385
    Author:FARRELL
    Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT