0

私のコードは次のようなものです:

    public class Test() {

    String [] ArrayA = new String [5] 

    ArrayA[0] = "Testing";

      public void Method1 () {

            System.out.println(Here's where I need ArrayA[0])

         }

     }

さまざまな方法を試しましたが(しゃれは意図していません)、どれも機能しませんでした。私が得ることができる助けをありがとう!

4

5 に答える 5

1
public class Test {

    String [] arrayA = new String [5]; // Your Array

    arrayA[0] = "Testing";

    public Test(){ // Your Constructor

        method1(arrayA[0]); // Calling the Method

    }

      public void method1 (String yourString) { // Your Method

            System.out.println(yourString);

         }

     }

new Test();
メイン クラスでは、Test のインスタンスを作成してメイン クラスからメソッドを呼び出したい場合は、OR を呼び出すことができます。

public class Test {

    public Test(){ // Your Constructor

        // method1(arrayA[0]); // Calling the Method // Commenting the method

    }

      public void method1 (String yourString) { // Your Method

            System.out.println(yourString);

         }

     }

メイン クラスで、クラスに test のインスタンスを作成しますmain

Test test = new Test();

String [] arrayA = new String [5]; // Your Array

arrayA[0] = "Testing";

test.method1(arrayA[0]); // Calling the method

そして、メソッドを呼び出します。

編集:

methodヒント: andvariableを大文字で始めないというコーディング標準があります。
また、クラスの宣言には().

于 2013-05-17T05:33:38.397 に答える
0

配列を渡すことについて話している場合は、それについてきちんとして可変引数を使用してみませんか :) 単一の文字列、複数の文字列、または文字列 [] を渡すことができます。

// All 3 of the following work!
method1("myText");
method1("myText","more of my text?", "keep going!");
method1(ArrayA);

public void method1(String... myArray){
    System.out.println("The first element is " + myArray[0]);
    System.out.printl("The entire list of arguments is");
    for (String s: myArray){
        System.out.println(s);
    }
}
于 2013-05-17T05:49:51.267 に答える
0

あなたが何をしようとしているのかわからない。匿名クラスを使用していない場合、それが Java コード (のように思われる) である場合、構文的に間違っています。

これがコンストラクター呼び出しの場合、以下のコード:

  public class Test1() {
    String [] ArrayA = new String [5]; 
    ArrayA[0] = "Testing";
      public void Method1 () {
            System.out.println(Here's where I need ArrayA[0]);
         }
     }

次のように記述します。

public class Test{
    public Test() {
    String [] ArrayA = new String [5]; 
    ArrayA[0] = "Testing";
        Method1(ArrayA);          
    }
    public void Method1(String[] ArrayA){
        System.out.println("Here's where I need " + ArrayA[0]);
    }
}
于 2013-05-17T06:09:29.407 に答える
0

これを試して

private void Test(){
    String[] arrayTest = new String[4];
    ArrayA(arrayTest[0]);
}

private void ArrayA(String a){
    //do whatever with array here
}
于 2013-05-17T05:29:36.493 に答える