where is the error in this method !
hi gays ,:cool:
i trying to creat method to copy array ,
PHP Code:
public static void arraycopy(Object src, int srcPos,Object dest, int destPos, int length)
{
Object[] srcArray=(Object[]) src;
Object[] destArray=(Object[]) dest;
for(int i=srcPos;i<srcPos+length;i++)
destArray[i]=srcArray[i];
}
Now how i can call this method from main ? :-?
eg :
PHP Code:
System.out.println("this is array to copy"+???+"and this is new array"+???);
Re: where is the error in this method !
Are the two methods in the same class?
If they are then you can call it like any other method: arraycopy(the args here);
If they are in different classes, then put the classname first followed by method name:
TheClassName.arraycopy(the args);
Re: where is the error in this method !
yes its in the same calss ..
ok , thx man :D
Now : the Method should be void or return ?
i try to make it return but i found problem :-
see it
PHP Code:
public static Object arraycopy(Object src, int srcPos,Object dest, int destPos, int length)
{
Object[] srcArray=(Object[]) src;
Object[] destArray=(Object[]) dest;
for(int i=srcPos;i<srcPos+length;i++){
destArray[i]=srcArray[i];
}
return destArray[i]; // can it return an object ????? And cant see srcArray[i] cuz it out of for loop ! what is the solution?? i remember that we can use tmp ? mmm can u make it ?
}
Re: where is the error in this method !
Have you tried compiling and executing the code? What happens?
Copy and paste the full text of the error message here.
Your return statement is returning only one element of the array and not the whole array.