Browse Source

Leo: Formatierung von SparseVector.java verschönert

master
fdai7303 6 months ago
parent
commit
310faffd86
  1. 4
      CheckSparseVector.java
  2. 130
      FullVersion(fromElsewhere)/CheckSparseVector.java
  3. 227
      FullVersion(fromElsewhere)/SparseVector.java
  4. 221
      SparseVector.java

4
CheckSparseVector.java

@ -1,4 +0,0 @@
public class CheckSparseVector {
//heyhey
//test leo
}

130
FullVersion(fromElsewhere)/CheckSparseVector.java

@ -1,130 +0,0 @@
public class CheckSparseVector {
public static void main(String[] args) {
//(ausgelagert)
// erstellen des this.Vektors = control Vector
SparseVector controlVector = new SparseVector(10);
controlVector.setElement(0, 10.0);
controlVector.setElement(5, 123.213);
controlVector.setElement(8, 65.01);
controlVector.setElement(9, 112.79);
System.out.println("\n");
// erstellen der testVektoren (the 5 "others")
SparseVector otherVector1 = new SparseVector(10);
otherVector1.setElement(0, 10.0);
otherVector1.setElement(5, 123.213);
otherVector1.setElement(8, 65.01);
otherVector1.setElement(9, 112.79);
System.out.println("\n");
SparseVector otherVector2 = new SparseVector(5);
otherVector2.setElement(0, 11.0);
otherVector2.setElement(3, 22.2);
System.out.println("\n");
SparseVector otherVector3 = new SparseVector(20);
otherVector3.setElement(0,123);
otherVector3.setElement(5,11);
otherVector3.setElement(11,2);
otherVector3.setElement(14,3);
otherVector3.setElement(18,4);
System.out.println("\n");
SparseVector otherVector4 = new SparseVector(10); // ein leerer other. Vektor mit Länge 10
System.out.println("der Wert des otherVector4 an der Position 5 ist: "+ otherVector4.getElement(5)); // 0.0
System.out.println("der Wert des otherVector4 an der Position 7 ist: "+ otherVector4.getElement(7));// 0.0
System.out.println("der Wert des otherVector4 an der Position 9 ist: " + otherVector4.getElement(9));// 0.0
System.out.println("die Länge des otherVector4 ist: " + otherVector4.getLength()); // 10
System.out.println("\n");
SparseVector otherVector5 = new SparseVector(20); // ein leerer other. Vektor mit Länge 10
System.out.println("der Wert des otherVector5 an der Position 5 ist: "+ otherVector5.getElement(5)); // 0.0
System.out.println("der Wert des otherVector5 an der Position 17 ist: "+ otherVector5.getElement(17));// 0.0
System.out.println("der Wert des otherVector5 an der Position 19 ist: " + otherVector5.getElement(19));// 0.0
System.out.println("die Länge des otherVector5 ist: " + otherVector5.getLength()); // 20
System.out.println("\n");
// Testen des this. Vektors --> testen Methoden wie getLength(), getElement(), setElement(), removeElement()
System.out.println("die Länge des controlVector ist: " + controlVector.getLength()); // 10
System.out.println("der Wert des controlVector an der Position 5 ist: "+ controlVector.getElement(5)); // 123.213
System.out.println("der Wert des controlVector an der Position 0 ist: " + controlVector.getElement(0)); //10.0
System.out.println("der Wert des controlVector an der Position 1 ist: " + controlVector.getElement(1)); //0.0
// testen equal(), wenn die beiden Vektoren identisch sind should be true
System.out.println(controlVector.equals(otherVector1));
System.out.println("\n");
controlVector.removeElement(5);
System.out.println("der Wert des controlVector an der Position 5 ist: "+ controlVector.getElement(5)); // 0.0
System.out.println("Die Länge nach remove ist: "+ controlVector.getLength()); // 10
controlVector.removeElement(7);
System.out.println("der Wert des controlVector an der Position 7 ist: "+ controlVector.getElement(7)); // 0.0
controlVector.setElement(5, 100);
System.out.println("der Wert des controlVector an der Position 5 ist: "+ controlVector.getElement(5)); //100.0
System.out.println("\n");
// testen equals(other) --> nicht identisch
System.out.println(controlVector.equals(otherVector1)); // should be false, weil der Wert an Index 5 zu 100 gesetzt wurde
System.out.println(controlVector.equals(otherVector2)); // should be false
System.out.println(controlVector.equals(otherVector3)); // should be false
System.out.println(controlVector.equals(otherVector4)); // should be false
System.out.println("\n");
otherVector1.setElement(5, 100);
System.out.println("der Wert des otherVector1 an der Position 5 ist: "+ otherVector1.getElement(5)); //100.0
System.out.println(otherVector1.equals(controlVector));// should be true, weil der Wert an Index 5 von this und other Vektor gleich 100
System.out.println("\n");
// testen add()
controlVector.add(otherVector1);
System.out.println("die Länge des controlVector ist: " + controlVector.getLength()); // 10
System.out.println("der Wert des controlVector an der Position 0 ist: "+ controlVector.getElement(0)); // 10 + 10 = 20
System.out.println("der Wert des controlVector an der Position 5 ist: " + controlVector.getElement(5)); // 100 + 100 = 200
System.out.println("der Wert des controlVector an der Position 6 ist: " + controlVector.getElement(6)); // 0.0
System.out.println("der Wert des controlVector an der Position 8 ist: " + controlVector.getElement(8)); // 65.01 + 65.01 = 130.02
System.out.println("der Wert des controlVector an der Position 9 ist: " + controlVector.getElement(9)); // 112.79 + 112.79 = 225.58
System.out.println("\n");
controlVector.add(otherVector2);
controlVector.setElement(3,90.1);
System.out.println("der Wert des controlVector an der Position 0 ist: "+ controlVector.getElement(0)); // 20
System.out.println("der Wert des controlVector an der Position 3 ist: " + controlVector.getElement(3)); // 90.1
System.out.println("\n");
controlVector.add(otherVector3);
System.out.println("der Wert des controlVector an der Position 0 ist: "+ controlVector.getElement(0)); // 20
System.out.println("der Wert des controlVector an der Position 5 ist: "+ controlVector.getElement(5)); // 200
System.out.println("der Wert des controlVector an der Position 9 ist: "+ controlVector.getElement(9)); // 225.58
System.out.println("\n");
controlVector.add(otherVector4);
System.out.println("der Wert des controlVector an der Position 0 ist: "+ controlVector.getElement(0));
System.out.println("der Wert des controlVector an der Position 5 ist: "+ controlVector.getElement(5));
System.out.println("\n");
controlVector.removeElement(0);
System.out.println("der Wert des controlVector an der Position 0 ist: "+ controlVector.getElement(0)); // 0.0
System.out.println("der Wert des controlVector an der Position 5 ist: "+ controlVector.getElement(5)); // 200
System.out.println("\n");
controlVector.add(otherVector5);
System.out.println("der Wert des controlVector an der Position 0 ist: "+ controlVector.getElement(0)); // 0.0
System.out.println("der Wert des controlVector an der Position 5 ist: "+ controlVector.getElement(5)); // 200
System.out.println("\n");
}
}

