This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array

 
bharath pawan
Greenhorn
Posts: 11
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain what you mean by "with out using indexing".
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • 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: 20511
54
Chrome Eclipse IDE Java Windows
  • 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: 20958
31
Eclipse IDE Firefox Browser MySQL Database
  • 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
  • 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 ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic