Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java array initialization within argument list

Tags:

java

arrays

How come the first call to someMethod doesn't compile without being explicit that it's String[]?

It's fine to use an array initializer to create a String[] array but you can't use it to pass an argument. Are the curly braces used in some other fashion for passing arguments that derails how I'd expect this to behave?

public void someMethod(String[] arr){
    //do some magic
}

public void makeSomeMagic(){

    String[] arr = {"cat", "fish", "cow"};

    //Does not compile!
    someMethod({"cat", "fish", "cow"});

    //This compiles!
    someMethod(new String[]{"cat", "fish", "cow"});

    //This compiles!
    someMethod(arr);
}

The compiler error is the following:

The method someMethod(String[]) in the type Moo is not applicable for the arguments (String, String, String)

like image 867
Nick Swarr Avatar asked Dec 14 '10 20:12

Nick Swarr


People also ask

How do you initialize all values of an array in Java?

Solution: This example fill (initialize all the elements of the array in one short) an array by using Array. fill(arrayname,value) method and Array. fill(arrayname ,starting index ,ending index ,value) method of Java Util class.

How do you initialize an array in Java while it is declared?

We declare an array in Java as we do other variables, by providing a type and name: int[] myArray; To initialize or instantiate an array as we declare it, meaning we assign values as when we create the array, we can use the following shorthand syntax: int[] myArray = {13, 14, 15};

How do you add an array to an argument 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);

How do you initialize an entire array with value?

int num[5] = {1, 1, 1, 1, 1}; This will initialize the num array with value 1 at all index. The array will be initialized to 0 in case we provide empty initializer list or just specify 0 in the initializer list. Designated Initializer: This initializer is used when we want to initialize a range with the same value.


2 Answers

You can only use the { "hello", "world" } initialization notation when declaring an array variable or in an array creation expression such as new String[] { ... }.

See Section 10.6 Array Initializers in the Java Language Specification:

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

like image 95
aioobe Avatar answered Oct 04 '22 01:10

aioobe


If you don't want to use explicit String[], use:

public void someMethod(String... arr){
    //do some magic
}
…
someMethod("cm", "applicant", "lead");

The three periods after the final parameter's type indicate that the final argument may be passed as an array or as a sequence of arguments.

Read more.

like image 31
MiKo Avatar answered Oct 04 '22 00:10

MiKo