Java method with array argument

pass array to method Java

Just pass it as any other variable.
In Java, arrays are passed by reference.

so. If I change the array (passed in as parameter) in a method, do I change the values in the original array in caller?

@Remian8985 If the array is declared globally or if you’re accessing it through its class (e.g, someClass.array), then yes.

yeah arrays are implemented as objects in Java and hence its only just necessary to pass a reference of the array to the method you want to invoke.

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 >

Heh, I think we’ve all done that before. What I’d like to know is why we still have to initialize our objects explicitly. Is there any situation where we WANT a typed null pointer? Even if so, it’s definitely an exception rather than the norm.

An array variable is simply a pointer, so you just pass it like so:

A little more elaboration. If what you want to do is create a COPY of an array, you’ll have to pass the array into the method and then manually create a copy there (not sure if Java has something like Array.CopyOf() ). Otherwise, you’ll be passing around a REFERENCE of the array, so if you change any values of the elements in it, it will be changed for other methods as well.

Читайте также:  Python csv reader bytes

There is an important point of arrays that is often not taught or missed in java classes. When arrays are passed to a function, then another pointer is created to the same array ( the same pointer is never passed ). You can manipulate the array using both the pointers, but once you assign the second pointer to a new array in the called method and return back by void to calling function, then the original pointer still remains unchanged.

You can directly run the code here : https://www.compilejava.net/

import java.util.Arrays; public class HelloWorld < public static void main(String[] args) < int Main_Array[] = ; Demo1.Demo1(Main_Array); // THE POINTER Main_Array IS NOT PASSED TO Demo1 // A DIFFERENT POINTER TO THE SAME LOCATION OF Main_Array IS PASSED TO Demo1 System.out.println("Main_Array = "+Arrays.toString(Main_Array)); // outputs : Main_Array = [20, 19, 18, 4, 16, 15, 14, 4, 12, 11, 9] // Since Main_Array points to the original location, // I cannot access the results of Demo1 , Demo2 when they are void. // I can use array clone method in Demo1 to get the required result, // but it would be faster if Demo1 returned the result to main > > public class Demo1 < public static void Demo1(int A[]) < int B[] = new int[A.length]; System.out.println("B = "+Arrays.toString(B)); // output : B = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] Demo2.Demo2(A,B); System.out.println("B = "+Arrays.toString(B)); // output : B = [9999, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] System.out.println("A = "+Arrays.toString(A)); // output : A = [20, 19, 18, 4, 16, 15, 14, 4, 12, 11, 9] A = B; // A was pointing to location of Main_Array, now it points to location of B // Main_Array pointer still keeps pointing to the original location in void main System.out.println("A mt24">
)" data-controller="se-share-sheet" data-se-share-sheet-title="Share a link to this answer" data-se-share-sheet-subtitle="" data-se-share-sheet-post-type="answer" data-se-share-sheet-social="facebook twitter devto" data-se-share-sheet-location="2" data-se-share-sheet-license-url="https%3a%2f%2fcreativecommons.org%2flicenses%2fby-sa%2f3.0%2f" data-se-share-sheet-license-name="CC BY-SA 3.0" data-s-popover-placement="bottom-start">Share
)" title="">Improve this answer
)">edited Feb 16, 2017 at 8:04
answered Feb 16, 2017 at 7:57
Add a comment|
5

Important Points

  • you have to use java.util package
  • array can be passed by reference

In the method calling statement

  • Don't use any object to pass an array
  • only the array's name is used, don't use datatype or array brackets []

Sample Program

