Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Section 1 Summary | Basic Data Structures
Java Data Structures
course content

Contenido del Curso

Java Data Structures

Java Data Structures

1. Basic Data Structures
2. Additional Data Structures
3. Map
4. enum & Stream API

bookSection 1 Summary

Great, the first section is complete, and now you have a grasp of the basics of the Java Collections Framework. You've learned how to work with wrapper classes, ArrayList, and LinkedList. We even wrote our simplified version of LinkedList, part of which you implemented yourself. Let's take a look at this implementation:

java

main

copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
package com.example; class Node { int data; Node next; public Node(int data) { this.data = data; this.next = null; } } class SinglyLinkedList { private Node head; public SinglyLinkedList() { this.head = null; } public void append(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; return; } Node current = head; while (current.next != null) { current = current.next; } current.next = newNode; } public void display() { Node current = head; while (current != null) { System.out.print(current.data + " "); current = current.next; } System.out.println(); } public void update(int index, int newData) { if (index < 0 || index >= size()) { System.out.println("Invalid index"); return; } Node current = head; for (int i = 0; i < index; i++) { current = current.next; } current.data = newData; } public void delete(int index) { if (index < 0 || index >= size()) { System.out.println("Invalid Index"); return; } if (index == 0) { head = head.next; return; } Node current = head; for (int i = 0; i < index - 1; i++) { current = current.next; } current.next = current.next.next; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.next; } return count; } } public class Main { public static void main(String[] args) { SinglyLinkedList linkedList = new SinglyLinkedList(); // Adding elements to the end of the list (Create) linkedList.append(1); linkedList.append(2); linkedList.append(3); linkedList.append(4); // Displaying the content of the list (Read) System.out.println("Contents of the list:"); linkedList.display(); // Updating a value by index (Update) linkedList.update(2, 10); System.out.println("List after updating the value at index 2:"); linkedList.display(); // Deleting an element by index (Delete) linkedList.delete(1); System.out.println("List after deleting the element at index 1:"); linkedList.display(); } }

Yes, the solution turned out to be quite large, so I understand the inconvenience of scrolling through the code in a small window. However, you can run this code and see how it works.

Here are the two methods that you implemented yourself:

java

SinglyLinkedList

copy
123456789101112131415161718192021222324252627
public void delete(int index) { if (index < 0 || index >= size()) { System.out.println("Invalid Index"); return; } if (index == 0) { head = head.next; return; } Node current = head; for (int i = 0; i < index - 1; i++) { current = current.next; } current.next = current.next.next; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.next; } return count; }

Your solution may differ slightly from the one I provided, but that's okay because programming is a creative process, and everyone's code can be completely different while serving the same functions.

Let's go over the main questions that were present in this section:

1. What happens when an `ArrayList` exceeds its current capacity?
2. Which statement is true when comparing LinkedList and ArrayList in Java?
3. What is the wrapper class for the primitive type `char` in Java?
4. Which method is used to add an element to the end of an `ArrayList` in Java?
5. In a `LinkedList`, each node contains a reference to:
6. What is the wrapper class for the primitive type `int` in Java?
What happens when an `ArrayList` exceeds its current capacity?

What happens when an ArrayList exceeds its current capacity?

Selecciona la respuesta correcta

Which statement is true when comparing LinkedList and ArrayList in Java?

Which statement is true when comparing LinkedList and ArrayList in Java?

Selecciona la respuesta correcta

What is the wrapper class for the primitive type `char` in Java?

What is the wrapper class for the primitive type char in Java?

Selecciona la respuesta correcta

Which method is used to add an element to the end of an `ArrayList` in Java?

Which method is used to add an element to the end of an ArrayList in Java?

Selecciona la respuesta correcta

In a `LinkedList`, each node contains a reference to:

In a LinkedList, each node contains a reference to:

Selecciona la respuesta correcta

What is the wrapper class for the primitive type `int` in Java?

What is the wrapper class for the primitive type int in Java?

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 8
some-alt