Browse Source

terry: Logiklücken lösen in methoden getElement und setElement

master
Terry Kwan 6 months ago
parent
commit
15e520952f
  1. 14
      CheckSparseVector.java
  2. 9
      SparseVector.java

14
CheckSparseVector.java

@ -22,8 +22,6 @@ public class CheckSparseVector {
otherVector1.setElement(9, 112.79);
otherVector1.setElement(10, -212.79);
otherVector1.setElement(12, 12);
System.out.println("\n");
SparseVector otherVector2 = new SparseVector(5);
@ -62,19 +60,19 @@ public class CheckSparseVector {
}
System.out.println("\n");
SparseVector otherVector5 = new SparseVector(20); // ein leerer other. Vektor mit Länge 10
SparseVector otherVector5 = new SparseVector(20); // ein leerer other. Vektor mit Länge 20
System.out.println("der Wert des otherVector5 an der Position 0 ist: "+ otherVector5.getElement(0)); // 0.0
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
try{
System.out.println(otherVector4.getElement(20));
System.out.println(otherVector5.getElement(20));
} catch(Exception e){
System.out.println(e);
}
try{
System.out.println(otherVector4.getElement(-1));
System.out.println(otherVector5.getElement(-1));
} catch(Exception e){
System.out.println(e);
}
@ -96,6 +94,8 @@ public class CheckSparseVector {
} catch(Exception e){
System.out.println(e);
}
System.out.println("\n");
// testen equal(), wenn die beiden Vektoren identisch sind should be true, not anymore :)
System.out.println(controlVector.equals(otherVector1));
System.out.println("\n");
@ -103,14 +103,14 @@ public class CheckSparseVector {
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
System.out.println("\n");
controlVector.removeElement(7);
System.out.println("der Wert des controlVector an der Position 7 ist: "+ controlVector.getElement(7)); // 0.0
System.out.println("\n");
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

9
SparseVector.java

@ -33,6 +33,13 @@ public class SparseVector {
public void setElement(int index, double value) {
removeElement(index);
int maxIndex = this.getLength()-1;
if (index > maxIndex) {
System.out.println("Fehler: Der Index " + index + " ist außerhalb des erlaubten Bereichs.");
return;
}
if (this.head == null || this.head.index > index) {
this.head = new Node(index, value, this.head);
@ -68,7 +75,7 @@ public class SparseVector {
Node now = this.head;
while (now != null && now.index < index) {
while (now != null) {
if (now.index == index) {
return now.value;
}

Loading…
Cancel
Save