Answer to Question #171071 in Java | JSP | JSF for Katie

Question #171071
  • Of the containers described in this week’s reading, explain which you prefer for both storing and accessing data. 
  • Take note of the different methods/actions available to each container and the degree to which their actions are efficient. What benefits does this container have over the other containers presented?
  • What are some of the drawbacks of this container? Provide a code example that supports your comments.
1
Expert's answer
2021-03-12T07:40:31-0500

There is a advantages and disadvantage of using linked list as a container-

  • Continuous memory location is not required for the RAM which is the advantage over the Array, Stack etc.

Insertion of elements in the list:

void insert():

  • The insertion of the elements in the list at new places be O(1) but if we are inserting new elements in the array or some other container then we need to create the new array or the new elements.
  • The dynamic insertion and deletion is not applicable for the array or other elements but in the list, it is easy to insert and update the elements.

Deletion of the elements from the list:

  • void delete() : if the application call this function, then it removes the last elements which was returned by next() or previous().

Disadvantages:

  • For this, the indexing and the random access is not allowed.
  • As here pointer store the memory due to which the unnecessary use of the memory gets increase with respect to other container such as array.
  • As here data is not stored in the continuous memory location hence cache is not much friendly.
import java.util.*;

public class ListContainerTest {

    public static void main(String args[])
    {
        // Creating object of the
        // class linked list
        LinkedList<String> lct
            = new LinkedList<String>();

        // insertion of new elements
        lct.add("x");
        lct.add("y");
        lct.addLast("z");
        lct.addFirst("s");
        lct.add(2, "t");

        System.out.println(lct);

        lct.remove("s");
        lct.remove(2);
        lct.removeFirst();
        lct.removeLast();

        System.out.println(lct);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog