Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing arrays as method parameters in Java

Tags:

java

The following code uses simple arrays of String in Java.

package javaarray;

final public class Main
{
    public void someMethod(String[] str)
    {
        System.out.println(str[0]+"\t"+str[1]);
    }
    public static void main(String[] args)
    {
        String[] str1 = new String[] {"day", "night"};
        String[] str2 = {"black", "white"};

        //Both of the above statements are valid.

        Main main=new Main();
        main.someMethod(str1);
        main.someMethod(str2);

        //We can invoke the method someMethod by supplying both of the above arrays alternatively.

        main.someMethod(new String[] { "day", "night" }); //This is also valid as obvious.
        main.someMethod({ "black", "white" }); //This is however wrong. The compiler complains "Illegal start of expression not a statement" Why?
    }
}

In the above code snippet, we can initialize arrays like this.

String[] str1 = new String[] {"day", "night"};
String[] str2 = {"black", "white"};

and we can directly pass it to a method without being assigned like this.

main.someMethod(new String[] { "day", "night" });

If it is so, then the following statement should also be valid.

main.someMethod({ "black", "white" });

but the compiler complains "Illegal start of expression not a statement" Why?

like image 355
Lion Avatar asked Apr 12 '12 03:04

Lion


People also ask

Can arrays be passed as parameters to methods?

Arrays can be passed as arguments to method parameters. Because arrays are reference types, the method can change the value of the elements.

How do you pass an array argument to a method in Java?

Passing Array To The Method In Java To pass an array as an argument to a method, you just have to pass the name of the array without square brackets. The method prototype should match to accept the argument of the array type. Given below is the method prototype: void method_name (int [] array);

Can an entire array be passed as a parameter Java?

You can pass an entire array, or a single element from an array, to a method. Notice that the int [ ] indicates an array parameter. Notice that passing a single array element is similar to passing any single value.

What is passing array as parameters?

To pass an array as a parameter to a function, pass it as a pointer (since it is a pointer). For example, the following procedure sets the first n cells of array A to 0. void zero(int* A, int n) { for(int k = 0; k < n; k++) { A[k] = 0; } } Now to use that procedure: int B[100]; zero(B, 100);


1 Answers

According to Java Language Specification (10.6. Array Initializers)

An array initializer may be specified in a declaration, or as part of an array creation expression (§15.10), creating an array and providing some initial values:

So, there are only two ways you can use an array initializer ({"foo", "bar"}):

  1. Variable declaration: String[] foo = {"foo", "bar"};
  2. Array creation expression: new String[] {"foo", "bar"};

You can't use an array initializer as a method parameter.

15.10. Array Creation Expressions

ArrayCreationExpression:
    new PrimitiveType DimExprs Dimsopt
    new ClassOrInterfaceType DimExprs Dimsopt
    new PrimitiveType Dims ArrayInitializer 
    new ClassOrInterfaceType Dims ArrayInitializer
like image 51
default locale Avatar answered Sep 25 '22 11:09

default locale