Results 1 to 9 of 9
Thread: Array Help
- 04-04-2011, 12:30 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
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.
- 04-04-2011, 12:38 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
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.
- 04-04-2011, 01:07 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
Thank you! I'm still having a little trouble figuring it out though.
I keep getting the errors .class expected or identifier expected.
- 04-04-2011, 01:37 AM #4
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.
- 04-04-2011, 01:43 AM #5
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
Okay, I'm just overall confused. I know this is probably really elementary, thanks for the help.
'.class' expected line 9 //swap(a[], a[]);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
- 04-04-2011, 01:48 AM #6
This is not legal syntax. Compare with your original code.Java Code:swap(a[], a[]);
- 04-04-2011, 01:52 AM #7
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
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.Java Code:swap(a[0], a[1]);
I don't know how to change the that to make it an array.
- 04-04-2011, 01:56 AM #8
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.
- 04-04-2011, 02:01 AM #9
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Variable of an object in an array compared to an element of another array?
By asmodean in forum New To JavaReplies: 23Last Post: 09-07-2010, 08:12 PM -
Trying to make an array list // inserting an element to middle of array
By javanew in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:03 AM -
Convert Char Array to String Array
By Mayur in forum New To JavaReplies: 8Last Post: 10-12-2009, 11:41 AM -
Array length and printing out uninitialized array.
By nicolek808 in forum New To JavaReplies: 4Last Post: 09-10-2009, 09:12 AM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks