Jestem tu nowy, także w programowaniu. Próbuję studiować inne tematy sam, ponieważ mój instruktor nie jest wystarczająco pomocny, gdy mam pytanie, więc tutaj jest. Chcę odwrócić słowo z ogólnym stosem.Odwracanie słowa ze stosem
Moje pop, push, isEmpty i peek metody działają (testowałem je za pomocą prostszego programu, który zrobiłem, zanim wypróbowałem to na tym.), A wynik wydaje mi się odwrotnym słowem char przez char, ale zawsze dając mi zerowy przed każdym char!
Moje pytania są następujące: Dlaczego tak się dzieje? I chociaż mam metodę expandCapacity do pracy, gdy pojemność wynosi 9, ale nie ma zastosowania, gdy dane wejściowe przekroczy limit.
Oto mój kod
package Stack;
import java.util.Scanner;
public class ReverseDriver<T> {
private static String out;
private static String in;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter your sentence: ");
in = input.nextLine();
int size = in.length();
ArrayStack<Character> revStack = new ArrayStack<>(size);
for (int i = 0; i < in.length(); i++) {
char u = in.charAt(i);
revStack.Push(u);
if (in.length() > 9) {
revStack.expandCapacity();
}
}
while (!revStack.IsEmpty()) {
char u = revStack.Pop();
out = out + u;
System.out.flush();
System.out.print(out);
}
}
}
Oto wyjściowy
run:
Enter a word:
word
nullr
nullro
nullrow
Exception in thread "main" java.lang.NullPointerException
at Stack.ReverseDriver.main(ReverseDriver.java:37)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
EDIT: oto sposoby, że powiedziałem, że pracowali.
@Override
public void Push (T element)
{
if (count == stack.length){
expandCapacity();
}
stack[++count] = element;
//System.out.println(count);
}
@Override
public String toString()
{
String result = "<top of stack>\n";
for (int index=count-1; index >= 0; index--){
result += stack[index] + "\n";
}
return result + "<bottom of stack>";
}
@Override
public boolean IsEmpty()
{ //Checks if array is empty
if(count == 0){
System.out.println("Nothing");
}
return count == 0;
}
public T Pop()
{
T output;
output = (stack[count - 1]);
count--;
return(output);
}
@Override
public T Peek()
{
//looks at the object at the top of this stack without removing it
//from the stack.
if(stack.length == 0){
// {
System.out.println("Cant peek a ghost");
}
return(stack[--count]);
}
// else
// {
// System.out.println(stack[count-1]);
// }
// }
@Override
public int Size()
{
//Sets the size of this vector
if(stack.length == 0){
System.out.println("Nothing inside");
}
System.out.println("The array's size is : " + count);
return count;
}
}
Choć doceniam, że mówiłeś, że to działa, możesz podać nam kod w metodzie expandCapacity i isEmpty, żeby dać nam trochę więcej wgląd dlaczego tak jest dzieje się :) – christopher
Czy jesteś pewien, że to ten sam kod generujący dane wyjściowe - "Enter a word" vs "Enter a sentence" –
zauważa: 'expandCapacity' zostaje wywołane 10 razy, jeśli' in.length() 'wynosi 10, czy to zamierzony? Również 'in' i' out' powinny bardziej poprawnie być zmiennymi lokalnymi w main, nie statycznymi (przynajmniej jeśli był to "prawdziwy" program). – hyde