This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "array" Watch "array" New topic
Author

array

bharath pawan
Greenhorn

Joined: Jan 25, 2007
Posts: 11
how to reverse an array with out using indexing
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Please explain what you mean by "with out using indexing".


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37897
    
  22
Somebody is having you on, surely?

How are you expected to gain access to the elements of an array other than by using their index?
If you have two indices to an array, let's call them i and j, it is very easy to swap the two, with a method like this.. . . and once you have got that working it is easy to use that method to reverse the whole array. But I can't think how you are supposed to do it without using the index.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

You could do this in C(++) because of pointer arithmetic, but in Java that doesn't exist.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Suggested framework:

1. Copy the array to a List (using Arrays.asList)

2. Create a Stack from that List.

3. Create a new empty Stack.

4. Pop entries from the first Stack and push them into the second Stack.

5. Make the second Stack into an array (using List.toArray).
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
If we're already converting the array into some sort of Java Collection, maybe java.util.Collections provides some useful methods ...
 
wood burning stoves
 
subject: array
 
Similar Threads
How could I print names randomly?
Doubt
about array
WA #1.....word association
Another 'Y' game