7

私はここでもプログラミングも初めてです。他のトピックを一人で勉強しようとしているのですが、質問があるときにインストラクターが十分に助けてくれないので、ここに行きます. 汎用スタックで単語を逆にしたい。

私の pop、push、isEmpty および peek メソッドは機能します (このプログラムで試す前に、作成したより単純なプログラムでテストしました)。出力では、char ごとに反転した単語 char が返されますが、前に常に null が返されます。各文字!

私の質問は次のとおりです。なぜこれが起こっているのですか?そして、容量が9のときに機能するexpandCapacityメソッドがありますが、入力が制限を超えると適用されません。


これが私のコードです

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);

        }

    }
}

ここに出力があります

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)

編集:これが私が言った、機能しているメソッドです。

@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;


    }



}
4

3 に答える 3

6
private static String out;

の値outは null です。

out = out + u;
// This is null = null + u;

したがって、出力の先頭に null があります。

out初期値を与えるには、新しい String オブジェクトを作成するだけです。

 private static String out = "";
于 2013-03-08T20:03:48.770 に答える
1

なぜExpandCapacityそこにビットが必要なのかわかりませんが、これも機能します:

public static void main(String[] args)
    {       

    String word ="reverse please";      
    Stack<Character> chStack = new Stack<Character>();      
    for (int i = 0; i < word.length(); i ++)
    {       
        chStack.push(word.charAt(i));       
    }

    String out = "";
    while (chStack.size() != 0)
    {
        out += chStack.pop();
        System.out.println(out);

    }               
}
于 2013-03-08T20:16:02.300 に答える
1

いくつかの注意事項があります。

  • あなたはジェネリッククラスを書いていないので、ドロップしてください。
  • 反復はできる限りそのままにしておきます。
  • 可能な限り Java 標準クラスを使用してみてください。この場合は、ArrayStack ではなく Stack を使用してください。
  • スタックのサイズを変更する必要はありません。データを追加すると、スタックのサイズが動的に処理されます。
  • すべてのステップで一度ではなく、文字列の作成が完了したら、文字列を書き込む必要があります。
  • + を使用して文字列を追加するのは非常に非効率的です。StringBuilder を使用します。
  • コードを読みやすくするメソッドを使用します。

コードは次のとおりです。

import java.util.Scanner; 
import java.util.Stack;

public class ReverseDriver {
  public static String reverse(String string) {
    Stack<Character> revStack = new Stack<Character>();
    for (char c : string.toCharArray()) {
      revStack.push(c);
    }
    StringBuilder builder = new StringBuilder();
    while(!revStack.isEmpty()){
      builder.append(revStack.pop());
    }
    return builder.toString();
  }

  public static void main(String[]args){
    Scanner input = new Scanner(System.in);
    System.out.println("Enter your sentence: ");
    String in = input.nextLine();
    System.out.println(reverse(in));
  }
}
于 2013-03-08T20:28:33.343 に答える