-1

先生は、LIFO の原理に基づいてスタックの動作を再現し、スタック オーバーフローと空のスタックも示すプログラムを作成するように私たちに依頼しました。

3つのクラスを作成しました。String[] args を使用してユーザーから入力を取得するメイン メソッドを含む Retrievee クラス。他のクラス Push は Retrievee クラスの値を使用します クラス Push は、そのサイズに応じて値を受け入れ、値がサイズを超えているかどうかをチェックし、オーバーフローを示し、取得ではなくクラス push の値を使用するクラス Pop に進みます。

class Push extends Retrivee
{
  int i;
  Push()
  {
    String push[]= new String[10];
    //int push[]=new int[10];
    for(i=0;i<12;i++)
    {
      //Retrivee ele= new Retrivee();
      push[i]=a[i];
      System.out.print("value in Push["+i+"]="+push[i]);
      if( i == 9 )
         {
           System.out.print("Stack for push has Overflown");
         }
    }
  }
}

class Pop extends Push
{
  int i;
  Pop()
  {
    String pop[]= new String[10];
    //int pop[]= new int[10];
    for(i=10;i>=-1;i--)
    {
      //Push ele1= new Push();
      pop[i]=push[i];
      System.out.print("value in Pop["+i+"]="+pop[i]);
      if(i == -1)
      {
       System.out.print("Stack for pop is empty");
      }
    }
  }
}
public class Retrivee
{
    public static void main(String args[] )
    {
        System.out.println("please enter 12 elements");
        String a[] = new String[13];
        //int a[]=new int [13];
        a[1]=args[1];
        a[2]=args[2];
        a[3]=args[3];
        a[4]=args[4];
        a[5]=args[5];
        a[6]=args[6];
        a[7]=args[7];
        a[8]=args[8];
        a[9]=args[9];
        a[10]=args[10];
        a[11]=args[11];

    /*
        a[0]=Integer.parseInt (args[0]);
        a[1]=Integer.parseInt (args[1]);
        a[2]=Integer.parseInt (args[2]);
        a[3]=Integer.parseInt (args[3]);
        a[4]=Integer.parseInt (args[4]);
        a[5]=Integer.parseInt (args[5]);
        a[6]=Integer.parseInt (args[6]);
        a[7]=Integer.parseInt (args[7]);
        a[8]=Integer.parseInt (args[8]);
        a[9]=Integer.parseInt (args[9]);
        a[10]=Integer.parseInt (args[10]);
        a[11]=Integer.parseInt (args[11]);
     */

     /* Push push1 =new Push();
        Pop pop1 =new Pop();

     */
    }
}

コンパイル時にエラーが発生しました

 Retrivee.java:11:error :cannot find symbol
    push[i]=a[i];
    symbol: variable a
    location:class Push
 Retrivee.java:31:error :cannot find symbol
    pop[i]=push[i];
    symbol: variable push
    location:class Pop

そのため、String[] args から取得した値を別のクラスに渡すことはできないのではないかと思いました。そのため、コードを少し改善しました。

class Push //extends Retrive
{
    public Push()
    {
        int i;
        String push[]= new String[10];
        //Retrive ele= new Retrive();
        try
        {
            for(i=0;i<13;i++)
            {
                push[i]=Retrive.a[i];
                System.out.println("value in Push["+i+"]="+push[i]);
            }
        }
        catch(Exception e)
        {
            System.out.println("Stack for push has Overflown");
        }
    }
}

class Pop //extends Push
{
    public Pop()
    {
        int j;
        String pop[]= new String[10];
        //Push ele1= new Push();
        try
        {
            for(j=9;j>=-1;j--)
            {
                pop[j]=Pop.push[j];
                System.out.println("value in Pop["+j+"]="+pop[j]);
            }
        }
        catch(Exception ev)
        {
            System.out.println("Stack for pop is empty");
        }
    }
}

public class Retrive 
{
    public static void main(String args[])
    {
        String a[] = new String[13];
        for(int k=0;k<13;k++)
        {
            int d[]=new int[13];
            d[k]=2*k;
            a[k]=Integer.toString(d[k]);
            System.out.println("a["+k+"]= "+a[k]);
        }
    }
}

しかし、再び同じエラーで終わった

 Retrive.java:12:error :cannot find symbol
    push[i]=Retrive.a[i];
    symbol: variable a
    location:class Retrive
 Retrive.java:34:error :cannot find symbol
    pop[i]=Pop.push[i];
    symbol: variable push
    location:class Pop

それから私はこのコードを試しましたが、メインメソッドを含む単一のクラスだけでした。

public class Retrivee
{
    public static void main(String args[] )
    {
        String a[] = new String[13];
        for(int k=0;k<13;k++)
        {
            int d[]=new int[13];
            d[k]=2*k;
            a[k]=Integer.toString(d[k]);
            System.out.println("a["+k+"]= "+a[k]);
        }
        int i;
        String push[]= new String[10];
        try
        {
            for(i=0;i<13;i++)
            {
                push[i]=a[i];
                System.out.println("value in Push["+i+"]="+push[i]);
            }
        }
        catch(Exception e)
        {
            System.out.println("Stack for push has Overflown");
        }
        int j;
        String pop[]= new String[10];
        try
        {
            for(j=9;j>=-1;j--)
            {
                pop[j]=push[j];
                System.out.println("value in Pop["+j+"]="+pop[j]);
            }
        }
        catch(Exception ev)
        {
            System.out.println("Stack for pop is empty");
        }
    }
}

そしてついにそれは働いた。上記の2つのケースで機能しなかったのはなぜですか? それが機能した可能性がある場合は、変更を行う方法と場所を教えてください。

最後に聞きたいのは、このように書いて main メソッドで String args[] のサイズを設定できない理由です。

  public static void main(String args[] = new String[10]) 
  {
  }

最初の例に従って、小さなコードを書きました

import java.util.*;
public class MyScan 
{
    public static void main(String[] args) 
    {
        //String[] args = new String[10];
        String[] in = new String[4];//= {"1 a 10 . 100 1000","12","2112"};
        in[0]= "1"; //args[0];
        in[1]= "2"; //args[1];
        in[2]= "3"; //args[2];
        String accum;
        for(int x = 0; x <3; x++)   
        {
            Scanner s = new Scanner(in[x]);
            accum = s.nextLine();
            System.out.println(accum);
        }
    }
} 

私が書いたとき

 in[0]= args[0];
 in[1]= args[1];
 in[2]= args[2];

投げた

  Exception in thread"main" java.lang.ArrayIndexOutOfBoundsException:0
  at MyScan.main<MyScan.java:9>

しかし、上記の初期化は別のプログラムで使用されたときに機能しましたが、多少異なる方法でした。

    String a= args[0];
    String b= args[1];
4

1 に答える 1

0

ここで複数の質問をしていますが...

cannot find symbolは、浮動変数への未定義の参照があることをコンパイラが伝える方法です。それらがどの変数であるかも教えてくれます。したがって、今のところ、何も定義されていませapush。それらを適切な値に宣言してインスタンス化します。

args[]基本的に無制限なので 、 のサイズを設定する必要はありません。args[]コマンドラインでパラメーターを渡すときに使用されます。

java program1 param1 param2 param3 param4 ... param99

...そして、これらのパラメータのそれぞれが になりますargs

これは for と書くことができmain、同じことを意味します:

public static void main(String... args) { }

上記は として知られていvarargsます。そのリファレンスを調べてみることをお勧めします。

于 2013-02-24T22:00:09.883 に答える