Results 1 to 9 of 9

Thread: Array Help

  1. #1
    PreetiSweet is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Post Array Help

    For my Java 101 class we have to get the two numbers to 'swap', this is what my professor gave us and he says that we need to fix the array to get the output to swap.

    Right now it just outputs the same thing twice, I can't figure out the new array.
    Help meee, please and thanks.

    Java Code:
    public class cw328swap {
    
        public static void main(String[] args) {
    
        	int [] a = {1,2};
        	System.out.println(a[0] + " g " + a[1]);
        	swap(a[0], a[1]);
        	System.out.println(a[0] + " g " + a[1]);
        }
        public static void swap (int n1, int n2) {
        	int temp=n1;
        	n1=n2;
        	n2=temp;
        }
    }
    Last edited by PreetiSweet; 04-04-2011 at 01:52 AM. Reason: Make the code clear.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    Suggested reading: Does Java pass by reference or pass by value? - JavaWorld...which should address the root of your problem. Fix is to do the swap on the array, not its values.
    Last edited by doWhile; 04-04-2011 at 12:41 AM.

  3. #3
    PreetiSweet is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    Thank you! I'm still having a little trouble figuring it out though.

    I keep getting the errors .class expected or identifier expected.

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

    Default

    When posting code use code tags. Place [ code ] before and [ /code ] after (without the spaces). Also copy and paste the exact error message(s) you get.

  5. #5
    PreetiSweet is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    Okay, I'm just overall confused. I know this is probably really elementary, thanks for the help.

    Java Code:
    public class cw328swap {
    
        public static void main(String[] args) {
    
        	int [] a = {1,2};
        	System.out.println(a[0] + " g " + a[1]);
        	swap(a[], a[]);
        	System.out.println(a[0] + " g " + a[1]);
        }
        public static void swap ( int n1, int n2) {
        	int temp=n1;
        	n1=n2;
        	n2=temp;
        }
    }
    '.class' expected line 9 //swap(a[], a[]);
    '.class' expected line 9

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

    Default

    Java Code:
    swap(a[], a[]);
    This is not legal syntax. Compare with your original code.

  7. #7
    PreetiSweet is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    Java Code:
    swap(a[0], a[1]);
    I was told that a[0] and a[1] are not arrays and that this is why the output does not get swapped, so I tried taking out the numbers.

    I don't know how to change the that to make it an array.

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

    Default

    But the square brackets are used to access a single element inside the array at the specified index. If you want to refer to the entire array then just use the variable name.

  9. #9
    PreetiSweet is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  2. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  3. Convert Char Array to String Array
    By Mayur in forum New To Java
    Replies: 8
    Last Post: 10-12-2009, 11:41 AM
  4. Array length and printing out uninitialized array.
    By nicolek808 in forum New To Java
    Replies: 4
    Last Post: 09-10-2009, 09:12 AM
  5. Replies: 1
    Last Post: 03-31-2009, 06:40 AM

Tags for this Thread

Posting Permissions

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