How can I pass an entire array to a method?

private void PassArray() { String[] arrayw = new String[4]; //populate array PrintA(arrayw[]); } private void PrintA(String[] a) { //do whatever with array here } 

How do I do this correctly?

10 Answers

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.

3

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.

1

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

PrintA(arrayw); 

Edit:

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.

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[]={1,2,3,4,5,6,7}; c(b); } void c(int b[]) { int e=b.length; for(int f=0;f<e;f++) { System.out.print(b[f]+" ");//Single Space } } public static void main(String args[]) { atg ob=new atg(); ob.a(); } } 

Output Sample Program

1 2 3 4 5 6 7

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 :

import java.util.Arrays; public class HelloWorld { public static void main(String[] args) { int Main_Array[] = {20,19,18,4,16,15,14,4,12,11,9}; 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 = "+Arrays.toString(A)); // output : A = [9999, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] // Hence to access this result from main, I have to return it to main } } public class Demo2 { public static void Demo2(int AAA[],int BBB[]) { BBB[0] = 9999; // BBB points to the same location as B in Demo1, so whatever I do // with BBB, I am manipulating the location. Since B points to the // same location, I can access the results from B } } 

You got a syntax wrong. Just pass in array's name. BTW - it's good idea to read some common formatting stuff too, for example in Java methods should start with lowercase letter (it's not an error it's convention)

0

If you are too lazy ;) to declare variable.

import static org.junit.Assert.assertEquals public class BinarySearch { public static void main(String[] args) { search(new int[] {-1,0,3,5,9,12}, 9); //creating and passing array } public static int search(int[] nums, int target) { //write some code return -1; } } 

This is very useful for unit test like below

assertEquals(4,search(new int[] {-1,0,3,5,9,12}, 9)); assertEquals(-1,search(new int[]{-1,0,3,5,9,12}, 2)); 
class test { void passArr() { int arr1[]={1,2,3,4,5,6,7,8,9}; printArr(arr1); } void printArr(int[] arr2) { for(int i=0;i<arr2.length;i++) { System.out.println(arr2[i]+" "); } } public static void main(String[] args) { test ob=new test(); ob.passArr(); } } 
public static void main(String[] args) { int[] A=new int[size]; //code for take input in array int[] C=sorting(A); //pass array via method //and then print array } public static int[] sorting(int[] a) { //code for work with array return a; //retuen array } 

In this way we can pass an array to a function, here this print function will print the contents of the array.

public class PassArrayToFunc { public static void print(char [] arr) { for(int i = 0 ; i<arr.length;i++) { System.out.println(arr[i]); } } public static void main(String[] args) { Scanner scan = new Scanner(System.in); char [] array = scan.next().toCharArray(); print(array); scan.close(); } } 
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy