Stapel in eine Zeichenfolge verwandeln?

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Stapel in eine Zeichenfolge verwandeln?

by Anonymous » 03 Mar 2025, 22:26

Ich arbeite an einem Projekt für den Unterricht und wir müssen eine Methode erstellen, die den von uns erstellten Stapel annimmt und wenn er als String ausgedruckt wird. Das habe ich bisher; Die Methode ist unten genannt toString ();. < /p>

Code: Select all

package edy.bsu.cs121.stack;

import javax.swing.JOptionPane;

public class myArrayStack {

private final int DEFAULT_CAPACITY = 100;
private int top;
private T[] stack;

@SuppressWarnings("unchecked")
public myArrayStack(){

top = 0;
stack = (T[])(new Object[DEFAULT_CAPACITY]);

}

@SuppressWarnings("unchecked")
public myArrayStack(int initialCapacity){

top = 0;
stack = (T[])(new Object[initialCapacity]);

}

public void myPush(String first){

stack[top] = (T) first;
top++;
}

public T myPop(){

if (isEmpty()){
JOptionPane.showMessageDialog(null, "Stack is empty!");
} else {
top--;
}
T result = stack[top];
stack[top] = null;
return result;
}

public T myPeek(){
if (isEmpty()){
JOptionPane.showMessageDialog(null, "Stack is empty!");
}
return stack[top-1];
}

public boolean isEmpty(){

if (top == 0){
return true;
}

return false;
}

public int mySize(){
return top;
}

//Searches for a name

public int mySearch (T element) {
{
int spot = -1;
for(int i = 0; i< top; i ++){
if(element.equals(stack[i])){
spot = i;
}
}
return spot;
}
}

public String toString(){
int i=top;

do {
T result = stack[top-i];
i--;
return (String) result;
} while (i>=0);

}

}

Top