Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: where is the error in this method !

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Exclamation where is the error in this method !

    hi gays ,

    i trying to creat method to copy array ,
    PHP Code:
      public static void arraycopy(Object src,  int  srcPos,Object destint destPosint 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"+???); 


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,195
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default 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);

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: where is the error in this method !

    yes its in the same calss ..
    ok , thx man

    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 destint destPosint 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 ?
         


  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,195
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: where is the error in this method !

    i found problem
    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.

Similar Threads

  1. [SOLVED] Another Method,
    By Time in forum Algorithms & Recursion
    Replies: 4
    Last Post: June 28th, 2010, 09:52 PM
  2. Looking for a method,
    By Time in forum Algorithms & Recursion
    Replies: 7
    Last Post: May 15th, 2010, 12:24 PM