forked from pabulaner/vivi
wie übertrage ich das Array in iterator
This commit is contained in:
parent
5753b0887b
commit
f5abcb10d3
@ -1,14 +1,11 @@
|
||||
package de.vivi.list;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.*;
|
||||
|
||||
public class ArrayByteList implements ByteList {
|
||||
|
||||
private int size = 0;
|
||||
byte[] array;
|
||||
Iterator b = null;
|
||||
|
||||
@Override
|
||||
public void add(byte value) {
|
||||
@ -97,19 +94,11 @@ public class ArrayByteList implements ByteList {
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toArray() {
|
||||
return Arrays.copyOf(array, size);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Iterator iterator() {
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forEach(Consumer action) {
|
||||
ByteList.super.forEach(action);
|
||||
public ArrayByteListIterator<Byte> iterator() {
|
||||
ArrayByteListIterator<Byte> it = new ArrayByteListIterator<Byte>();
|
||||
while(it.hasNext()) {
|
||||
it.next();
|
||||
}
|
||||
return it;
|
||||
}
|
||||
}
|
||||
|
||||
40
list/src/main/java/de/vivi/list/ArrayByteListIterator.java
Normal file
40
list/src/main/java/de/vivi/list/ArrayByteListIterator.java
Normal file
@ -0,0 +1,40 @@
|
||||
package de.vivi.list;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class ArrayByteListIterator<Byte> implements Iterator<Byte> {
|
||||
|
||||
// ArrayByteList array;
|
||||
// private Byte nextByte;
|
||||
private int currentIndex = 0;
|
||||
|
||||
// public ArrayByteListIterator() {
|
||||
// array = new ArrayByteList();
|
||||
// }
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return array.get(currentIndex) != 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Byte next() {
|
||||
// if (hasNext()) {
|
||||
// //nextByte = array.get(currentIndex++);
|
||||
// } else {
|
||||
// nextByte = null;
|
||||
// }
|
||||
return array.get(currentIndex++);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
Iterator.super.remove();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forEachRemaining(Consumer action) {
|
||||
Iterator.super.forEachRemaining(action);
|
||||
}
|
||||
}
|
||||
@ -1,8 +1,28 @@
|
||||
package de.vivi.list;
|
||||
|
||||
public class BubbleSort implements ByteSort {
|
||||
@Override
|
||||
public void sort(ArrayByteList copy) {
|
||||
|
||||
boolean isSmaller = false;
|
||||
byte value;
|
||||
int indexFound;
|
||||
|
||||
@Override
|
||||
public ByteList sort(ByteList list) {
|
||||
for (int i = list.size() - 1; i >= 0; i--) {
|
||||
for (int j = 0; j < list.size(); j++) {
|
||||
if ((i - j) >= 0) {
|
||||
if (list.get(i) < list.get(i - j)) {
|
||||
indexFound = i;
|
||||
isSmaller = true;
|
||||
value = list.get(i);
|
||||
list.set(i, list.get(i - 1));
|
||||
list.set(i - 1, list.get(i - 2));
|
||||
list.set(i - 2, list.get(i - 3));
|
||||
list.set(i - 3, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,9 @@
|
||||
package de.vivi.list;
|
||||
|
||||
public interface ByteList extends Iterable {
|
||||
import java.util.Iterator;
|
||||
|
||||
public interface ByteList extends Iterable<Byte> {
|
||||
|
||||
void add(byte value);
|
||||
|
||||
void add(int index, byte value);
|
||||
@ -20,7 +23,4 @@ public interface ByteList extends Iterable {
|
||||
ArrayByteList copy();
|
||||
|
||||
void clear();
|
||||
|
||||
byte[] toArray();
|
||||
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package de.vivi.list;
|
||||
|
||||
public interface ByteSort {
|
||||
void sort(ArrayByteList copy);
|
||||
|
||||
ByteList sort(ByteList list);
|
||||
}
|
||||
|
||||
@ -1,111 +1,59 @@
|
||||
package de.vivi.list;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class CombinedByteList implements ByteList {
|
||||
|
||||
private int value;
|
||||
private int size = 0;
|
||||
private byte[] array;
|
||||
|
||||
public CombinedByteList(int value) {
|
||||
this.value = value;
|
||||
public CombinedByteList(int i) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(byte 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
|
||||
public void add(int index, byte value) {
|
||||
byte[] newArray = new byte[array.length + 1];
|
||||
System.arraycopy(array, 0, newArray, 0, array.length);
|
||||
for (int i = newArray.length - 1; i >= index; i--) {
|
||||
newArray[i] = newArray[i - 1];
|
||||
}
|
||||
newArray[index] = value;
|
||||
array = newArray;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(int index) {
|
||||
for (int i = index; i < array.length - 1; i++) {
|
||||
array[i] = array[i + 1];
|
||||
}
|
||||
byte[] newArray = new byte[array.length - 1];
|
||||
System.arraycopy(array, 0, newArray, 0, array.length - 1);
|
||||
array = newArray;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(int index, byte value) {
|
||||
byte[] newArray = new byte[array.length];
|
||||
System.arraycopy(array, 0, newArray, 0, array.length);
|
||||
newArray[index] = value;
|
||||
array = newArray;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte get(int index) {
|
||||
return array[index];
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return array.length;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(int value) {
|
||||
boolean isIndexInArray = false;
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
if (array[i] == value) {
|
||||
isIndexInArray = true;
|
||||
}
|
||||
}
|
||||
return isIndexInArray;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return Arrays.toString(array);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayByteList copy() {
|
||||
ArrayByteList newArrayList = new ArrayByteList();
|
||||
newArrayList.array = Arrays.copyOf(array, size);
|
||||
return newArrayList;
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
byte[] newArray = new byte[0];
|
||||
System.arraycopy(array, 0, newArray, 0, 0);
|
||||
array = newArray;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toArray() {
|
||||
return Arrays.copyOf(array, size);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator iterator() {
|
||||
public Iterator<Byte> iterator() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,106 +0,0 @@
|
||||
package de.vivi.list;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class LinkedByteList implements ByteList {
|
||||
|
||||
private int size = 0;
|
||||
private byte[] array;
|
||||
|
||||
@Override
|
||||
public void add(byte 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
|
||||
public void add(int index, byte value) {
|
||||
byte[] newArray = new byte[array.length + 1];
|
||||
System.arraycopy(array, 0, newArray, 0, array.length);
|
||||
for (int i = newArray.length - 1; i >= index; i--) {
|
||||
newArray[i] = newArray[i - 1];
|
||||
}
|
||||
newArray[index] = value;
|
||||
array = newArray;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(int index) {
|
||||
for (int i = index; i < array.length - 1; i++) {
|
||||
array[i] = array[i + 1];
|
||||
}
|
||||
byte[] newArray = new byte[array.length - 1];
|
||||
System.arraycopy(array, 0, newArray, 0, array.length - 1);
|
||||
array = newArray;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(int index, byte value) {
|
||||
byte[] newArray = new byte[array.length];
|
||||
System.arraycopy(array, 0, newArray, 0, array.length);
|
||||
newArray[index] = value;
|
||||
array = newArray;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte get(int index) {
|
||||
return array[index];
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return array.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(int value) {
|
||||
boolean isIndexInArray = false;
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
if (array[i] == value) {
|
||||
isIndexInArray = true;
|
||||
}
|
||||
}
|
||||
return isIndexInArray;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return Arrays.toString(array);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayByteList copy() {
|
||||
ArrayByteList newArrayList = new ArrayByteList();
|
||||
newArrayList.array = Arrays.copyOf(array, size);
|
||||
return newArrayList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
byte[] newArray = new byte[0];
|
||||
System.arraycopy(array, 0, newArray, 0, 0);
|
||||
array = newArray;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toArray() {
|
||||
return Arrays.copyOf(array, size);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator iterator() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -9,8 +9,8 @@ public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ByteList array = new ArrayByteList();
|
||||
ByteList linked = new LinkedByteList();
|
||||
ByteList combined = new CombinedByteList(16);
|
||||
// ByteList linked = new LinkedByteList();
|
||||
// ByteList combined = new CombinedByteList(16);
|
||||
testListVivi(array);
|
||||
testSortVivi(array);
|
||||
//testList(array);
|
||||
@ -27,52 +27,49 @@ public class Main {
|
||||
list.add((byte) 5);
|
||||
list.add((byte) -86);
|
||||
list.add((byte) 3);
|
||||
// list.add((byte) 5);
|
||||
|
||||
System.out.println(list);
|
||||
// System.out.println(list);
|
||||
|
||||
list.add(3, (byte) 99);
|
||||
System.out.println(list);
|
||||
// System.out.println(list);
|
||||
list.add(2, (byte) -40);
|
||||
System.out.println(list);
|
||||
// System.out.println(list);
|
||||
|
||||
list.remove(2);
|
||||
System.out.println(list);
|
||||
list.remove(3);
|
||||
System.out.println(list);
|
||||
// list.remove(2);
|
||||
//// System.out.println(list);
|
||||
// list.remove(3);
|
||||
// System.out.println(list);
|
||||
|
||||
list.set(1, (byte) 43);
|
||||
System.out.println(list);
|
||||
list.set(0, (byte) 8);
|
||||
System.out.println(list);
|
||||
// list.set(0, (byte) 8);
|
||||
//// System.out.println(list);
|
||||
//
|
||||
// list.get(2);
|
||||
//// System.out.println(list.get(2));
|
||||
// list.get(1);
|
||||
//// System.out.println(list.get(1));
|
||||
//
|
||||
// list.size();
|
||||
// System.out.println(list.size());
|
||||
// list.contains(43); // returns: true
|
||||
//// System.out.println(list.contains(43));
|
||||
// list.contains(4);
|
||||
// System.out.println(list.contains(4));
|
||||
|
||||
list.get(2);
|
||||
System.out.println(list.get(2));
|
||||
list.get(1);
|
||||
System.out.println(list.get(1));
|
||||
// String s = list.toString();
|
||||
// System.out.println(s);
|
||||
|
||||
list.size();
|
||||
System.out.println(list.size());
|
||||
list.contains(43); // returns: true
|
||||
System.out.println(list.contains(43));
|
||||
list.contains(4);
|
||||
System.out.println(list.contains(4));
|
||||
// list.copy();
|
||||
// System.out.println(list.copy());
|
||||
|
||||
String s = list.toString();
|
||||
System.out.println(s);
|
||||
// ByteList list2 = list.copy();
|
||||
// list2.set(0, (byte) 6);
|
||||
// System.out.println(list2);
|
||||
|
||||
list.copy();
|
||||
System.out.println(list.copy());
|
||||
|
||||
ByteList list2 = list.copy();
|
||||
list2.set(0, (byte) 6);
|
||||
System.out.println(list2);
|
||||
|
||||
for (byte value : list.toArray()) {
|
||||
System.out.println(value);
|
||||
}
|
||||
|
||||
list.clear();
|
||||
System.out.println(list);
|
||||
// list.clear();
|
||||
// System.out.println(list);
|
||||
|
||||
for (byte value : list) {
|
||||
System.out.println(value);
|
||||
@ -156,6 +153,7 @@ public class Main {
|
||||
|
||||
// sort the lists with the sorting algorithm
|
||||
bubble.sort(list.copy());
|
||||
System.out.println(bubble.sort(list.copy()));
|
||||
// merge.sort(list.copy());
|
||||
// bogo.sort(list.copy());
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user