如何在Java中存储方法返回的数组 [英] How to store an array returned by a method in Java

查看:42
本文介绍了如何在Java中存储方法返回的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个方法返回的数组存储到另一个数组中.我怎样才能做到这一点?

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;
}

当我调用这个方法时,如何将返回的数组(z)存入另一个数组?

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

推荐答案

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();

在此之后 copy 也将引用 z 之前引用的相同数组.

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

如果这不是您想要的并且您想要创建数组的副本,您可以使用 System.arraycopy 创建一个副本.

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.

这篇关于如何在Java中存储方法返回的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