/
Pereroncino
/
dzzd
Обзор
Документация
Войти
/
Pereroncino
/
dzzd
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
dz.java
58 строк
2 KB
Pereroncino
upload files
20 ноя 2025, 08:14
20 ноя 2025, 08:14
90b60a3
Код
Авторство
О чём код?
import java.util.LinkedList; public class Cache<T> { private final LinkedList<T> list; private final int capacity; public Cache(int n) { this.capacity = n; this.list = new LinkedList<>(); } public void add(T item) { if (list.size() == capacity) { list.removeFirst(); } list.addLast(item); } public boolean remove(T item) { return list.remove(item); } public boolean exists(T item) { return list.contains(item); } public T getFirst() { return list.isEmpty() ? null : list.getFirst(); } public T getLast() { return list.isEmpty() ? null : list.getLast(); } public T getItemByIndex(int i) { if (i < 0 || i >= list.size()) return null; return list.get(i); } } class TestCache { public static void main(String[] args) { Cache<String> cache = new Cache<>(3); cache.add("one"); cache.add("two"); cache.add("three"); System.out.println("First: " + cache.getFirst()); System.out.println("Last: " + cache.getLast()); cache.add("four"); System.out.println("First после добавления four: " + cache.getFirst()); System.out.println("Exists 'two'? " + cache.exists("two")); System.out.println("Exists 'one'? " + cache.exists("one")); System.out.println("Remove 'three': " + cache.remove("three")); System.out.println("Exists 'three'? " + cache.exists("three")); System.out.println("Item at index 0: " + cache.getItemByIndex(0)); System.out.println("Item at index 5: " + cache.getItemByIndex(5)); } }