1
0
forked from pabulaner/vivi

wie übertrage ich das Array in iterator

This commit is contained in:
happymeal2024 2025-04-01 11:59:47 +02:00
parent 5753b0887b
commit f5abcb10d3
8 changed files with 121 additions and 231 deletions

View File

@ -1,14 +1,11 @@
package de.vivi.list; package de.vivi.list;
import java.util.Arrays; import java.util.*;
import java.util.Iterator;
import java.util.function.Consumer;
public class ArrayByteList implements ByteList { public class ArrayByteList implements ByteList {
private int size = 0; private int size = 0;
byte[] array; byte[] array;
Iterator b = null;
@Override @Override
public void add(byte value) { public void add(byte value) {
@ -97,19 +94,11 @@ public class ArrayByteList implements ByteList {
} }
@Override @Override
public byte[] toArray() { public ArrayByteListIterator<Byte> iterator() {
return Arrays.copyOf(array, size); ArrayByteListIterator<Byte> it = new ArrayByteListIterator<Byte>();
} while(it.hasNext()) {
it.next();
}
@Override return it;
public Iterator iterator() {
return b;
}
@Override
public void forEach(Consumer action) {
ByteList.super.forEach(action);
} }
} }

View 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);
}
}

View File

@ -1,8 +1,28 @@
package de.vivi.list; package de.vivi.list;
public class BubbleSort implements ByteSort { 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;
} }
} }

View File

@ -1,6 +1,9 @@
package de.vivi.list; 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(byte value);
void add(int index, byte value); void add(int index, byte value);
@ -20,7 +23,4 @@ public interface ByteList extends Iterable {
ArrayByteList copy(); ArrayByteList copy();
void clear(); void clear();
byte[] toArray();
} }

View File

@ -1,5 +1,6 @@
package de.vivi.list; package de.vivi.list;
public interface ByteSort { public interface ByteSort {
void sort(ArrayByteList copy);
ByteList sort(ByteList list);
} }

View File

@ -1,111 +1,59 @@
package de.vivi.list; package de.vivi.list;
import java.util.Arrays;
import java.util.Iterator; import java.util.Iterator;
public class CombinedByteList implements ByteList { public class CombinedByteList implements ByteList {
private int value; public CombinedByteList(int i) {
private int size = 0;
private byte[] array;
public CombinedByteList(int value) {
this.value = value;
} }
@Override @Override
public void add(byte value) { 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 @Override
public void add(int index, byte value) { 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 @Override
public void remove(int index) { 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 @Override
public void set(int index, byte value) { 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 @Override
public byte get(int index) { public byte get(int index) {
return array[index]; return 0;
} }
@Override @Override
public int size() { public int size() {
return array.length; return 0;
} }
@Override @Override
public boolean contains(int value) { public boolean contains(int value) {
boolean isIndexInArray = false; return 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 @Override
public ArrayByteList copy() { public ArrayByteList copy() {
ArrayByteList newArrayList = new ArrayByteList(); return null;
newArrayList.array = Arrays.copyOf(array, size);
return newArrayList;
} }
@Override @Override
public void clear() { public void clear() {
byte[] newArray = new byte[0];
System.arraycopy(array, 0, newArray, 0, 0);
array = newArray;
} }
@Override @Override
public byte[] toArray() { public Iterator<Byte> iterator() {
return Arrays.copyOf(array, size);
}
@Override
public Iterator iterator() {
return null; return null;
} }
} }

View File

@ -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;
}
}

View File

@ -9,8 +9,8 @@ public class Main {
public static void main(String[] args) { public static void main(String[] args) {
ByteList array = new ArrayByteList(); ByteList array = new ArrayByteList();
ByteList linked = new LinkedByteList(); // ByteList linked = new LinkedByteList();
ByteList combined = new CombinedByteList(16); // ByteList combined = new CombinedByteList(16);
testListVivi(array); testListVivi(array);
testSortVivi(array); testSortVivi(array);
//testList(array); //testList(array);
@ -27,52 +27,49 @@ public class Main {
list.add((byte) 5); list.add((byte) 5);
list.add((byte) -86); list.add((byte) -86);
list.add((byte) 3); list.add((byte) 3);
// list.add((byte) 5);
System.out.println(list); // System.out.println(list);
list.add(3, (byte) 99); list.add(3, (byte) 99);
System.out.println(list); // System.out.println(list);
list.add(2, (byte) -40); list.add(2, (byte) -40);
System.out.println(list); // System.out.println(list);
list.remove(2); // list.remove(2);
System.out.println(list); //// System.out.println(list);
list.remove(3); // list.remove(3);
System.out.println(list); // System.out.println(list);
list.set(1, (byte) 43); list.set(1, (byte) 43);
System.out.println(list); System.out.println(list);
list.set(0, (byte) 8); // list.set(0, (byte) 8);
System.out.println(list); //// 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); // String s = list.toString();
System.out.println(list.get(2)); // System.out.println(s);
list.get(1);
System.out.println(list.get(1));
list.size(); // list.copy();
System.out.println(list.size()); // System.out.println(list.copy());
list.contains(43); // returns: true
System.out.println(list.contains(43));
list.contains(4);
System.out.println(list.contains(4));
String s = list.toString(); // ByteList list2 = list.copy();
System.out.println(s); // list2.set(0, (byte) 6);
// System.out.println(list2);
list.copy(); // list.clear();
System.out.println(list.copy()); // System.out.println(list);
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) { for (byte value : list) {
System.out.println(value); System.out.println(value);
@ -156,6 +153,7 @@ public class Main {
// sort the lists with the sorting algorithm // sort the lists with the sorting algorithm
bubble.sort(list.copy()); bubble.sort(list.copy());
System.out.println(bubble.sort(list.copy()));
// merge.sort(list.copy()); // merge.sort(list.copy());
// bogo.sort(list.copy()); // bogo.sort(list.copy());
} }