Browse Source

Kommentare3

master
elif.efe 6 months ago
parent
commit
f627fa4797
  1. 119
      SparseVector.java

119
SparseVector.java

@ -46,20 +46,25 @@ public class SparseVector {
this.length = n;
}
/* den value in index hinzufügen
* @author Elif
* @param index Stelle,w wo Vektor eingefügt werden soll
* @param value wert, den wir übergeben
- Aktualisieren des Wertes, wenn der firstNode an dem Index mit einem Wert exisitiert
- Neuer firstNode mit neuem Wert hinzufügen, wenn der firstNode an dem Index nicht existiert
*/
public void setElement(int index, double value) {
/**
* den value in index hinzufügen
* - Aktualisieren des Wertes, wenn der firstNode an dem Index mit einem Wert
* exisitiert
* - Neuer firstNode mit neuem Wert hinzufügen, wenn der firstNode an dem Index
* nicht existiert
*
* @author Elif
* @param index Stelle,w wo Vektor eingefügt werden soll
* @param value wert, den wir übergeben
*/
public void setElement(int index, double value) {
// methode wird aufgerufen, falls index existiert = platz schaffen
removeElement(index);
int maxIndex = this.getLength() - 1;
//1. if: schauen, ob Index im erlaubten Bereich
//2. if: schauen, ob erster Knoten null ist und index von erstem knoten größer als index, dann neuen knoten setzen
// 1. if: schauen, ob Index im erlaubten Bereich
// 2. if: schauen, ob erster Knoten null ist und index von erstem knoten größer
// als index, dann neuen knoten setzen
if (index > maxIndex) {
System.out.println("Fehler: Der Index " + index + " ist außerhalb des erlaubten Bereichs.");
return;
@ -69,8 +74,8 @@ public class SparseVector {
this.firstNode = new Node(index, value, this.firstNode);
}
//hilfsknoten erstellen
//solange im index und gesuchten index nicht erreicht haben, gehen wir weiter
// hilfsknoten erstellen
// solange im index und gesuchten index nicht erreicht haben, gehen wir weiter
Node now = this.firstNode;
while (now.next != null && now.next.index < index) {
@ -81,7 +86,7 @@ public class SparseVector {
if (now != null && now.index == index) {
now.value = value;
System.out.println("at index " + index + " set value " + value);
// knoten den wir erstellen wollten existiert nicht, deshlab new node
// knoten den wir erstellen wollten existiert nicht, deshlab new node
} 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);
@ -90,10 +95,14 @@ public class SparseVector {
}
/* getElement: return the Wert value of that index
input das Index, und return den entsprechenden Wert des Index
*/
/**
* return the Wert value of that index input das Index, und return den
* entsprechenden Wert des Index
*
* @author Paul
* @param index Stelle, an der der Wert steht
* @return double Wert des Elements
*/
public double getElement(int index) {
if (index < 0 || index >= this.length) { // für Testfall wenn Länge < index
@ -131,39 +140,41 @@ public class SparseVector {
return;
}
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);
}
}
// Länge des Vektors ausgeben
public int getLength() {
int length = SparseVector.this.length;
return length;
}
// Während wir momentan nicht bei Null sind und den Index nicht gefunden haben
// setzte pre und now den nächsten Node weiter
while (now != null && now.index != index) {
pre = now;
now = now.next;
}
// Wenn wir den Index gefunden haben und nicht bei Null sind
// setze den nächsten Node auf den momentanen Node
if (now != null && now.index == index) {
pre.next = now.next;
System.out.println("Node mit Wert: " + now.value + " am Index " + index + " gefunden und gelöscht.");
} else {
System.out.println("Keine Node am Index: " + index + " nicht gefunden!");
}
}
// bool Methode equals: testen, ob other = this (nur vergleichen die Nicht-Null Elemente)
/**
* Gibt die Länge des Vektors aus
*
* @author Leonhard
* @return int Vektor Länge
*/
public int getLength() {
return SparseVector.this.length;
}
public boolean equals(SparseVector other) {
/**
* testen, ob other = this (nur vergleichen die Nicht-Null Elemente)
*
* @author YC Terry
* @param other Anderer Vektor, mit dem verglichen wird
* @return boolean Wahrheitswert, ob Vektoren gleich sind
*/
public boolean equals(SparseVector other) {
if (this.getLength() != other.getLength()) {
@ -186,7 +197,6 @@ public class SparseVector {
// wenn die Anzahl der Positionen mit Nicht-Null-Elementen nicht übereinstimmen
// = nicht identisch
} else if ((thisnow != null && othernow == null) || (othernow != null && thisnow == null)) {
return false;
@ -196,16 +206,19 @@ public class SparseVector {
return true;
}
// void add: to add two vectors together and renew (overwrite) the this.vector
public void add(SparseVector other) {
/**
* to add two vectors together and renew (overwrite) the this.vector
*
* @author YC Terry
* @param other Anderer Vektor, der auf addiert wird
*/
public void add(SparseVector other) {
Node thisnow = this.firstNode;
Node othernow = other.firstNode;
Node thispre = 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!!");
@ -251,4 +264,4 @@ public class SparseVector {
}
}
}
}
Loading…
Cancel
Save