Text2-综合练习1

Text2-综合练习

需求:定义一个方法copyArray(int arr[],int from,int to)

功能:将数组arr中索引from(包含from)开始。

刀索引to结束(不包含to)的元素复制到新数组中,

将新数组返回

 public static void main(String[] args) {
       
        int arr[]={1,2,3,4,5,6,7,8,9};
        int copyAfter[]=copyArray(arr,3,7);
        for (int i = 0; i < copyAfter.length; i++) {
            System.out.print(copyAfter[i]+" ");
        }
    }
    public static int[] copyArray(int arr[],int from,int to){
        int index=0;
        int newArr[]=new int[to-from];
        for (int i = from; i <to; i++) {
            newArr[index]=arr[i];
            index++;
        }

        return newArr;
    }

代码运行结果如下图所示:

posted @   chi_yu  阅读(1)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
点击右上角即可分享
微信分享提示