views:

99

answers:

5

Hello everyone. I want to store the array returned by a method into another array. How can I do this?

 public int[] method()
{

int z[] = {1,2,3,5};
return z;
}

When I call this method, how I can store the array returned (z) into another array.

+1  A: 
int[] x = method();
saugata
Thank you very much, int[] x = method(); worked perfectly, thanks.
Neville
+3  A: 

int[] anotherArray = method();

Do you want to make another physical copy of the array ?

Then use

System.arraycopy(Object src,  int  srcPos, Object dest, int destPos, int length)
Calm Storm
+1  A: 

If you want to duplicate the array, you can use [this API][1]:

http://java.sun.com/javase/6/docs/api/java/util/Arrays.html#copyOf(int[], int)

Laurent K
A: 

Are you sure you have to copy?

int[] myArray = method();  // now myArray can be used 
fastcodejava
wonder if we can use a for loop here?
Maxood
+3  A: 
public int[] method() {
    int z[] = {1,2,3,5};
    return z;
}

The above method does not return an array par se, instead it returns a reference to the array. In the calling function you can collect this return value in another reference like:

int []copy = method();

After this copy will also refer to the same array that z was refering to before.

If this is not what you want and you want to create a copy of the array you can create a copy using System.arraycopy.

codaddict