java数组训练题(用方法复制数组并且打印)

package com.itheima;

public class arraytrain {
    public static void main(String[] args){
        int[] arr = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
//        Scanner d = new Scanner(System.in);
//        int number = d.nextInt();
//        System.out.println(havenumber(arr,number));
        printarr(copyarray(arr,0,4));

    }

    public static boolean havenumber(int[] arr,int number){

        for (int i = 0; i < arr.length; i++) {
            if(arr[i] == number){
               return true;
            }
        }
        return false;
    }

    //复制数组
    //返回数组的方法表示:public static int[] 方法名
    public static int[] copyarray(int[] arr,int index_start,int index_end){
        int length = index_end - index_start;
        int[] savearr = new int[length];
        int new_index = 0;
        for (int i = index_start; i < index_end; i++) {
            savearr[new_index++] = arr[i];

        }  //不就是相当于getchars那种方法嘛
        return savearr;
    }

    public static void printarr(int[] arr){
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}

你可能感兴趣的:(java基础算法训练,java,算法,开发语言)