1
0
forked from pabulaner/vivi

LinkedList finished

This commit is contained in:
happymeal2024 2025-04-18 14:02:12 +02:00
parent 657bcf5463
commit e28f39412b
6 changed files with 40 additions and 40 deletions

View File

@ -30,10 +30,8 @@ public class ArrayByteList implements ByteList {
@Override @Override
public void add(int index, byte value) { public void add(int index, byte value) {
byte[] newArray = new byte[array.length + 1]; byte[] newArray = new byte[array.length + 1];
System.arraycopy(array, 0, newArray, 0, array.length); System.arraycopy(array, 0, newArray, 0, index);
for (int i = newArray.length - 1; i >= index; i--) { System.arraycopy(array, index, newArray, index + 1, size - index);
newArray[i] = newArray[i - 1];
}
newArray[index] = value; newArray[index] = value;
array = newArray; array = newArray;
} }
@ -82,7 +80,7 @@ public class ArrayByteList implements ByteList {
} }
@Override @Override
public ArrayByteList copy() { public ByteList copy() {
ArrayByteList newArrayList = new ArrayByteList(); ArrayByteList newArrayList = new ArrayByteList();
newArrayList.array = Arrays.copyOf(array, array.length); newArrayList.array = Arrays.copyOf(array, array.length);
return newArrayList; return newArrayList;

View File

@ -21,14 +21,4 @@ public class ArrayByteListIterator implements Iterator<Byte> {
public Byte next() { public Byte next() {
return array.get(currentIndex++); return array.get(currentIndex++);
} }
@Override
public void remove() {
Iterator.super.remove();
}
@Override
public void forEachRemaining(Consumer action) {
Iterator.super.forEachRemaining(action);
}
} }

View File

@ -18,7 +18,7 @@ public interface ByteList extends Iterable<Byte> {
String toString(); String toString();
ArrayByteList copy(); ByteList copy();
void clear(); void clear();
} }

View File

@ -83,19 +83,6 @@ public class LinkedByteList implements ByteList {
} }
return current; return current;
} }
// NodeLinked node(int index) {
// if (index < (size >> 1)) {
// NodeLinked x = first;
// for (int i = 0; i < index; i++)
// x = x.next;
// return x;
// } else {
// NodeLinked x = last;
// for (int i = size - 1; i > index; i--)
// x = x.prev;
// return x;
// }
// }
@Override @Override
public byte get(int index) { public byte get(int index) {
@ -123,8 +110,15 @@ public class LinkedByteList implements ByteList {
} }
@Override @Override
public ArrayByteList copy() { public ByteList copy() {
return null; LinkedByteList clone = new LinkedByteList();
clone.first = clone.last = null;
clone.size = 0;
for (NodeLinked x = first; x != null; x = x.next) {
clone.add(x.item);
}
return clone;
} }
@Override @Override
@ -139,8 +133,9 @@ public class LinkedByteList implements ByteList {
} }
@Override @Override
public Iterator<Byte> iterator() { public LinkedByteListIterator iterator() {
return null; LinkedByteListIterator it = new LinkedByteListIterator(this);
return it;
} }
@Override @Override

View File

@ -1,6 +1,23 @@
package de.vivi.list; package de.vivi.list;
public class LinkedByteListIterator { import java.util.Iterator;
public LinkedByteListIterator(LinkedByteList bytes) { import java.util.function.Consumer;
public class LinkedByteListIterator implements Iterator<Byte> {
private LinkedByteList linkedList;
private int currentIndex;
public LinkedByteListIterator(LinkedByteList linkedList) {
this.linkedList = linkedList;
}
@Override
public boolean hasNext() {
return currentIndex < linkedList.size();
}
@Override
public Byte next() {
return linkedList.get(currentIndex++);
} }
} }

View File

@ -33,10 +33,10 @@ public class Main {
System.out.println(list); System.out.println(list);
System.out.println(list.copy());
// list.remove(4); // list.remove(4);
// list.contains(5); // list.contains(5);
System.out.println(list.contains(2)); // System.out.println(list.contains(2));
// list.add(0, (byte) 99); // list.add(0, (byte) 99);
// list.add(3, (byte) 99); // list.add(3, (byte) 99);
@ -79,9 +79,9 @@ public class Main {
// list.clear(); // list.clear();
// System.out.println(list); // System.out.println(list);
// for (byte value : list) { for (byte value : list) {
// System.out.print("Hello it " + value + " "); System.out.print(value + " ");
// } }
} }