From f855a760922fda49f019e5f00b18ee54c1c70306 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=B0=D0=B4=D0=B6=D0=B8=D0=BA=D1=83=D1=80=D0=B1?= =?UTF-8?q?=D0=B0=D0=BD=D0=BE=D0=B2=20=D0=A2=D0=B5=D0=BC=D0=B8=D1=80=D0=BB?= =?UTF-8?q?=D0=B0=D0=BD?= Date: Tue, 25 Aug 2026 13:25:03 +0300 Subject: [PATCH] fix: correct minor syntax issues --- .../ru/project/collection/MyLinkedList.java | 28 ++++++++++--------- .../java/ru/project/collection/MyList.java | 2 +- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/ru/project/collection/MyLinkedList.java b/app/src/main/java/ru/project/collection/MyLinkedList.java index 7164857..23b2b75 100644 --- a/app/src/main/java/ru/project/collection/MyLinkedList.java +++ b/app/src/main/java/ru/project/collection/MyLinkedList.java @@ -1,13 +1,13 @@ +import java.util.Arrays; public class MyLinkedList implements MyList { - private Node head; + private Node head; private int size; - public void add(T value) { if (head == null) { this.head = new Node<>(value); } else { - Node temp = head; + Node temp = head; while (temp.getNext() != null) { temp = temp.getNext(); } @@ -18,12 +18,12 @@ public class MyLinkedList implements MyList public T get(int index) { int currentIndex = 0; - Node temp = head; + Node temp = head; while(temp != null) { if (currentIndex == index) { - return (T) temp.getValue(); + return temp.getValue(); } else { @@ -36,6 +36,9 @@ public class MyLinkedList implements MyList } public void remove(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } if (index == 0) { head = head.getNext(); @@ -44,7 +47,7 @@ public class MyLinkedList implements MyList } int currentIndex = 0; - Node temp = head; + Node temp = head; while (temp != null) { if (currentIndex == index-1) @@ -64,16 +67,15 @@ public class MyLinkedList implements MyList throw new IllegalArgumentException(); } - Node temp = head; + Node temp = head; for (int i = 0; i < index; i++) { temp = temp.getNext(); } - T oldValue = (T) temp.getValue(); + T oldValue = temp.getValue(); temp.setValue(value); return oldValue; } - public int size() { return size; } @@ -81,10 +83,11 @@ public class MyLinkedList implements MyList { return size == 0; } + @Override public String toString() { Object[] res = new Object[size]; int index = 0; - Node temp = head; + Node temp = head; while (temp != null) { res[index] = temp.getValue(); index++; @@ -92,7 +95,8 @@ public class MyLinkedList implements MyList } return Arrays.toString(res); } - public static class Node { + + private static class Node { private T value; private Node next; @@ -116,6 +120,4 @@ public class MyLinkedList implements MyList return next; } } - - } \ No newline at end of file diff --git a/app/src/main/java/ru/project/collection/MyList.java b/app/src/main/java/ru/project/collection/MyList.java index e97132e..bd47567 100644 --- a/app/src/main/java/ru/project/collection/MyList.java +++ b/app/src/main/java/ru/project/collection/MyList.java @@ -8,7 +8,7 @@ public interface MyList { T set(int index, T element); - T remove(int index); + void remove(int index); int size();