I know arrays are easy and everything.. but something which I think should be perfectly logical isn't working in my program.
I'm trying to make a method to reverse the order of my array, so that, for example
1 2 3 4 5
will become
5 4 3 2 1
I use something basic:
int left=array[0];
int right=array.length;
while(left<right){
int temp=array[left];
array[left]=array[right];
array[right]=temp;
left++;
right--;
But array.length isn't number at the end of the array, it's the size of the array! If I have:
int num[] = new int[10] where num[10]=23, then num.length will return 10, not 23.
So I've tried using something like num[num.length], but I keep getting an error message about something being out of bounds. What am I doing wrong?
In b4 pointless java flames.
Name:
Anonymous2007-03-07 13:28 ID:5A+0zV0H
array.length - 1 you idiot
Name:
Anonymous2007-03-07 13:37 ID:dVhKGr1O
>>2
I've already tried using that but it still gave me bounding errors. I'll do it again, but I really don't know why it's not working. I get the logic but it's like.. I dunno.
int left=array[0];
Tell me, what do you think this statement does?
Name:
Anonymous2007-03-07 15:29 ID:4DVER/ly
>>6
invoke the composition of the int, left, =, and array functions on the list [0]. I suspect it evaluates into _|_ though :/
Name:
Anonymous2007-03-07 15:50 ID://3lYfL/
>>4
I know that, dickhead. And if I do a System.out.println(array[array.length-1]); it gives me the number I want. But for some reason I can't assign it to another variable.
Name:
Anonymous2007-03-07 15:51 ID:qKeOGIiz
try that in Haskell: reverse [1, 2, 3]
or this in Erlang: lists:reverse([1, 2, 3]).
>>11
Ok, ok, now show us a pythoic solution that doesn't look like ass.
Name:
Anonymous2007-03-07 19:07 ID:qKeOGIiz
>>11
lol Python tries very hard to be functional like the real functional languages but fails hard. Even C++ is more useful than Python with the STL and Boost :D
Name:
Anonymous2007-03-08 2:36 ID:5rVKwRVE
>>8 And if I do a System.out.println(array[array.length-1]); it gives me the number I want. But for some reason I can't assign it to another variable.
Java: the only C-like language whose users do not understand the concept of zero-indexed arrays.
>>13
Refer to >>11. The first and third solutions in particular are pretty nice. Oh and >>16 owned me, forgot about that.
>>15 Java: the only C-like language whose users do not understand the concept of zero-indexed arrays.
Programmers of a language are of the same quality of the language. Java gets Java-quality programmers. Go figure.
Only a lousy programmer blames his tools.
Ever used bash, Visual Basic, ...?
>>16
I have been owned, I had forgotten about step in slices. That's much better, and BTW >>12-13, bow to >>16.
>>23
WOAH?.. I didnt know you could do stuff like that in lisp... cool
I might actually bother learning it someday
Name:
Anonymous2007-03-10 0:03 ID:QV7888IK
If the OP is still here:
You need to count along the indices of the array, not its contents.
int right = array.length - 1;
is correct, because the last element is at index 4. The first element is at index 0, so you need this:
int left = 0;
With left = array[0], left starts out as 1 with the array you gave, so the first element is never swapped in this case. As well,
temp = array[left];
becomes
temp = array[array[0]];
the first time through the loop, and this definitely doesn't look right.
Name:
Anonymous2007-03-10 2:11 ID:3OWcXcQm
why don't you just write a for loop starting from the last cell of the array going to the first cell
ie for(int i = i.length; i > -1; i++)
Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy