Не могу решить проблему, тест 8 и 15 выполняются не правильно. Можете дать подсказку что не так.
public interface Container extends Iterable<Object> {
// Removes all of the elements.
void clear();
// Returns the number of elements.
int size();
// Returns a string representation of this container.
String toString();
// Returns an iterator over elements.
// Iterator must implements the remove method.
Iterator<Object> iterator();}
public interface List extends Container {
// Inserts the specified element at the beginning.
void addFirst(Object element);
// Appends the specified element to the end.
void addLast(Object element);
// Removes the first element.
void removeFirst();
// Removes the last element.
void removeLast();
// Returns the first element.
Object getFirst();
// Returns the last element.
Object getLast();
// Returns the first occurrence of the specified element.
// Returns null if no such element.
// (use 'equals' method to check an occurrence)
Object search(Object element);
// Removes the first occurrence of the specified element.
// Returns true if this list contained the specified element.
// (use 'equals' method to check an occurrence)
boolean remove(Object element);}
public class ListImpl implements List {
int size = 0;
private Node first;
private Node last;
private static class Node {
public Object data;
public Node next;
public Node previous;
@Override
public String toString() {
return data.toString();
}
public Node(Object data) {
this.data = data;
}
}
@Override
public void addFirst(Object element) {
Node currentNode = new Node(element);
if (first == null) {
first = currentNode;
last = currentNode;
} else {
currentNode.next = first;
first.previous = currentNode;
first = currentNode;
}
size++;
}
@Override
public void addLast(Object element) {
Node currentNode = new Node(element);
if (last == null) {
first = currentNode;
last = currentNode;
} else {
currentNode.previous = last;
last.next = currentNode;
last = currentNode;
}
size++;
}
@Override
public void removeFirst() {
if (first == null) {
throw new NoSuchElementException();
} else {
Node nextNode = first.next;
nextNode.previous = null;
first = first.next;
size--;
}
}
@Override
public void removeLast() {
if (last == null) {
throw new NoSuchElementException();
} else {
Node previousNode = last.previous;
previousNode.next = null;
last = last.previous;
size--;
}
}
@Override
public Object getFirst() {
if (first == null) {
throw new NoSuchElementException();
} else
return first.data;
}
@Override
public Object getLast() {
if (last == null) {
throw new NoSuchElementException();
} else
return last.data;
}
@Override
public Object search(Object element) {
Node currentNode = first;
for (int i = 0; i < size; i++) {
if (currentNode.data.equals(element)) {
return currentNode.data;
}
currentNode = currentNode.next;
}
return null;
}
@Override
public boolean remove(Object element) {
Node currentNode = first;
for (int i = 0; i < size; i++) {
if (currentNode.data.equals(element)) {
Node next = currentNode.next;
Node previous = currentNode.previous;
if (next != null) {
next.previous = previous;
}
if (previous != null) {
previous.next = next;
}
size--;
return true;
}
// currentNode = currentNode.next;
}
size--;
return false;
}
@Override
public void clear() {
size = 0;
first = null;
last = null;
}
@Override
public int size() {
return size;
}
@Override
public Iterator<Object> iterator() {
return new IteratorImpl();
}
@Override
public String toString() {
if (size == 0) {
return "[]";
} else {
IteratorImpl iterator = new IteratorImpl();
StringBuilder result = new StringBuilder("[");
while (iterator.hasNext()) {
Node currentNode = (Node) iterator.next();
result.append(currentNode.data.toString());
if (iterator.hasNext()) {
result.append(", ");
} else {
result.append("]");
}
}
return result.toString();
}
}
public class IteratorImpl implements Iterator {
private Node lastReturned;
private Node nextNode;
private int index = 0;
@Override
public boolean hasNext() {
return index < size;
}
@Override
public Object next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
index++;
if (lastReturned == null) {
lastReturned = first;
return lastReturned;
}
lastReturned = lastReturned.next;
return lastReturned;
}
@Override
public void remove() {
if (index == 0) {
throw new IllegalStateException();
}
ListImpl.this.remove(lastReturned.data);
index--;
}
}
public static void main(String[] args) {
test1();
test2();
test3();
test4();
test5();
test6();
test7();
test8();
test9();
test10();
test11();
test12();
test13();
test14();
test15();
}
private static void test1() {
System.out.println("\n" + "--------------------------------------");
List list = new ListImpl();
Class c = list.getClass();
// must be 1
System.out.println(c.getInterfaces().length);
// must be List
System.out.println(c.getInterfaces()[0].getSimpleName());
// must be java.lang.Object
System.out.println(c.getSuperclass().getName());
// must be 1
System.out.println(c.getInterfaces()[0].getInterfaces().length);
// must be Container
System.out.println(c.getInterfaces()[0].getInterfaces()[0].getSimpleName());
// must be 1
System.out.println(c.getInterfaces()[0].getInterfaces()[0].getInterfaces().length);
// must be java.lang.Iterable
System.out.println(c.getInterfaces()[0].getInterfaces()[0].getInterfaces()[0].getName());
/* an output must be as the following:
*************************************
1
List
java.lang.Object
1
Container
1
java.lang.Iterable
*************************************
*/
}
private static void test2() {
System.out.println("\n" + "--------------------------------------");
List list = new ListImpl();
list.addLast("A");
list.addLast("B");
list.addLast("C");
System.out.println(list);
System.out.println(list.size());
list.clear();
System.out.println(list);
System.out.println(list.size());
list.addLast("A");
list.addLast("B");
list.addLast("C");
System.out.println(list);
System.out.println(list.size());
/* an output must be as the following:
*************************************
[A, B, C]
3
[]
0
[A, B, C]
3
*************************************
*/
}
private static void test3() {
System.out.println("\n" + "--------------------------------------");
List list = new ListImpl();
list.addFirst("A");
list.addLast("B");
list.addFirst("C");
System.out.println(list);
System.out.print(list.getFirst());
System.out.print(list.getLast());
/* an output must be as the following:
*************************************
[C, A, B]
CB
*************************************
*/
}
private static void test4() {
System.out.println("\n" + "--------------------------------------");
List list = new ListImpl();
list.addFirst("c");
list.addFirst("b");
list.addFirst("a");
list.addLast("A");
list.addLast("B");
list.addLast("C");
System.out.println(list);
/* an output must be as the following:
*************************************
[a, b, c, A, B, C]
*************************************
*/
}
private static void test5() {
System.out.println("\n" + "--------------------------------------");
List list = new ListImpl();
list.addLast("A");
list.addLast("B");
list.addLast("C");
for (Object element : list) {
System.out.print(element);
}
/* an output must be as the following:
*************************************
ABC
*************************************
*/
}
private static void test6() {
System.out.println("\n" + "--------------------------------------");
List list = new ListImpl();
list.addLast("A");
list.addLast("B");
list.addLast("C");
Iterator it = list.iterator();
while (it.hasNext()) {
System.out.print(it.next());
}
System.out.println();
it = list.iterator();
while (it.hasNext()) {
System.out.print(it.next());
}
/* an output must be as the following:
*************************************
ABC
ABC
*************************************
*/
}
private static void test7() {
System.out.println("\n" + "--------------------------------------");
List list = new ListImpl();
list.addLast("A");
list.addLast("B");
list.addLast("C");
Iterator it = list.iterator();
System.out.println(it.next());
it.remove();
System.out.println(it.next());
it.remove();
System.out.println(it.next());
it.remove();
System.out.println(list);
/* an output must be as the following:
*************************************
A
B
C
[]
*
*************************************
*/
}
private static void test8() {
System.out.println("\n" + "--------------------------------------");
List list = new ListImpl();
list.addLast("A");
list.addFirst("B");
list.addLast("C");
Iterator it = list.iterator();
System.out.println(it.next());
System.out.println(it.next());
System.out.println(it.next());
it.remove();
System.out.println(list);
it = list.iterator();
System.out.println(it.next());
it.remove();
System.out.println(list);
it = list.iterator();
System.out.println(it.next());
it.remove();
System.out.println(list);
/* an output must be as the following:
*************************************
B
A
C
[B, A]
B
[A]
A
[]
*************************************
*/
}
private static void test9() {
System.out.println("\n" + "--------------------------------------");
List list = new ListImpl();
list.addLast("A");
list.addLast("B");
list.addLast("C");
Iterator it = list.iterator();
System.out.println(it.next());
it.remove();
try {
it.remove();
} catch (IllegalStateException ex) {
System.out.println("exception");
}
/* an output must be as the following:
*************************************
A
exception
*************************************
*/
}
private static void test10() {
System.out.println("\n" + "--------------------------------------");
List list = new ListImpl();
list.addLast("A");
list.addLast("B");
list.addLast("C");
Iterator it = list.iterator();
try {
it.remove();
} catch (IllegalStateException ex) {
System.out.println("exception");
}
/* an output must be as the following:
*************************************
exception
*************************************
*/
}
private static void test11() {
System.out.println("\n" + "--------------------------------------");
List list = new ListImpl();
list.addFirst("B");
list.addLast("C");
list.addFirst("A");
System.out.println(list);
list.removeFirst();
System.out.println(list);
list.removeFirst();
System.out.println(list);
/* an output must be as the following:
*************************************
[A, B, C]
[B, C]
[C]
*************************************
*/
}
private static void test12() {
System.out.println("\n" + "--------------------------------------");
List list = new ListImpl();
list.addFirst("B");
list.addLast("C");
list.addFirst("A");
System.out.println(list);
list.removeLast();
System.out.println(list);
list.removeLast();
System.out.println(list);
/* an output must be as the following:
*************************************
[A, B, C]
[A, B]
[A]
*************************************
*/
}
private static void test13() {
System.out.println("\n" + "--------------------------------------");
List list = new ListImpl();
list.addFirst("B");
list.addLast("C");
list.addFirst("A");
System.out.println(list);
System.out.println(list.getFirst());
System.out.println(list.getLast());
/* an output must be as the following:
*************************************
[A, B, C]
A
C
*************************************
*/
}
private static void test14() {
System.out.println("\n" + "--------------------------------------");
List list = new ListImpl();
list.addFirst(3);
list.addFirst(2);
list.addFirst(1);
System.out.println(list);
System.out.println(list.search(1));
System.out.println(list.search(2));
System.out.println(list.getLast());
/* an output must be as the following:
*************************************
[1, 2, 3]
1
2
3
*************************************
*/
}
private static void test15() {
System.out.println("\n" + "--------------------------------------");
List list = new ListImpl();
list.addLast(1);
list.addLast(2);
list.addLast(1);
list.remove(1);
System.out.println(list);
list.remove(2);
System.out.println(list);
Iterator it = list.iterator();
System.out.println(it.next());
/* an output must be as the following:
*************************************
[2, 1]
[1]
1
*************************************
*/
}}
private Node searchNodeByData(Object data) {
Node current = first;
while (current != null) {
if (current.data.equals(data)) {
return current;
}
current = curent.next;
}
return null;
}
@Override
public boolean remove(Object element) {
Node current = searchNodeByData(element);
if (current == null) {
return false;
}
Node next = current.next;
Node previous = current.previous;
if (next != null) {
next.previous = previous;
}
if (previous != null) {
previous.next = next;
}
if (current == first)
first = next;
if (current == last)
last = previous;
size--;
return true;
}
@Override
public void remove() {
if (index == 0) {
throw new IllegalStateException();
}
Node prev = lastReturned.previous;
ListImpl.this.remove(lastReturned.data);
lastReturned = prev;
index--;
}
Айфон мало держит заряд, разбираемся с проблемой вместе с AppLab
Я решил сделать небольшое веб-приложение на Spring BootПланировщик задач для пользователей
Учусь использовать thymeleaf, пока идет тугоЕсть admin, только он может добавить новую команду, т е надо создать форму, где надо ввести имя команды...
Есть класс UserDTO и класс CarDTO
Делаю скрипт, который будет загружать картинку в библиотеку медиафайловДвижок WordPress, но скрипт(самопис), который выполняет загрузку, к движку...