method

create copyArray method in different way (method overloading)

1.take 2 parameters , first one is a int array,represent the original array that you want to copy, second one is int parameter,represent the length for new array

return int array

public static int[] copyArray(int[] originalArr , int newLength)

this method will copy originalArr from first element into new array, and copy newLength of them

sample 1:

the originalArr is {11,22,33,44,55,66}

newLength is: 2

the new array you need to return is {11,22}

sample 2:

the originalArr is {11,22,33,44,55,66}

newLength is: 8

the new array you need to return is {11,22,33,44,55,66,0,0}

 

2.take 3 parameters , first one is a int array,represent the original array that you want to copy, second one is int parameter,represent the length for new array, second one is int parameter, represent copy from which position

return int array

public static int[] copyArray(int[] originalArr , int newLength, int fromWhich)

this method will copy originalArr from the fromWhich element into new array, and copy newLength of them

sample 1:

the originalArr is {11,22,33,44,55,66}

newLength is: 2

fromWhich is:3

the new array you need to return is {44,55}

sample 2:

the originalArr is {11,22,33,44,55,66}

newLength is: 8

fromWhich is:2

the new array you need to return is {33,44,55,66,0,0,0,0}

posted @ 2024-03-27 17:09  派若多克斯  阅读(58)  评论(0)    收藏  举报