How to create a sub array from another array in Java?

前端 未结 9 494
醉话见心
醉话见心 2020-11-29 16:02

How to create a sub-array from another array? Is there a method that takes the indexes from the first array such as:

methodName(object array, int start, int          


        
9条回答
  •  無奈伤痛
    2020-11-29 16:32

    Yes, it's called System.arraycopy(Object, int, Object, int, int) .

    It's still going to perform a loop somewhere though, unless this can get optimized into something like REP STOSW by the JIT (in which case the loop is inside the CPU).

    int[] src = new int[] {1, 2, 3, 4, 5};
    int[] dst = new int[3];
    
    System.arraycopy(src, 1, dst, 0, 3); // Copies 2, 3, 4 into dst
    

提交回复
热议问题