ich habe mal wieder ein problem. Ich möchte (besser: soll) eine Methode schreiben, mit der ich einen String zu einem Array hinzufügen kann. Dieser soll allerdings nicht hinten angefügt werden, sondern lexikographisch bzw. alphabetisch.
Ja, natürlich wäre ein sort zum Schluss einfacher, aber so soll es eben nicht sein.
mein array heisst sortedArray
size ist mit 0 initialisiert worden. und das array wurde im constuctor mit der länge 10 angegeben.
Code:
public boolean add(String element) {
// Checks if there's space for another
// element otherwise returns false
if ( size == sortedArray.length)
{
System.out.println("sorry, array is full!");
return false;
}
else if (sortedArray[0] == null) {
sortedArray[0] = element;
size++;
System.out.println("first element has been added!");
return true;
}
// Goes through the array until the last
// element is reached
for(int i=0; i < size ; i++){
// Compares the String at i with the input element
// but ignores UPPER and lower case
if(sortedArray[i].compareToIgnoreCase(element) > 0) {
// Move all values in array greater than i one place to the right
System.arraycopy( sortedArray, i, sortedArray, i+1 , size-1 );
sortedArray[i] = element;
}
System.out.println("an element has been sorted and added!"+ i);
sortedArray[size] = element;
break;
}
size++;
return true;
}
in der for Schleife ist definitiv ein Fehler. Ich vermute die if anweisung zur compare to methode...
Wie gesagt: add("A"); add("C"); add("B") soll funktionieren
Bitte erklären, was ich falsch gemacht habe, damit ichs verstehe
for(int i=0; i < size ; i++){
// Compares the String at i with the input element
// but ignores UPPER and lower case
if(sortedArray[i].compareToIgnoreCase(element) > 0) {
// Move all values in array greater than i one place to the right
System.arraycopy( sortedArray, i, sortedArray, i+1 , size-1 );
sortedArray[i] = element;
}
System.out.println("an element has been sorted and added!"+ i);
sortedArray[size] = element;
break;
}
size++;
return true;
}
Also ich hab mir jetzt nicht den kompletten Code angeschaut, aber das break an dieser Stelle scheint mir stark fehler-verdächtig. Du brichst die Schleife ja ab und gehst nicht von 0 bis size - 1, sondern machst nur einen Durchgang.
Hallo, hat immernoch keiner eine Idee? Ich glaube ich mache einen Denkfehler mit der size variabel....
irgendwas stimmt da nicht.
Momentaner Stand:
Code:
public boolean add(String element) {
// Checks if there's space for another
// element otherwise returns false
if ( size == sortedArray.length)
{
System.out.println("sorry, array is full!");
return false;
}
// Goes through the array until the last
// element is reached
for(int i=0; i < size ; i++){
if (sortedArray[i] == null) {
sortedArray[i] = element;
size++;
System.out.println("first element has been added!");
break;
}
// Compares the String at i with the input element
// but ignores UPPER and lower case
if(sortedArray[i].compareToIgnoreCase(element) > 0) {
// Move all values in array greater than i one place to the right
System.arraycopy( sortedArray, i, sortedArray, i+1 , size-1 );
sortedArray[i] = element;
size++;
break;
}
}
size++;
return true;
}
ein add("A"), add("C"), add("B") ergibt [A, C] im array, statt A,B,C