227
FullVersion(fromElsewhere)/SparseVector.java

@ -1,227 +0,0 @@
public class SparseVector {
// Hilfsklasse Node
private class Node {
int index;
double value;
Node next;
public Node(int index, double value, Node next) {
this.index = index;
this.value = value;
this.next = next;
}
}
// Standard Konstruktor: leeren Vektor initialisieren
private Node head = null;
private int length = 0;
public SparseVector() {
this.head = null;
this.length = 0;
}
// Konstruktor: erzeugen Vektor mit Lange n
public SparseVector (int n) {
this.head = null;
this.length = n;
}
/* setElement(int index, double value): add value into each index
(case1: wenn der Knoten an dem Index mit einem Wert existiert, den Wert aktualisieren)
(case2: wenn der Knoten an dem Index noch nicht existiert, ein neuer Knoten mit neuem Wert hinzüfugen)
*/
public void setElement(int index, double value) { //z.B. wenn ich zu index (6) einen value (1) einfüge,
removeElement(index);
if (this.head == null || this.head.index > index) {
// aber entweder Element nicht existiert oder das erste Element ist > 6 (Start: index 7)
this.head = new Node(index, value, this.head); // erzeugen eines neuen Kopfs
}
Node current = this.head;
while (current.next != null && current.next.index < index) { // Zeiger bewegt sich durch den Vektor, solange der gesuchte Index noch nicht erreicht wird
current = current.next;
}
if (current != null && current.index == index) { // und der gesuchte Index erreicht wird
current.value = value;
System.out.println("at index " + index + " set value " + value);
} else if (current.next == null || current.next.index > index) { // zum Beispiel, wenn Zeiger am Ende ankommt, ohne den gesuchten Index
// oder next index ist 7 und ich brauche index 6, einfügen das Element index 6 vor index 7
current.next = new Node(index, value, current.next); // siehe ipad Testat1 setElement
System.out.println("at index " + index + " set value " + value);
}
}
/* getElement: return the Wert value of that index
input das Index, und return den entsprechenden Wert des Index
*/
public double getElement(int index) {
if (index < 0 || index >= this.length) { // für Testfall wenn Länge < index
throw new IndexOutOfBoundsException("Index " + index + " ist außerhalb der Vektorlänge " + this.length + ". Max. Index ist " + (this.length-1));
}
Node current = this.head;
while (current != null && current.index < index) {
current = current.next;
}
if (current != null && current.index == index) {
double result = current.value;
return result;
}
return 0.0;
}
// removeElement: remove the whole element per index
public void removeElement(int index) { // siehe ipad Testat1 removeElement
Node current = this.head;
Node previous = null;
if (current != null && current.index == index) {
this.head = current.next;
System.out.println(index + " found and deleted");
return;
}
while (current != null && current.index != index) {
previous = current;
current = current.next;
}
if (current != null && current.index == index) {
previous.next = current.next;
System.out.println("index " + index + " found and the value " + current.value + " deleted.");
} else {
System.out.println("Element not found with index " + index);
}
}
// getLength: return the length (max. capacity) of Vektor
public int getLength() {
int length = SparseVector.this.length;
return length;
}
// bool Methode equals: testen, ob other = this (nur vergleichen die Nicht-Null Elemente)
public boolean equals(SparseVector other) {
Node thisCurrent = this.head;
Node otherCurrent = other.head;
while (thisCurrent != null || otherCurrent != null) {
if (thisCurrent != null && otherCurrent != null) {
if (!(thisCurrent.index == otherCurrent.index && thisCurrent.value == otherCurrent.value)) {
return false;
}
thisCurrent = thisCurrent.next;
otherCurrent = otherCurrent.next;
// wenn die Anzahl der Positionen mit Nicht-Null-Elementen nicht übereinstimmen = nicht identisch
} else if ((thisCurrent != null && otherCurrent == null) || (otherCurrent != null && thisCurrent == null)) {
return false;
}
}
return true;
}
// void add: to add two vectors together and renew (overwrite) the this.vector
public void add(SparseVector other) {
Node thisCurrent = this.head;
Node otherCurrent = other.head;
Node thisPrevious = null;
// Fall 0: 2 Vektoren mit unterschiedlichen Längen geht nicht!
if (this.getLength() != other.getLength()) {
System.out.println("Vektoren mit unterschiedlichen Längen können nicht zusammen addiert werden!!");
System.out.println("Länge des controlVector: " + this.getLength() + " aber die von otherVector: " + other.getLength());
return;
}
while (thisCurrent != null && otherCurrent != null) {
// Fall 1, gleicher Index, dann nur Werte zusammen addieren update this Vektor
if (thisCurrent.index == otherCurrent.index ) {
thisCurrent.value += otherCurrent.value; // overwrite the this. value
thisPrevious = thisCurrent;
thisCurrent = thisCurrent.next;
otherCurrent = otherCurrent.next;
}
// Fall 2: Der Index von otherCurrent ist kleiner, füge diesen Knoten in this ein
else if (otherCurrent.index < thisCurrent.index) {
Node newNode = new Node(otherCurrent.index, otherCurrent.value, thisCurrent);
if (thisPrevious == null) {
this.head = newNode;
} else {
thisPrevious.next = newNode;
}
// this Vektor Zeiger und other Vektor Zeiger gehen weiter voran
otherCurrent = otherCurrent.next;
thisPrevious = newNode;
// Fall 3: Der Index von otherCurrent > thisCurrent, 2 Zeiger gehen weiter
} else if (otherCurrent.index > thisCurrent.index) {
thisPrevious = thisCurrent;
thisCurrent = thisCurrent.next;
}
}
}
}

