Passing directly an array initializer to a method parameter doesn't work
foo({1,2});
{1, 2} this kind of array initialization only work at the place you are declaring an array.. At other places, you have to create it using new
keyword..
That is why: -
Object[] obj = {1, 2};
Was fine..
This is because, the type of array, is implied by the type of reference
we use on LHS.. But, while we use it somewhere else, Compiler cannot find out the type (Like in your case)..
Try using : -
foo(new Object[]{1,2});
Passing an array initializer as a method argument is not allowed
The Java Language Specification is the final authority on such questions: Chapter 15 Expressions in general, §15.12 Method Invocation Expressions and §15.10.1. Array Creation Expressions and 10.6. Array Initializers in particular.
In a nutshell, what you'll discover is:
- The argument list of a method invocation consists of a series of comma-separated expression;
new int[]{0,0,0}
is an arrayCreationExpression, which, in turn, is a type of expression and thus allowable as an argument in a method invocation;{0,0,0}
is an arrayInitializer which, by itself, is not an expression and thus not legal to use as an argument in a method invocation.
Java. Input array in function without initialising the array
I think you want:
points_of_game(new int[][][]{this.original_map, this.current_map}, this.initial_map);
Or if you change you method signature to:
points_of_game(int[][] initial, int[][]... boards) {
// boards has type int[][][]
}
You can call it like this:
points_of_game(this.initial_map, this.original_map, this.current_map);
How to pass a function an array literal?
{"foo"}
doens't tell Java anything about what type of array you are trying to create...
Instead, try something like...
takesStringArray(new String[] {"foo"});
integer array in a collection
Most of the time, you'll need to do new int[] { 1, 2 }
:
pairs.add( new int[] {1,2} );
The only place that you can avoid the new int[]
is when you're declaring a variable of type int[]
, as you've done with int[] newArray = {1, 2};
. It's just a limitation of the language design. In particular, you can read 10.6. Array Initializers in the specification, which states that:
An array initializer may be specified in a declaration (§8.3, §9.3,
§14.4), or as part of an array creation expression (§15.10), to create
an array and provide some initial values.
The important thing to take away from that is that { 1, 2 }
, is an array initializer, and you can use it in a declaration (int[] newArray = {1, 2};
), or in an array creation expression (new int[] { 1, 2 }
); you can't use it on its own.
array's initialization body as function parameter (C-array), is it possible?
This is about C++11 initializer lists (section 18.9).
void foo (std :: initializer_list <int> inputs) {
for (auto i : inputs) {
// ...
}
}
foo ({10, 20, 30});
Only the compiler can create an initializer list, but you can treat it like a standard STL-style container with begin()
, end()
, size()
, and random-access iterators.
std::vector
(and I expect some other containers) can now be constructed with initializer lists, so
std :: vector <std :: string> foo {"a", "b"};
is equivalent to
std :: vector <std :: string> foo;
foo .push_back ("a");
foo .push_back ("b");
except that it may perform fewer allocations. Note that the const char*
have been turned into std::string
automagically.
Trying to get a Java function to return an array based on a string parameter passed to it
The problem is that you're declaring each String[] assignInitial
inside an if
statements, so the scope of that variable is local to the if
statement. Outside of the if
statements, that variable is not declared.
To fix this, pull the declaration out of the if
scope and into the method scope. You'll have to add new String[]
in front of each array since array constants can only be used in initializers.
public static String[] initial (String responsibleDepartment) {
String[] assignInitial = null;
if (responsibleDepartment.equals("Accounts Receivable")) {
// Note the new String[] below
assignInitial = new String[] {"Mike Davis", "Ben Jones", "Ann Smith"};
}
if (responsibleDepartment.equals("Customer Service")) {
assignInitial = new String[] {"Mary Wexler", "Turd Ferguson"};
}
// Other if statements here
return assignInitial;
Note that there might be other, easier ways to find the values for assignInitial
than this series of if
statements. For example, you could built a switch
statement or a Map<String, String>
.
Related Topics
Sorting Strings That Contains Number in Java
Spring MVC + JSON = 406 Not Acceptable
Using Javamail to Connect to Gmail Smtp Server Ignores Specified Port and Tries to Use 25
What Is the Most Efficient Java Collections Library
Midpoint Between Two Latitude and Longitude
Jtable How to Change Background Color
How to Use Multiple Scanner Objects on System.In
How to Output Two Columns to the Console in Java
How to Compile a .Java with Support for Older Versions of Java
Refreshing Gui by Another Thread in Java (Swing)
How to Draw an Image Over Another Image
How to Correctly Use Custom Renderers to Paint Specific Cells in a Jtable