forked from pabulaner/vivi
add, set, get, size, clear, toString
This commit is contained in:
parent
5a7a6e8a56
commit
acc84590fd
@ -5,24 +5,26 @@ import java.util.*;
|
||||
public class ArrayByteList implements ByteList {
|
||||
|
||||
private int size = 0;
|
||||
byte[] array;
|
||||
private byte[] array;
|
||||
|
||||
public ArrayByteList() {
|
||||
array = new byte[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(byte value) {
|
||||
add(size(), value);
|
||||
++size;
|
||||
if (size == 1) {
|
||||
byte[] oldArray = new byte[size];
|
||||
oldArray[size - 1] = value;
|
||||
array = oldArray;
|
||||
//System.out.println(Arrays.toString(arrayOld));
|
||||
} else {
|
||||
byte[] newArray = new byte[size];
|
||||
System.arraycopy(array, 0, newArray, 0, size - 1);
|
||||
newArray[size - 1] = value;
|
||||
array = newArray;
|
||||
//System.out.println(Arrays.toString(arrayNew));
|
||||
}
|
||||
//System.out.println(Arrays.toString(array));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -6,7 +6,6 @@ import java.util.function.Consumer;
|
||||
public class ArrayByteListIterator implements Iterator<Byte> {
|
||||
|
||||
private ArrayByteList array;
|
||||
// private Byte nextByte;
|
||||
private int currentIndex = 0;
|
||||
|
||||
public ArrayByteListIterator(ArrayByteList array) {
|
||||
@ -20,11 +19,6 @@ public class ArrayByteListIterator implements Iterator<Byte> {
|
||||
|
||||
@Override
|
||||
public Byte next() {
|
||||
// if (hasNext()) {
|
||||
// //nextByte = array.get(currentIndex++);
|
||||
// } else {
|
||||
// nextByte = null;
|
||||
// }
|
||||
return array.get(currentIndex++);
|
||||
}
|
||||
|
||||
|
||||
@ -1,7 +1,5 @@
|
||||
package de.vivi.list;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
public interface ByteList extends Iterable<Byte> {
|
||||
|
||||
void add(byte value);
|
||||
|
||||
@ -1,52 +1,67 @@
|
||||
package de.vivi.list;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.*;
|
||||
|
||||
public class LinkedByteList implements ByteList {
|
||||
|
||||
public NodeLinked head;
|
||||
public NodeLinked tail;
|
||||
private NodeLinked head;
|
||||
private NodeLinked currNode;
|
||||
private int size;
|
||||
|
||||
public LinkedByteList() {
|
||||
this.head = null;
|
||||
this.tail = null;
|
||||
head = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(byte value) {
|
||||
NodeLinked new_Node = new NodeLinked(value);
|
||||
NodeLinked newNode = new NodeLinked(value); // erzeuge neues Node
|
||||
if (head == null) {
|
||||
head = new_Node;
|
||||
tail = new_Node;
|
||||
head = newNode;
|
||||
} else {
|
||||
tail.next = new_Node;
|
||||
tail = new_Node;
|
||||
NodeLinked last = head;
|
||||
while (last.next != null) {
|
||||
last = last.next;
|
||||
}
|
||||
last.next = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(int index, byte value) {
|
||||
if (index == size) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void remove(int index) {
|
||||
|
||||
size--;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(int index, byte value) {
|
||||
NodeLinked x = node(index);
|
||||
x.value = value;
|
||||
}
|
||||
|
||||
NodeLinked node(int index) {
|
||||
NodeLinked x = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
x = x.next;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte get(int index) {
|
||||
return 0;
|
||||
return node(index).value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return 0;
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -61,7 +76,13 @@ public class LinkedByteList implements ByteList {
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
|
||||
for (NodeLinked x = head; x != null;) {
|
||||
NodeLinked next = x.next;
|
||||
x.next = null;
|
||||
x = next;
|
||||
}
|
||||
head = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -69,5 +90,23 @@ public class LinkedByteList implements ByteList {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public Byte[] toArray() {
|
||||
Byte[] result = new Byte[size];
|
||||
int i = 0;
|
||||
for (NodeLinked x = head; x != null; x = x.next) {
|
||||
result[i++] = x.value;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
System.out.print("LinkedList: ");
|
||||
currNode = head;
|
||||
while (currNode != null) {
|
||||
System.out.print(currNode.value + " ");
|
||||
currNode = currNode.next;
|
||||
}
|
||||
return String.valueOf(currNode).replace("null", "");
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,6 @@
|
||||
package de.vivi.list;
|
||||
|
||||
public class LinkedByteListIterator {
|
||||
public LinkedByteListIterator(LinkedByteList bytes) {
|
||||
}
|
||||
}
|
||||
@ -28,11 +28,12 @@ public class Main {
|
||||
list.add((byte) 5);
|
||||
list.add((byte) -86);
|
||||
list.add((byte) 3);
|
||||
// list.add((byte) 5);
|
||||
// list.add((byte) -100);
|
||||
list.add((byte) 5);
|
||||
list.add((byte) -100);
|
||||
|
||||
// System.out.println(list);
|
||||
System.out.println(list);
|
||||
|
||||
// list.add(0, (byte) 99);
|
||||
// list.add(3, (byte) 99);
|
||||
//// System.out.println(list);
|
||||
// list.add(2, (byte) -40);
|
||||
@ -44,7 +45,7 @@ public class Main {
|
||||
// System.out.println(list);
|
||||
|
||||
// list.set(1, (byte) 43);
|
||||
System.out.println(list);
|
||||
// System.out.println(list);
|
||||
// list.set(0, (byte) 8);
|
||||
//// System.out.println(list);
|
||||
//
|
||||
|
||||
@ -2,13 +2,11 @@ package de.vivi.list;
|
||||
|
||||
public class NodeLinked {
|
||||
|
||||
byte data;
|
||||
byte value;
|
||||
NodeLinked next;
|
||||
NodeLinked prev;
|
||||
|
||||
public NodeLinked(byte data) {
|
||||
this.data = data;
|
||||
public NodeLinked(byte value) {
|
||||
this.value = value;
|
||||
this.next = null;
|
||||
this.prev = null;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user