Explain when it would be better to use an array than a linked list to store a co
ID: 3693969 • Letter: E
Question
Explain when it would be better to use an array than a linked list to store a collection of data.
Explain when it would be better to use a linked list than an array to store a collection of data.
Suppose I have an array of ints called numbers. Suppose further that I am using a variable called count to keep track of how many values are currently stored in the array, and I’m using capacity to store how many slots make up the array. Write code that will detect when the array is more than 80% full and resize it so that it will then be only 50% full. (3 points)
Explain an advantage of using a java.util.ArrayList over the arrays we’ve been coding for most of this semester.
Suppose I have a linked list whose first node is pointed to by the variable first. Write code that will remove the very last node in the list. (2 points)
With a linked list, when you get to the end of the list, the next field of that last node is null. What change might you make to have the list be circular, so that once you get to the end, you can easily restart at the beginning of the list.
The Queue we programmed in class suffered from a performance problem: when we removed an entry from the front of the list, we had to shift all the other entries in the list down. Describe how you would improve this design.
What does it mean to say that a particular data type implements the Comparable interface?
Write a main function that will create and populate an ArrayList of double values and then find and print the minimum value in that list. (3 points)
Explanation / Answer
1.array are better to be used when:
a)Arrays can be allocated in a single block.
b)Arrays are used for faster random access.
c)Arrays use less memory (linked lists need one or two extra pointers per element).
2.Linked list should be used instead of arrays when
a)when we need contant time insertions or deletions from the list.
b)when we dont dont know how many items will be there in the list instead of declating in the array at beginning.
c)when we want to insert elemnts somewhere between the array without random access.
3.
4.The main advantage to an ArrayList is that it automatically expands as data is added. It allocates elements dynamically to the arraylist as soon as theya re added unline in array where size needs to be mentioned before.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.