java数组传参问题

AC_YE 2021-01-29 05:15:39
int【】a = {1,3,2}; int【】b= new int [10]; 调用System.arrayCopy(a,0,b,0,a.length); 这里的传参为什么直接把数组名a传过去了,这个arrayCopy里面的参数是Object src吗,int【】a可以直接传a过去的吗,不应该是Object【】src这样写才可以传参的吗Object src这样是代表一个类吧,Object【】src才是数组吧
...全文
334 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 4 楼 立青_ 的回复:
你这个是Android的,在 AndroidSdk >=21 时重载了,java只有这一个本地方法
public static native void arraycopy(Object src, int srcPos,Object dst, int dstPos, int length);
嗯,android的话,是在底层用C++实现的,调用so接口。jni传参用object而已。
立青_ 2021-02-04
  • 打赏
  • 举报
回复
引用 3 楼 不会写代码的猴子 的回复:

private static void arraycopy(int[] src, int srcPos, int[] dst, int dstPos, int length) {
    if (src == null) {
        throw new NullPointerException("src == null");
    }
    if (dst == null) {
        throw new NullPointerException("dst == null");
    }
    if (srcPos < 0 || dstPos < 0 || length < 0 ||
        srcPos > src.length - length || dstPos > dst.length - length) {
        throw new ArrayIndexOutOfBoundsException(
            "src.length=" + src.length + " srcPos=" + srcPos +
            " dst.length=" + dst.length + " dstPos=" + dstPos + " length=" + length);
    }
    if (length <= ARRAYCOPY_SHORT_INT_ARRAY_THRESHOLD) {
        // Copy int by int for shorter arrays.
        if (src == dst && srcPos < dstPos && dstPos < srcPos + length) {
            // Copy backward (to avoid overwriting elements before
            // they are copied in case of an overlap on the same
            // array.)
            for (int i = length - 1; i >= 0; --i) {
                dst[dstPos + i] = src[srcPos + i];
            }
        } else {
            // Copy forward.
            for (int i = 0; i < length; ++i) {
                dst[dstPos + i] = src[srcPos + i];
            }
        }
    } else {
        // Call the native version for longer arrays.
        arraycopyIntUnchecked(src, srcPos, dst, dstPos, length);
    }
你这个是Android的,在 AndroidSdk >=21 时重载了,java只有这一个本地方法
public static native void arraycopy(Object src, int srcPos,Object dst, int dstPos, int length);
  • 打赏
  • 举报
回复

private static void arraycopy(int[] src, int srcPos, int[] dst, int dstPos, int length) {
    if (src == null) {
        throw new NullPointerException("src == null");
    }
    if (dst == null) {
        throw new NullPointerException("dst == null");
    }
    if (srcPos < 0 || dstPos < 0 || length < 0 ||
        srcPos > src.length - length || dstPos > dst.length - length) {
        throw new ArrayIndexOutOfBoundsException(
            "src.length=" + src.length + " srcPos=" + srcPos +
            " dst.length=" + dst.length + " dstPos=" + dstPos + " length=" + length);
    }
    if (length <= ARRAYCOPY_SHORT_INT_ARRAY_THRESHOLD) {
        // Copy int by int for shorter arrays.
        if (src == dst && srcPos < dstPos && dstPos < srcPos + length) {
            // Copy backward (to avoid overwriting elements before
            // they are copied in case of an overlap on the same
            // array.)
            for (int i = length - 1; i >= 0; --i) {
                dst[dstPos + i] = src[srcPos + i];
            }
        } else {
            // Copy forward.
            for (int i = 0; i < length; ++i) {
                dst[dstPos + i] = src[srcPos + i];
            }
        }
    } else {
        // Call the native version for longer arrays.
        arraycopyIntUnchecked(src, srcPos, dst, dstPos, length);
    }
qybao 2021-01-29
  • 打赏
  • 举报
回复
arraycopy的参数是Object,不是Object[],你好好看看文档 https://docs.oracle.com/javase/8/docs/api/java/lang/System.html int[]也是Object,所以当然能用了
立青_ 2021-01-29
  • 打赏
  • 举报
回复
万物皆对象,数组也是对象

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