import java.util.*; class atg < void a() < int b[]=; c(b); > void c(int b[]) < int e=b.length; for(int f=0;f> public static void main(String args[]) < atg ob=new atg(); ob.a(); >>

Источник

Can I pass an array as arguments to a method with variable arguments in Java?

The problem here is that args is treated as Object[] in the method myFormat , and thus is a single argument to String.format , while I'd like every single Object in args to be passed as a new argument. Since String.format is also a method with variable arguments, this should be possible. If this is not possible, is there a method like String.format(String format, Object[] args) ? In that case I could prepend extraVar to args using a new array and pass it to that method.

I can't help but wonder why this question is a "is this valid" kind of question. Couldn't you have just tried it? Don't overdo asking, you'll do yourself more harm than good.

true enough, this could have been easily tested. however, the nice thing about a question like this is that it exposes the topic and solicits interesting answers.

I actually tried the code above, with the intention to pass the array args as arguments to the method. However, I didn't realize that I should've prepended extraVar to args first. When you know that variable arguments are treated as an array (even outside the method), this is of course quite logical.

6 Answers 6

Yes, a T. is only a syntactic sugar for a T[] .

JLS 8.4.1 Format parameters

The last formal parameter in a list is special; it may be a variable arity parameter, indicated by an elipsis following the type.

If the last formal parameter is a variable arity parameter of type T , it is considered to define a formal parameter of type T[] . The method is then a variable arity method. Otherwise, it is a fixed arity method. Invocations of a variable arity method may contain more actual argument expressions than formal parameters. All the actual argument expressions that do not correspond to the formal parameters preceding the variable arity parameter will be evaluated and the results stored into an array that will be passed to the method invocation.

Here's an example to illustrate:

public static String ezFormat(Object. args) < String format = new String(new char[args.length]) .replace("\0", "[ %s ]"); return String.format(format, args); >public static void main(String. args) < System.out.println(ezFormat("A", "B", "C")); // prints "[ A ][ B ][ C ]" >

And yes, the above main method is valid, because again, String. is just String[] . Also, because arrays are covariant, a String[] is an Object[] , so you can also call ezFormat(args) either way.

See also

Varargs gotchas #1: passing null

How varargs are resolved is quite complicated, and sometimes it does things that may surprise you.

static void count(Object. objs) < System.out.println(objs.length); >count(null, null, null); // prints "3" count(null, null); // prints "2" count(null); // throws java.lang.NullPointerException. 

Due to how varargs are resolved, the last statement invokes with objs = null , which of course would cause NullPointerException with objs.length . If you want to give one null argument to a varargs parameter, you can do either of the following:

count(new Object[] < null >); // prints "1" count((Object) null); // prints "1" 

The following is a sample of some of the questions people have asked when dealing with varargs:

Vararg gotchas #2: adding extra arguments

As you've found out, the following doesn't "work":

 String[] myArgs = < "A", "B", "C" >; System.out.println(ezFormat(myArgs, "Z")); // prints "[ [Ljava.lang.String;@13c5982 ][ Z ]" 

Because of the way varargs work, ezFormat actually gets 2 arguments, the first being a String[] , the second being a String . If you're passing an array to varargs, and you want its elements to be recognized as individual arguments, and you also need to add an extra argument, then you have no choice but to create another array that accommodates the extra element.

Here are some useful helper methods:

static T[] append(T[] arr, T lastElement) < final int N = arr.length; arr = java.util.Arrays.copyOf(arr, N+1); arr[N] = lastElement; return arr; >static T[] prepend(T[] arr, T firstElement)

Now you can do the following:

 String[] myArgs = < "A", "B", "C" >; System.out.println(ezFormat(append(myArgs, "Z"))); // prints "[ A ][ B ][ C ][ Z ]" System.out.println(ezFormat(prepend(myArgs, "Z"))); // prints "[ Z ][ A ][ B ][ C ]" 

Varargs gotchas #3: passing an array of primitives

 int[] myNumbers = < 1, 2, 3 >; System.out.println(ezFormat(myNumbers)); // prints "[ [I@13c5982 ]" 

Varargs only works with reference types. Autoboxing does not apply to array of primitives. The following works:

 Integer[] myNumbers = < 1, 2, 3 >; System.out.println(ezFormat(myNumbers)); // prints "[ 1 ][ 2 ][ 3 ]" 

Источник

How does the Java array argument declaration syntax ". " work?

Note the "dot dot dot" in the array declaration, rather than the usual bracket []. Clearly it works. In fact I wrote a small test and verified it works. So, I pulled the java grammar to see where this syntax of argument declaration is, but did not find anything. So to the experts out there, how does this work? Is it part of the grammar? Also, while I can declare function like this, I can't declare an array within a function's body like this. Anyway, do you know of any place that has this documented. It is curiosity, and perhaps not worth of any time invested in it, but I was stumped.

10 Answers 10

I believe this was implemented in Java 1.5. The syntax allows you to call a method with a comma separated list of arguments instead of an array.

public static void main(String. args); main("this", "is", "multiple", "strings"); 
public static void main(String[] args); main(new String[] ); 

If the last formal parameter is a variable arity parameter of type T, it is considered to define a formal parameter of type T[]. The method is then a variable arity method. Otherwise, it is a fixed arity method. Invocations of a variable arity method may contain more actual argument expressions than formal parameters. All the actual argument expressions that do not correspond to the formal parameters preceding the variable arity parameter will be evaluated and the results stored into an array that will be passed to the method invocation (§15.12.4.2).

Basically, the last parameter of any method call can have T. . If it has that, it is converted to T[] .

So basically, what you have is a fancy way of reproducing the more traditional

In simple term its an Array of Member like

public setMembers(Member[] members); 

When to use:

Generally while designing API it is good to use when number of argument is not fixed.

Источник

How to call a "vararg" method with an array of params?

. assuming you want the args to be the multiple values. If you need extra wrapping, you have to do that yourself:

String text = String.format(formatString, new Object[] < args >); 

Basically, if the argument type already matches the parameter array type, the compiler doesn't do any wrapping.

If m is being invoked with kn actual argument expressions, or, if m is being invoked with k=n actual argument expressions and the type of the kth argument expression is not assignment compatible with T[], then the argument list (e1, . , en-1, en, . ek) is evaluated as if it were written as (e1, . en-1, new T[]).

Note the "If" at the start - that's what effectively says that if the argument is already okay, no wrapping is performed.

in short by example: (S2 and I2 are likely the most interesting versions passing the varargs as an Object[] )

PrintStream ps = System.out ; String[] sarr = new String[] < "a" , "b" >; Object os = sarr ; int[] iarr = new int[] < 1 , 2 >; // cannot be cast to Object[] - see I6 below Integer[] ioarr = new Integer[] < 1 , 2 >; // can be cast to Object[] - see I2 below Object oi = iarr ; // cannot be cast to Object[] - see I6 below ps.printf( "%n## Strings:%n%n" ) ; ps.printf( "S1: pass as single objects: %s, %s%n" , "a" , "b" ) ; ps.printf( "S2: pass as varargs objects: %s, %s%n" , (Object[]) sarr ) ; ps.printf( "S3: pass as single objects: %s, %s%n" , sarr , "c" ) ; ps.printf( "S4: pass as single objects: %s, %s%n" , os , "c" ) ; // ps.printf( "S5: MissingFormatArgumentException: %s, %s%n" , os ) ; // ps.printf( "S6: ClassCastException: %s, %s%n" , (Object[]) os ) ; ps.printf( "%n## int:%n%n" ) ; ps.printf( "I1: pass as single objects: %s, %s%n" , 1 , 2 ) ; ps.printf( "I2: pass as varargs objects: %s, %s%n" , (Object[]) ioarr ) ; ps.printf( "I3: pass as single objects: %s, %s%n" , iarr , 3 ) ; ps.printf( "I4: pass as single objects: %s, %s%n" , oi , 3 ) ; // ps.printf( "I5: MissingFormatArgumentException %s, %s%n" , iarr ) ; // ps.printf( "I6: ClassCastException: %s, %s%n" , (Object[]) oi ) ; 
## Strings: S1: pass as single objects: a, b S2: pass as varargs objects: a, b S3: pass as single objects: [Ljava.lang.String;@ad02649, c S4: pass as single objects: [Ljava.lang.String;@ad02649, c ## int: I1: pass as single objects: 1, 2 I2: pass as varargs objects: 1, 2 I3: pass as single objects: [I@6781882a, 3 I4: pass as single objects: [I@6781882a, 3 

Источник

Оцените статью