Results 1 to 6 of 6
  1. #1
    ecc.soccer is offline Member
    Join Date
    Oct 2013
    Posts
    6
    Rep Power
    0

    Question How to swap the first and last array value

    This is what I have so far, but I am not sure how to fix it... please help... Thanks

    public class SwapFandL {

    public static void swap(int [] x){
    int temp = x[0];
    x[0] = x[x.length-1];
    x[x.length-1] = temp;


    }
    public static void main(String[] args){
    int [] x = {1, 3, 5, 7, 2, 9};

    System.out.print(swap(x));

    }
    }

  2. #2
    ecc.soccer is offline Member
    Join Date
    Oct 2013
    Posts
    6
    Rep Power
    0

    Default Re: How to swap the first and last array value

    The output should be : 9 3 5 7 2 1

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: How to swap the first and last array value

    If you get an error then you should copy and paste the full error message and indicate on which line it occurs.

    Your swap method has a void return type, therefore there is nothing for the print statement to display. Also, you cannot print an array that way. You need to iterate over the array and print out each element individually.

  4. #4
    ecc.soccer is offline Member
    Join Date
    Oct 2013
    Posts
    6
    Rep Power
    0

    Default Re: How to swap the first and last array value

    This is how I changed it...
    and the output I got was : [I@3dd4ab05

    public class SwapFandL {

    public static int[] swap(int [] x){
    int temp = x[0];
    x[0] = x[x.length-1];
    x[x.length-1] = temp;
    return x;

    }
    public static void main(String[] args){
    int [] x = {1, 3, 5, 7, 2, 9};

    System.out.print(swap(x));

    }
    }

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: How to swap the first and last array value

    Read my entire post above!

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,924
    Rep Power
    4

    Default Re: How to swap the first and last array value

    Since you are swapping elements of an array (which is a reference) you do not need to return anything. However, to print out the array you need to do what Junky recommended. Another alternative is to the Arrays class. You should find a method in that class that could be of use.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 2
    Last Post: 03-20-2012, 12:01 AM
  2. Swap 2 dimensional Array
    By whit3ang3l in forum New To Java
    Replies: 4
    Last Post: 03-06-2012, 12:53 PM
  3. Swap array value
    By irnie1994 in forum New To Java
    Replies: 1
    Last Post: 11-10-2011, 05:06 PM
  4. how can i loop my array swap
    By belfast09 in forum New To Java
    Replies: 3
    Last Post: 06-15-2011, 04:35 AM
  5. Multidimensional array - swap
    By BeginnerNoob in forum New To Java
    Replies: 13
    Last Post: 03-30-2011, 03:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •