Aazim Abdul
Aazim Abdul

Reputation: 59

How do I pass an array of strings into another method?

My code is like this:

    public class Test() {

    String [] ArrayA = new String [5] 

    ArrayA[0] = "Testing";

      public void Method1 () {

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

         }

     }

I've tried various methods (No pun intended) but none worked. Thanks for any help I can get!

Upvotes: 0

Views: 152

Answers (5)

dharam
dharam

Reputation: 8096

I am not sure what you are trying to do. If it is java code(which it seems like) then it is syntactically wrong if you are not using anonymous classes.

If this is a constructor call then the code below:

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

should be written as this:

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

Upvotes: 0

anshulkatta
anshulkatta

Reputation: 2064

Try this Snippet :-

public class Test {

        void somemethod()
        {
            String [] ArrayA = new String [5] ;

                ArrayA[0] = "Testing";

                Method1(ArrayA);
        }
      public void Method1 (String[] A) {

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

         }
      public static void main(String[] args) {
        new Test().somemethod();
    }

}

The Class name should never had Test()

Upvotes: 0

Ravi Pal
Ravi Pal

Reputation: 395

try this

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

private void ArrayA(String a){
    //do whatever with array here
}

Upvotes: 0

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

         }

     }

In your main class, you can just call new Test();
OR if you want the method to be called from your main class by creating an instance of Test you may write:

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

         }

     }

In your main class, create an instance of test in your main class.

Test test = new Test();

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

arrayA[0] = "Testing";

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

And call your method.

EDIT:

Tip: There's a coding standard that says never start your method and variable in uppercase.
Also, declaring classes doesn't need ().

Upvotes: 1

greedybuddha
greedybuddha

Reputation: 7507

If we're talking about passing arrays around, why not be neat about it and use the varargs :) You can pass in a single String, multiple String's, or a String[].

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

Upvotes: 0

Related Questions