Last active
September 7, 2025 08:21
-
-
Save sunmeat/c3dccc2b0e9fbed740dc to your computer and use it in GitHub Desktop.
custom array list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.alex.collections; | |
class MyArrayList { | |
private int size = 0; | |
private int capacity = 10; | |
private int[] data; | |
public MyArrayList() { | |
// data = new int[capacity]; // замість копіювання коду - застосовується делегування конструкторів! | |
this(10); | |
} | |
public MyArrayList(int capacity) { | |
if (capacity < 10) { | |
capacity = 10; | |
} | |
this.capacity = capacity; | |
data = new int[capacity]; | |
} | |
// забезпечення достатньої ємності масиву | |
private void ensureCapacity(int minCapacity) { | |
if (minCapacity > capacity) { | |
int newCapacity = Math.max(capacity * 2, minCapacity); | |
int[] newData = new int[newCapacity]; | |
System.arraycopy(data, 0, newData, 0, size); | |
data = newData; | |
capacity = newCapacity; | |
} | |
} | |
// додавання елемента в кінець списку | |
public void pushBack(int value) { | |
ensureCapacity(size + 1); | |
data[size++] = value; | |
} | |
// додавання елемента на початок списку | |
public void pushFront(int value) { | |
ensureCapacity(size + 1); | |
for (int i = size; i > 0; i--) { | |
data[i] = data[i - 1]; | |
} | |
data[0] = value; | |
size++; | |
} | |
// очищення списку | |
public void clear() { | |
for (int i = 0; i < size; i++) { | |
data[i] = 0; | |
} | |
size = 0; | |
} | |
// перевірка, чи список порожній | |
public boolean isEmpty() { | |
return size == 0; // краса :) | |
} | |
// виведення списку на екран | |
public void print() { | |
if (isEmpty()) { | |
System.out.println("Колекція порожня."); | |
} else { | |
for (int i = 0; i < size; i++) { | |
System.out.print(data[i] + " "); | |
} | |
System.out.println(); | |
} | |
} | |
// отримання елемента за індексом | |
public int get(int index) { | |
if (index < 0 || index >= size) { | |
throw new IndexOutOfBoundsException("Недійсний індекс: " + index); | |
} | |
return data[index]; | |
} | |
// зміна елемента за індексом | |
public void set(int index, int value) { | |
if (index < 0 || index >= size) { | |
throw new IndexOutOfBoundsException("Недійсний індекс: " + index); | |
} | |
data[index] = value; | |
} | |
// видалення елемента за індексом | |
public void remove(int index) { | |
if (index < 0 || index >= size) { | |
throw new IndexOutOfBoundsException("Недійсний індекс: " + index); | |
} | |
for (int i = index; i < size - 1; i++) { | |
data[i] = data[i + 1]; | |
} | |
data[size - 1] = 0; | |
size--; | |
} | |
// видалення останнього елемента | |
public void popBack() { | |
if (isEmpty()) { | |
throw new IllegalStateException("Список порожній"); | |
} | |
data[size - 1] = 0; | |
size--; | |
} | |
// видалення першого елемента | |
public void popFront() { | |
if (isEmpty()) { | |
throw new IllegalStateException("Список порожній"); | |
} | |
for (int i = 0; i < size - 1; i++) { | |
data[i] = data[i + 1]; | |
} | |
data[size - 1] = 0; | |
size--; | |
} | |
// повернення поточного розміру списку | |
public int size() { | |
return size; | |
} | |
// повернення поточної ємності масиву | |
public int capacity() { | |
return capacity; | |
} | |
// пошук першого входження елемента | |
public int indexOf(int value) { | |
for (int i = 0; i < size; i++) { | |
if (data[i] == value) { | |
return i; | |
} | |
} | |
return -1; | |
} | |
// перевірка, чи містить список елемент | |
public boolean contains(int value) { | |
return indexOf(value) != -1; | |
} | |
// повернення копії масиву елементів | |
public int[] toArray() { | |
int[] result = new int[size]; | |
System.arraycopy(data, 0, result, 0, size); | |
return result; | |
} | |
// зменшення ємності до поточного розміру | |
public void trimToSize() { | |
if (size < capacity) { | |
int[] newData = new int[size]; | |
System.arraycopy(data, 0, newData, 0, size); | |
data = newData; | |
capacity = size; | |
} | |
} | |
} | |
class Program { | |
public static void main(String[] args) { | |
// тести роботи з еррейлістом: | |
var ar = new MyArrayList(); | |
ar.pushBack(12); | |
ar.pushBack(18); | |
ar.pushBack(16); | |
System.out.println("Після додавання елементів у кінець:"); | |
ar.print(); | |
ar.pushFront(14); | |
System.out.println("Після додавання елемента на початок:"); | |
ar.print(); | |
System.out.println("Розмір: " + ar.size() + ", Ємність: " + ar.capacity()); | |
ar.set(1, 20); | |
System.out.println("Після заміни елемента за індексом 1:"); | |
ar.print(); | |
ar.remove(2); | |
System.out.println("Після видалення елемента за індексом 2:"); | |
ar.print(); | |
ar.popFront(); | |
System.out.println("Після видалення першого елемента:"); | |
ar.print(); | |
ar.popBack(); | |
System.out.println("Після видалення останнього елемента:"); | |
ar.print(); | |
System.out.println("Чи містить 20? " + ar.contains(20)); | |
System.out.println("Індекс 20: " + ar.indexOf(20)); | |
ar.clear(); | |
System.out.println("Після очищення:"); | |
ar.print(); | |
System.out.println("Чи порожній? " + ar.isEmpty()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment