Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array

 
bharath pawan
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to reverse an array with out using indexing
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain what you mean by "with out using indexing".
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could do this in C(++) because of pointer arithmetic, but in Java that doesn't exist.
 
Paul Clapham
Sheriff
Pie
Posts: 20196
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic