Hallo,
Ich habe ein "Stack"-Programm programmiert.
Das Programm hat ein Objekt "Stack" was n ur ein hinzufügen zulässt, wenn das aktuelel feld frei ist wenn der String nicht voll ist(Im fall eines nicht vollen arrays und das aktuelle frei aber besetzt ist, wird der zähler um eins inkrementiert.
Nur krieg ich einen Fehler das das Array auf -1 zugreifen will. Ich erkenne aber leider den Fehler nicht!
Hier mal die beiden Klassen:
Mainclass:
Der Fehler:
Danke schonmal
Gruß
Daniel
Ich habe ein "Stack"-Programm programmiert.
Das Programm hat ein Objekt "Stack" was n ur ein hinzufügen zulässt, wenn das aktuelel feld frei ist wenn der String nicht voll ist(Im fall eines nicht vollen arrays und das aktuelle frei aber besetzt ist, wird der zähler um eins inkrementiert.
Nur krieg ich einen Fehler das das Array auf -1 zugreifen will. Ich erkenne aber leider den Fehler nicht!
Hier mal die beiden Klassen:
Mainclass:
Code:
package stack;
import java.util.*;
public class StackUI {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
Stack MeinStack = new Stack(5);
String auswahl = "e";
auswahl = menue(auswahl);
auswahl = auswahl.toLowerCase();
while (!auswahl.equals("e")) {
if (auswahl.equals("a"))
{
System.out.println("Bitte geben Sie die Information ein:");
MeinStack.push(s.next());
}
if (auswahl.equals("d"))
MeinStack.pop();
if (auswahl.equals("o)")) {
ausgabe_stack(MeinStack);
}
auswahl = menue(auswahl);
if (auswahl.equals("e"))
System.out.println("Programm beendet.");
}
}
public static String menue(String auswahl) {
Scanner s = new Scanner(System.in);
System.out.println("Ablegen/push(a) , löschen/pop(d), beenden (e) oder ausgeben(o)?");
auswahl = s.next();
return auswahl;
}
public static void ausgabe_stack (Stack Stack) {
System.out.println("Test");
for (int i=0;i<Stack.getMax();i++) {
System.out.println("Stapel-Nr "+i+": +"+Stack.showStack()+"");
}
}
}
//Copyright by Daniel Roß
Code:
package stack;
public class Stack {
private int maxAnzahl;
private int aktuellerSlot=0;
String[] Stack;
public Stack (int maxAnzahl)
{
Stack = new String[maxAnzahl];
}
//Funktion zum hinzufügen einer Information eines Stacks
public void push (String ihnhalt) {
if (this.Stack[this.aktuellerSlot] != null && !this.isFull())
{
this.aktuellerSlot++;
}
if (this.Stack[this.aktuellerSlot] == null) {
this.Stack[this.aktuellerSlot] = ihnhalt;
}
if (this.Stack[this.aktuellerSlot] == null && !this.isFull())
{
this.aktuellerSlot++;
}
}
public int getMax () {
return this.maxAnzahl;
}
//Funktion zum löschen einer Information eines Stacks
public void pop () {
if (this.Stack[this.aktuellerSlot] == null)
{
this.aktuellerSlot--;
this.Stack[this.aktuellerSlot] = null;
}
if (this.aktuellerSlot==0) //auskommentiert: Stack wird wie Sie gesagt hatten, wenn es komplett leer ist, auf null gesetzt.
this.Stack[this.aktuellerSlot] = null; //Nur wenn es auf Null gesetzt wird, können bei einem erneuten push keine Werte mehr eingetragen werden!
else
{
this.Stack[this.aktuellerSlot] = null;
this.aktuellerSlot--;
}
}
//Gibt True zurück, wenn der oberste Slot nicht null ist.
public boolean isFull() {
if (this.Stack[maxAnzahl-1] != null)
return true;
else
return false;
}
//Gibt True zurück, wenn der unterterste Slot null ist.
public boolean isEmpty() {
if (this.Stack[0] == null)
return true;
else
return false;
}
public String[] showStack() {
return this.Stack;
}
}
//Copyright by Daniel Roß
Der Fehler:
Aber wenn ich einmal "a" aufrufe und data1 als Ihnhalt angebe, will er die schon bei -1 eingetragen: Doch wieso?Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at stack.Stack.isFull(Stack.java:57)
at stack.Stack.push(Stack.java:25)
at stack.StackUI.main(StackUI.java:23)
Danke schonmal
Gruß
Daniel