Seção 1 Resumo
Ótimo, a primeira seção está completa, e agora você tem um entendimento básico do Java Collections Framework. Você aprendeu a trabalhar com classes de embrulho, ArrayList
e LinkedList
. Nós até escrevemos nossa versão simplificada de LinkedList
, parte da qual você implementou por conta própria. Vamos dar uma olhada nessa implementação:
main.java
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113package 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(); } }
Sim, a solução acabou por ser bastante extensa, por isso compreendo o incómodo de deslocar-se pelo código numa janela pequena. No entanto, pode executar este código e ver como ele funciona.
Aqui estão os dois métodos que implementou por si próprio:
SinglyLinkedList.java
123456789101112131415161718192021222324252627public 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; }
Sua solução pode diferir ligeiramente da que forneci, mas tudo bem porque a programação é um processo criativo, e o código de cada um pode ser completamente diferente enquanto desempenha as mesmas funções.
Vamos revisar as principais perguntas que estiveram presentes nesta seção:
1. O que acontece quando um ArrayList
excede sua capacidade atual?
2. Qual afirmação é verdadeira ao comparar LinkedList e ArrayList em Java?
3. Qual é a classe wrapper para o tipo primitivo char
em Java?
4. Qual método é utilizado para adicionar um elemento ao final de um ArrayList
em Java?
5. Em uma LinkedList
, cada nó contém uma referência para:
6. Qual é a classe wrapper para o tipo primitivo int
em Java?
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
Awesome!
Completion rate improved to 4
Seção 1 Resumo
Deslize para mostrar o menu
Ótimo, a primeira seção está completa, e agora você tem um entendimento básico do Java Collections Framework. Você aprendeu a trabalhar com classes de embrulho, ArrayList
e LinkedList
. Nós até escrevemos nossa versão simplificada de LinkedList
, parte da qual você implementou por conta própria. Vamos dar uma olhada nessa implementação:
main.java
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113package 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(); } }
Sim, a solução acabou por ser bastante extensa, por isso compreendo o incómodo de deslocar-se pelo código numa janela pequena. No entanto, pode executar este código e ver como ele funciona.
Aqui estão os dois métodos que implementou por si próprio:
SinglyLinkedList.java
123456789101112131415161718192021222324252627public 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; }
Sua solução pode diferir ligeiramente da que forneci, mas tudo bem porque a programação é um processo criativo, e o código de cada um pode ser completamente diferente enquanto desempenha as mesmas funções.
Vamos revisar as principais perguntas que estiveram presentes nesta seção:
1. O que acontece quando um ArrayList
excede sua capacidade atual?
2. Qual afirmação é verdadeira ao comparar LinkedList e ArrayList em Java?
3. Qual é a classe wrapper para o tipo primitivo char
em Java?
4. Qual método é utilizado para adicionar um elemento ao final de um ArrayList
em Java?
5. Em uma LinkedList
, cada nó contém uma referência para:
6. Qual é a classe wrapper para o tipo primitivo int
em Java?
Obrigado pelo seu feedback!