黑马程序员技术交流社区

标题: 这咋做 [打印本页]

作者: MMM521    时间: 2017-4-2 22:45
标题: 这咋做
5.定义一个方法copyOfRange(int[] arr,int from, int to),功能:将数组arr中从索引from(包含from)开始到索引to结束(不包含to)的元素复制到新数组中
  ,并将新数组返回

作者: Hp_Yx    时间: 2017-4-2 23:02
估计明天才学到,现在看hashmap

作者: JINHUAN    时间: 2017-4-2 23:21
// 方法一:使用API中System类的数组复制方法
        public static int[] copyOfRange(int[] arr, int from, int to) {
                int[] newArr = new int[to - from];
                System.arraycopy(arr, from, newArr, 0, to - from);
                return newArr;
        }

        // 方法二:for循环遍历源数组复制指定索引的元素给新数组
        public static int[] copyOfRange1(int[] arr, int from, int to) {
                int[] newArr = new int[to - from];
                for (int i = from, j = 0; i < to; i++, j++) {
                        newArr[j] = arr[i];
                }
                return newArr;
        }





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2