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

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

问题描述

我想通过存储方法返回到另一个数组的数组。我怎样才能做到这一点?

 公众诠释[]()的方法
{INT Z [] = {1,2,3,5};
返回Z者除外;
}

当我把这种方法,我怎么能存储返回数组(Z)到另一个阵列。


解决方案

 公众诠释[]的方法(){
    INT Z [] = {1,2,3,5};
    返回Z者除外;
}

上述方法不返回一个数组参数本身,而不是将其返回给数组的引用。在调用函数就可以像其他引用收集这些返回值:

  INT [] =复制法();

复制后也将引用同一个数组以Z 是指的是前。

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

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

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.

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

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