forked from pabulaner/vivi
for each
This commit is contained in:
parent
ba5282f500
commit
b38e400aea
96
list/src/main/java/de/vivi/list/ArrayByteList.java
Normal file
96
list/src/main/java/de/vivi/list/ArrayByteList.java
Normal file
@ -0,0 +1,96 @@
|
||||
package de.vivi.list;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class ArrayByteList 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(byte 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 String copy() {
|
||||
byte[] newArray = new byte[array.length];
|
||||
System.arraycopy(array, 0, newArray, 0, array.length);
|
||||
array = newArray;
|
||||
return Arrays.toString(newArray);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
byte[] newArray = new byte[0];
|
||||
System.arraycopy(array, 0, newArray, 0, 0);
|
||||
array = newArray;
|
||||
}
|
||||
}
|
||||
23
list/src/main/java/de/vivi/list/ByteList.java
Normal file
23
list/src/main/java/de/vivi/list/ByteList.java
Normal file
@ -0,0 +1,23 @@
|
||||
package de.vivi.list;
|
||||
|
||||
public interface ByteList {
|
||||
void add(byte value);
|
||||
|
||||
void add(int index, byte value);
|
||||
|
||||
void remove(int index);
|
||||
|
||||
void set(int index, byte value);
|
||||
|
||||
byte get(int index);
|
||||
|
||||
int size();
|
||||
|
||||
boolean contains(byte value);
|
||||
|
||||
String toString();
|
||||
|
||||
String copy();
|
||||
|
||||
void clear();
|
||||
}
|
||||
@ -1,26 +1,94 @@
|
||||
package de.vivi.list;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ByteList array = new ArrayByteList();
|
||||
ByteList linked = new LinkedByteList();
|
||||
ByteList combined = new CombinedByteList(16);
|
||||
|
||||
testList(array);
|
||||
testList(linked);
|
||||
testList(combined);
|
||||
/*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));
|
||||
|
||||
testSort(array);
|
||||
testSort(linked);
|
||||
testSort(combined);
|
||||
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);
|
||||
testListVivi(array);
|
||||
// do some stuff with value...
|
||||
|
||||
//testList(array);
|
||||
// testList(linked);
|
||||
// testList(combined);
|
||||
|
||||
// testSort(array);
|
||||
// testSort(linked);
|
||||
// testSort(combined);
|
||||
}
|
||||
|
||||
private static void testListVivi() {
|
||||
private static void testListVivi(ByteList list) {
|
||||
list.add((byte) 10);
|
||||
list.add((byte) 5);
|
||||
list.add((byte) -86);
|
||||
list.add((byte) 3);
|
||||
|
||||
System.out.println(list);
|
||||
|
||||
list.add(3, (byte) 99);
|
||||
System.out.println(list);
|
||||
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);
|
||||
System.out.println(list);
|
||||
|
||||
list.set(1, (byte) 43);
|
||||
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((byte) 43); // returns: true
|
||||
System.out.println(list.contains((byte) 43));
|
||||
list.contains((byte) 4);
|
||||
System.out.println(list.contains((byte) 4));
|
||||
|
||||
String s = list.toString();
|
||||
System.out.println(s);
|
||||
|
||||
list.copy();
|
||||
System.out.println(list.copy());
|
||||
|
||||
list.clear();
|
||||
System.out.println(list);
|
||||
|
||||
|
||||
}
|
||||
|
||||
private static void testList(ByteList list) {
|
||||
/*private static void testList(ByteList list) {
|
||||
// add the element at the end
|
||||
list.add((byte) 10);
|
||||
list.add((byte) 5);
|
||||
@ -86,4 +154,6 @@ public class Main {
|
||||
merge.sort(list.copy());
|
||||
bogo.sort(list.copy());
|
||||
}
|
||||
|
||||
*/
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user