221
SparseVector.java

@ -1,5 +1,6 @@
public class SparseVector {
//Paul:
private class Node {
int index;
double value;
@ -13,7 +14,7 @@ public class SparseVector {
}
// Standard Konstruktor mit einem leeren Vektor initialisieren
//Elif: Standard Konstruktor mit einem leeren Vektor initialisieren
private Node head = null;
private int length = 0;
@ -22,55 +23,47 @@ public class SparseVector {
this.length = 0;
}
// Konstruktor mit Vektor länge n
public SparseVector (int n) {
//Elif: Konstruktor mit Vektor länge n
public SparseVector(int n) {
this.head = null;
this.length = n;
}
/* den value in index hinzufügen
- Aktualiiseren des Wertes, wenn der head an dem Index mit einem Wert exisitiert
- Neuer head mit neuem Wert hinzufügen, wenn der head an dem Index nicht existiert
*/
public void setElement(int index, double value) {
//Elif: den value in index hinzufügen - Aktualiiseren des Wertes, wenn der head an dem Index mit einem Wert exisitiert - Neuer head mit neuem Wert hinzufügen, wenn der head an dem Index nicht existiert
public void setElement(int index, double value) {
removeElement(index);
removeElement(index);
if (this.head == null || this.head.index > index) {
this.head = new Node(index, value, this.head);
this.head = new Node(index, value, this.head);
}
//current = now
// current = now
Node now = this.head;
while (now.next != null && now.next.index < index) {
while (now.next != null && now.next.index < index) {
now = now.next;
}
// gesuchte index gefunden
if (now != null && now.index == index) {
now.value = value;
System.out.println("at index " + index + " set value " + value);
// gesuchte index gefunden
if (now != null && now.index == index) {
now.value = value;
System.out.println("at index " + index + " set value " + value);
} else if (now.next == null || now.next.index > index) {
now.next = new Node(index, value, now.next);
} else if (now.next == null || now.next.index > index) {
now.next = new Node(index, value, now.next);
System.out.println("at index " + index + " set value " + value);
}
}
/* getElement: return the Wert value of that index
input das Index, und return den entsprechenden Wert des Index
*/
//Paul: getElement: return the Wert value of that index input das Index, und return den entsprechenden Wert des Index
public double getElement(int index) {
if (index < 0 || index >= this.length) { // für Testfall wenn Länge < index
throw new IndexOutOfBoundsException("Index " + index + " ist außerhalb der Vektorlänge " + this.length + ". Max. Index ist " + (this.length-1));
throw new IndexOutOfBoundsException("Index " + index + " ist außerhalb der Vektorlänge " + this.length
+ ". Max. Index ist " + (this.length - 1));
}
Node now = this.head;
@ -82,140 +75,126 @@ public class SparseVector {
if (now != null && now.index == index) {
double result = now.value;
double result = now.value;
return result;
}
return 0.0;
}
// entfernt Element nach Index
public void removeElement(int index) {
//Leonhard: entfernt Element nach Index
public void removeElement(int index) {
// previous = pre
Node now = this.head;
Node pre = null;
if (now != null && now.index == index) {
this.head = now.next;
return;
Node now = this.head;
Node pre = null;
}
if (now != null && now.index == index) {
this.head = now.next;
return;
}
while (now != null && now.index != index) {
while (now != null && now.index != index) {
pre = now;
now = now.next;
}
if (now != null && now.index == index) {
pre.next = now.next;
// System.out.println("index " + index + " found and the value " + now.value + "
// deleted.");
} else {
// System.out.println("Element not found with index " + index);
}
}
pre = now;
now = now.next;
//Leonhard: Länge des Vektors ausgeben
public int getLength() {
}
if (now != null && now.index == index) {
int length = SparseVector.this.length;
pre.next = now.next;
// System.out.println("index " + index + " found and the value " + now.value + " deleted.");
return length;
} else {
}
// System.out.println("Element not found with index " + index);
//Terry: bool Methode equals: testen, ob other = this (nur vergleichen die Nicht-Null Elemente)
public boolean equals(SparseVector other) {
}
}
Node thisnow = this.head;
Node othernow = other.head;
while (thisnow != null || othernow != null) {
// Länge des Vektors ausgeben
if (thisnow != null && othernow != null) {
if (!(thisnow.index == othernow.index && thisnow.value == othernow.value)) {
return false;
}
public int getLength() {
thisnow = thisnow.next;
othernow = othernow.next;
int length = SparseVector.this.length;
// wenn die Anzahl der Positionen mit Nicht-Null-Elementen nicht übereinstimmen
// = nicht identisch
return length;
} else if ((thisnow != null && othernow == null) || (othernow != null && thisnow == null)) {
return false;
}
}
return true;
}
// bool Methode equals: testen, ob other = this (nur vergleichen die Nicht-Null Elemente)
public boolean equals(SparseVector other) {
Node thisnow = this.head;
Node othernow = other.head;
// Terry: void add: to add two vectors together and renew (overwrite) the this.vector
public void add(SparseVector other) {
while (thisnow != null || othernow != null) {
Node thisnow = this.head;
Node othernow = other.head;
Node thispre = null;
if (thisnow != null && othernow != null) {
if (!(thisnow.index == othernow.index && thisnow.value == othernow.value)) {
return false;
}
// Fall 0: 2 Vektoren mit unterschiedlichen Längen geht nicht!
thisnow = thisnow.next;
othernow = othernow.next;
if (this.getLength() != other.getLength()) {
// wenn die Anzahl der Positionen mit Nicht-Null-Elementen nicht übereinstimmen = nicht identisch
System.out.println("Vektoren mit unterschiedlichen Längen können nicht zusammen addiert werden!!");
System.out.println(
"Länge des controlVector: " + this.getLength() + " aber die von otherVector: " + other.getLength());
return;
} else if ((thisnow != null && othernow == null) || (othernow != null && thisnow == null)) {
}
return false;
}
while (thisnow != null && othernow != null) {
}
return true;
// Fall 1, gleicher Index, dann nur Werte zusammen addieren update this Vektor
if (thisnow.index == othernow.index) {
thisnow.value += othernow.value; // overwrite the this. value
thispre = thisnow;
thisnow = thisnow.next;
othernow = othernow.next;
}
// void add: to add two vectors together and renew (overwrite) the this.vector
public void add(SparseVector other) {
Node thisnow = this.head;
Node othernow = other.head;
Node thispre = null;
// Fall 0: 2 Vektoren mit unterschiedlichen Längen geht nicht!
// Fall 2: Der Index von othernow ist kleiner, füge diesen Knoten in this ein
else if (othernow.index < thisnow.index) {
if (this.getLength() != other.getLength()) {
System.out.println("Vektoren mit unterschiedlichen Längen können nicht zusammen addiert werden!!");
System.out.println("Länge des controlVector: " + this.getLength() + " aber die von otherVector: " + other.getLength());
return;
Node newNode = new Node(othernow.index, othernow.value, thisnow);
if (thispre == null) {
this.head = newNode;
} else {
thispre.next = newNode;
}
while (thisnow != null && othernow != null) {
// Fall 1, gleicher Index, dann nur Werte zusammen addieren update this Vektor
if (thisnow.index == othernow.index ) {
thisnow.value += othernow.value; // overwrite the this. value
thispre = thisnow;
thisnow = thisnow.next;
othernow = othernow.next;
}
// Fall 2: Der Index von othernow ist kleiner, füge diesen Knoten in this ein
else if (othernow.index < thisnow.index) {
Node newNode = new Node(othernow.index, othernow.value, thisnow);
if (thispre == null) {
this.head = newNode;
} else {
thispre.next = newNode;
}
// this Vektor Zeiger und other Vektor Zeiger gehen weiter voran
othernow = othernow.next;
thispre = newNode;
// this Vektor Zeiger und other Vektor Zeiger gehen weiter voran
othernow = othernow.next;
thispre = newNode;
// Fall 3: Der Index von othernow > thisnow, 2 Zeiger gehen weiter
} else if (othernow.index > thisnow.index) {
// Fall 3: Der Index von othernow > thisnow, 2 Zeiger gehen weiter
} else if (othernow.index > thisnow.index) {
thispre = thisnow;
thisnow = thisnow.next;
thispre = thisnow;
thisnow = thisnow.next;
}
}
}
}
}
}
}
Loading…
Cancel
Save