是否可以返回本地初始化的数组,如果没有我怎么办?

m4d*_*ign 3 java arrays

举个例子:

public class foo
{
    public int[] func()
    {
        int arr[] = new int[3];
        // here initialised the array

        return arr;
    }
}
Run Code Online (Sandbox Code Playgroud)

我对Java没有经验,但我知道一些C/C++.

Moh*_*sen 6

是的,它完全没问题,因为垃圾收集是在没有对象的进一步引用时完成的.您还可以使用lengthinside(arr.length)或outside(例如aFoo.func().length)方法来查找数组大小.