File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "array" Watch "array" New topic


bharath pawan

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

Joined: Oct 13, 2005
Posts: 46375
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

Joined: Oct 27, 2005
Posts: 20275

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

How To Ask Questions How To Answer Questions
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

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 ...
I agree. Here's the link:
subject: array
It's not a secret anymore!