jeudi 3 avril 2014

Returning an Array of Integers


Vote count:

0




I am trying to make a method in which it will take an array of integers as a parameter, creates a new array that is the same size, copies the elements from the first array into the new array, and then returns a reference to the new array.


Right now, my code is as follows:


class CopyingAnArray {



int[] cloneArray(int[] arr) {
size=newarray.length;
int[] newarray=new int[size];
int[] copyarray=newarray;
return copyarray;
}

public static void main(String argv[]) {
ArrayTester converter=new ArrayTester();
int[] newarray={2,5,6,7};
System.out.println(converter.cloneArray(newarray));
}
}


Here is an explanation of what I think I am doing. I am taking the size of an array and putting it an array:



size=newarray.length;
int[] newarray=new int[size];


Then I am copying the array into a new array named copyarray. Then, I return copyarray.



int[] newarray=new int[size];
int[] copyarray=newarray;
return copyarray;


Any suggestions or advice on what I did wrong/solve the code?



asked 1 min ago






Aucun commentaire:

Enregistrer un commentaire