The syntax is:
AddElement(1, "Numbers1", new int[]{1,2,3})
——
Only when declaring a variable/field can you use the shorthand version, eg
int[] myArray = {1, 2, 3};
Answer from Bohemian on Stack OverflowIn Java, how can I pass an array directly as a parameter to a function? - Stack Overflow
pass array to method Java - Stack Overflow
Passing arrays as arguments for a function
Passing array as parameter.
Could anybody give me some tips on using methods with arrays as parameters in java?
The syntax is:
AddElement(1, "Numbers1", new int[]{1,2,3})
——
Only when declaring a variable/field can you use the shorthand version, eg
int[] myArray = {1, 2, 3};
The standard form of making an array, initialized with numbers of your choosing, is:
new int[] {1,2,3}, e.g:
addElement(5, "Hello", new int[] {1, 2, 3});
You can omit the new int[] part of that, but only if you use this expression as the initial value of a new field or variable declaration, and not when passing to a method call:
int[] example = {1, 2, 3};.
If you make your method argument 'varargs', you can just pass an infinite amount of int parameters, though. You are probably looking for this:
public void addElement(int rowKey, String columnKey, int... values) {
map
.computeIfAbsent(rowKey, r -> new HashMap<>())
.computeIfAbsent(columnKey, c -> new HashMap<>())
.put(values);
}
addElement(5, "Hello", 1, 2, 3);
You may get a warning here in some IDEs, which is trying to say that if someone creates an array, and uses that to call a varargs method, they can then later change the array and this changes the array in your map store:
int[] example = {1, 2, 3};
addElement(5, "Hello", example);
example[0] = 6;
System.out.println(getElement(5, "Hello")[0]);
// prints 6 - that may not be what you want
If you dislike this; make a copy of the array in your addElement method: .put(Arrays.copyOf(values));
Note that this problem applies if you use int[] too; it's just assumed that you know about it if you write int[], so most IDEs don't generate the warning then.
You do this:
private void PassArray() {
String[] arrayw = new String[4]; //populate array
PrintA(arrayw);
}
private void PrintA(String[] a) {
//do whatever with array here
}
Just pass it as any other variable.
In Java, arrays are passed by reference.
Simply remove the brackets from your original code.
PrintA(arryw);
private void PassArray(){
String[] arrayw = new String[4];
//populate array
PrintA(arrayw);
}
private void PrintA(String[] a){
//do whatever with array here
}
That is all.
I have a method with the following signature - public static void max(int tt []). I can properly initialize an array by doing this - int [] a = {4242343,23,423,423,4};. However, java gives me an array if I try passing an array to a method like this - max( {43,43,34 });. Is there a reason for this?
I am confused with this, how will the output be 225 and not 222? as arrays are passed by reference in java, once we point a to b in the test method, the a's reference in main method should also get changed right?
public class Tester
{
static void test(int[] a)
{
int[] b = new int[2];
a = b;
System.out.print(b.length);
System.out.print(a.length);
}
public static void main(String[] args)
{
int[] a = new int[5];
test(a) ;
System.out.print(a.length);
}
}Because the { myObject } syntax is special syntactic sugar which only applies when you're initialising an array variable. This is because on its own the assignment lacks type information; but in the special case of assignment the type is fully inferred from the variable.
In the first example, the compiler knows you're assigning to a (which is an Object[]), so this syntax is allowed. In the latter you aren't initialising a variable (and due to a weakness in Java's type inference, it won't even fully work out the context of the parameter assignment either). So it wouldn't know what type the array should be, even if it could unambiguously determine that that's what you're trying to do (as opposed to e.g. declaring a block).
Calling
someMethod ( new Object[] { myObject } )
would work if you want to define the array in-place without using a variable.
While the above answers your question as asked, I notice that the method you're calling is varargs rather than explicitly requiring an array paramter. So in this case you could simply call
someMethod(myObject);
someMethod(new Object[] { "" });
Should do the trick!