forked from pabulaner/vivi
iterable issue
This commit is contained in:
parent
b38e400aea
commit
5753b0887b
@ -1,11 +1,14 @@
|
||||
package de.vivi.list;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class ArrayByteList implements ByteList {
|
||||
|
||||
private int size = 0;
|
||||
private byte[] array;
|
||||
byte[] array;
|
||||
Iterator b = null;
|
||||
|
||||
@Override
|
||||
public void add(byte value) {
|
||||
@ -65,7 +68,7 @@ public class ArrayByteList implements ByteList {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(byte value) {
|
||||
public boolean contains(int value) {
|
||||
boolean isIndexInArray = false;
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
if (array[i] == value) {
|
||||
@ -76,15 +79,14 @@ public class ArrayByteList implements ByteList {
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "\"" + Arrays.toString(array) + "\"";
|
||||
return Arrays.toString(array);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String copy() {
|
||||
byte[] newArray = new byte[array.length];
|
||||
System.arraycopy(array, 0, newArray, 0, array.length);
|
||||
array = newArray;
|
||||
return Arrays.toString(newArray);
|
||||
public ArrayByteList copy() {
|
||||
ArrayByteList newArrayList = new ArrayByteList();
|
||||
newArrayList.array = Arrays.copyOf(array, size);
|
||||
return newArrayList;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -93,4 +95,21 @@ public class ArrayByteList implements ByteList {
|
||||
System.arraycopy(array, 0, newArray, 0, 0);
|
||||
array = newArray;
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
|
||||
8
list/src/main/java/de/vivi/list/BubbleSort.java
Normal file
8
list/src/main/java/de/vivi/list/BubbleSort.java
Normal file
@ -0,0 +1,8 @@
|
||||
package de.vivi.list;
|
||||
|
||||
public class BubbleSort implements ByteSort {
|
||||
@Override
|
||||
public void sort(ArrayByteList copy) {
|
||||
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,6 @@
|
||||
package de.vivi.list;
|
||||
|
||||
public interface ByteList {
|
||||
public interface ByteList extends Iterable {
|
||||
void add(byte value);
|
||||
|
||||
void add(int index, byte value);
|
||||
@ -13,11 +13,14 @@ public interface ByteList {
|
||||
|
||||
int size();
|
||||
|
||||
boolean contains(byte value);
|
||||
boolean contains(int value);
|
||||
|
||||
String toString();
|
||||
|
||||
String copy();
|
||||
ArrayByteList copy();
|
||||
|
||||
void clear();
|
||||
|
||||
byte[] toArray();
|
||||
|
||||
}
|
||||
|
||||
5
list/src/main/java/de/vivi/list/ByteSort.java
Normal file
5
list/src/main/java/de/vivi/list/ByteSort.java
Normal file
@ -0,0 +1,5 @@
|
||||
package de.vivi.list;
|
||||
|
||||
public interface ByteSort {
|
||||
void sort(ArrayByteList copy);
|
||||
}
|
||||
111
list/src/main/java/de/vivi/list/CombinedByteList.java
Normal file
111
list/src/main/java/de/vivi/list/CombinedByteList.java
Normal file
@ -0,0 +1,111 @@
|
||||
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;
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
106
list/src/main/java/de/vivi/list/LinkedByteList.java
Normal file
106
list/src/main/java/de/vivi/list/LinkedByteList.java
Normal file
@ -0,0 +1,106 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@ -1,33 +1,18 @@
|
||||
package de.vivi.list;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ByteList array = new ArrayByteList();
|
||||
|
||||
/*byte[] byteArray = {0};
|
||||
int newSize = byteArray.length;
|
||||
byte[] newByte = new byte[newSize];
|
||||
System.arraycopy(byteArray, 0, newByte, 0 , byteArray.length);
|
||||
newByte[newSize - 1] = 4;
|
||||
System.out.println(Arrays.toString(byteArray));
|
||||
System.out.println(Arrays.toString(newByte));
|
||||
|
||||
int newSize2 = newByte.length + 1;
|
||||
byte[] newByte2 = new byte[newSize2];
|
||||
System.arraycopy(newByte, 0, newByte2, 0 , newByte.length);
|
||||
newByte2[newSize2 - 1] = 5;
|
||||
System.out.println(Arrays.toString(newByte));
|
||||
System.out.println(Arrays.toString(newByte2));
|
||||
|
||||
*/
|
||||
// ByteList linked = new LinkedByteList();
|
||||
// ByteList combined = new CombinedByteList(16);
|
||||
ByteList linked = new LinkedByteList();
|
||||
ByteList combined = new CombinedByteList(16);
|
||||
testListVivi(array);
|
||||
// do some stuff with value...
|
||||
|
||||
testSortVivi(array);
|
||||
//testList(array);
|
||||
// testList(linked);
|
||||
// testList(combined);
|
||||
@ -50,10 +35,6 @@ public class Main {
|
||||
list.add(2, (byte) -40);
|
||||
System.out.println(list);
|
||||
|
||||
for (byte value : list) {
|
||||
System.out.println(value + " ");
|
||||
}
|
||||
|
||||
list.remove(2);
|
||||
System.out.println(list);
|
||||
list.remove(3);
|
||||
@ -71,10 +52,10 @@ public class Main {
|
||||
|
||||
list.size();
|
||||
System.out.println(list.size());
|
||||
list.contains((byte) 43); // returns: true
|
||||
System.out.println(list.contains((byte) 43));
|
||||
list.contains((byte) 4);
|
||||
System.out.println(list.contains((byte) 4));
|
||||
list.contains(43); // returns: true
|
||||
System.out.println(list.contains(43));
|
||||
list.contains(4);
|
||||
System.out.println(list.contains(4));
|
||||
|
||||
String s = list.toString();
|
||||
System.out.println(s);
|
||||
@ -82,9 +63,20 @@ public class Main {
|
||||
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);
|
||||
|
||||
for (byte value : list) {
|
||||
System.out.println(value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -156,4 +148,15 @@ public class Main {
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
private static void testSortVivi(ByteList list) {
|
||||
ByteSort bubble = new BubbleSort();
|
||||
// ByteSort merge = new MergeSort();
|
||||
// ByteSort bogo = new BogoSort();
|
||||
|
||||
// sort the lists with the sorting algorithm
|
||||
bubble.sort(list.copy());
|
||||
// merge.sort(list.copy());
|
||||
// bogo.sort(list.copy());
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user