Reverse the order of elements in an array

how would i go about Reverse the order of elements in an array?
[70 byte] By [Jared007a] at [2007-11-26 16:22:23]
# 1
qUesT_foR_knOwLeDge? What to make 10 quick duke dollars and do this chap's homework?
DrLaszloJamfa at 2007-7-8 22:46:11 > top of Java-index,Java Essentials,New To Java...
# 2
There are several ways of doing this. What have you come up with so far?
bckrispia at 2007-7-8 22:46:11 > top of Java-index,Java Essentials,New To Java...
# 3

> There are several ways of doing this. What have you come up with so far?

Why bother with the Socratic method? Hoping to actually teach the OP something? There are enough forum members that someone soon comes along to post a complete solution, specially when dukes are involved. <sigh/>

DrLaszloJamfa at 2007-7-8 22:46:11 > top of Java-index,Java Essentials,New To Java...
# 4
...And thus, the reason I only have a bronze star. :P
bckrispia at 2007-7-8 22:46:11 > top of Java-index,Java Essentials,New To Java...
# 5
I link to the javadocs can't be too bad. [url http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collections.html#reverse(java.util.List)]Collections.reverse(List)[/url]
Caffeine0001a at 2007-7-8 22:46:11 > top of Java-index,Java Essentials,New To Java...
# 6

> I link to the javadocs can't be too bad. [url

> http://java.sun.com/j2se/1.5.0/docs/api/java/util/Coll

> ections.html#reverse(java.util.List)]Collections.rever

> se(List)[/url]

Of course I fortell the next question, but I want an Array not a List? So I provide the next link: [url http://java.sun.com/j2se/1.5.0/docs/api/java/util/Arrays.html#asList(T...)]Arrays.asList(T...)[/url]

Caffeine0001a at 2007-7-8 22:46:11 > top of Java-index,Java Essentials,New To Java...
# 7

> > I link to the javadocs can't be too bad. [url

> >

> http://java.sun.com/j2se/1.5.0/docs/api/java/util/Coll

>

> >

> ections.html#reverse(java.util.List)]Collections.rever

>

> > se(List)[/url]

>

> Of course I fortell the next question, but I want an

> Array not a List? So I provide the next link: [url

> http://java.sun.com/j2se/1.5.0/docs/api/java/util/Arra

> ys.html#asList(T...)]Arrays.asList(T...)[/url]

The next question. But the results need to be an Array. So once more. Another link: [url http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collection.html#toArray()]Collection.toArray()[/url]

Caffeine0001a at 2007-7-8 22:46:11 > top of Java-index,Java Essentials,New To Java...
# 8

> > I link to the javadocs can't be too bad. [url

> >

> http://java.sun.com/j2se/1.5.0/docs/api/java/util/Coll

>

> >

> ections.html#reverse(java.util.List)]Collections.rever

>

> > se(List)[/url]

>

> Of course I fortell the next question, but I want an

> Array not a List? So I provide the next link: [url

> http://java.sun.com/j2se/1.5.0/docs/api/java/util/Arra

> ys.html#asList(T...)]Arrays.asList(T...)[/url]

Yeah but then you have to worry about the next question which is more of a statement: "I can't use Arrays".

kablaira at 2007-7-8 22:46:11 > top of Java-index,Java Essentials,New To Java...
# 9

> > > I link to the javadocs can't be too bad. [url

> > >

> >

> http://java.sun.com/j2se/1.5.0/docs/api/java/util/Coll

>

> >

> > >

> >

> ections.html#reverse(java.util.List)]Collections.rever

>

> >

> > > se(List)[/url]

> >

> > Of course I fortell the next question, but I want

> an

> > Array not a List? So I provide the next link:

> [url

> http://java.sun.com/j2se/1.5.0/docs/api/java/util/Arr

>

> > ys.html#asList(T...)]Arrays.asList(T...)[/url]

>

> Yeah but then you have to worry about the next

> question which is more of a statement: "I can't use

> Arrays".

Then I would as them: How would you do it in real life? Of course they could think a real world way of doing it. In that case, I'll give them an real life example.

Assumed List

1 2 3 4 5 6 7 8 9 10 11 12 13

Switch the first and the last elements

13 2 3 4 5 6 7 8 9 10 11 12 1

Then the second and second to the last

13 12 3 4 5 6 7 8 9 10 11 2 1

And repeat until your half way.

13 12 10 4 5 6 7 8 9 3 2 1

13 12 10 9 5 6 7 8 4 3 2 1

13 12 10 9 8 6 7 5 4 3 2 1

13 12 10 9 8 7 6 5 4 3 2 1

Done.

Caffeine0001a at 2007-7-8 22:46:11 > top of Java-index,Java Essentials,New To Java...
# 10
Of course the next question. How do a repeat? and How do I swap?It just drives people to just give the solution.
Caffeine0001a at 2007-7-8 22:46:11 > top of Java-index,Java Essentials,New To Java...
# 11

> Of course the next question. How do a repeat? and

> How do I swap?

>

> It just drives people to just give the solution.

Drives me to point them in the direction they need to head to learn themselves. If they choose not to pursue it then I have no sympathy for them. People who aren't looking to have their work done for them may not know where to find the information they need or what precisely to "Google" for. Give them that information and they'll either help themselves and come back with questions or they aren't worth the trouble.

kablaira at 2007-7-8 22:46:11 > top of Java-index,Java Essentials,New To Java...
# 12

> Then I would as them: How would you do it in real life?

This morning I wanted to reverse the order of 6 or 7 cereal boxes on a shelf. I grabbed them as a bunch (by squeezing them all together) and put them together on the kitchen table. Then I walked around to the other size of the table and grabbed them as a bunch again, but from behind and placed them back on the shelf, now in the reverse order.

Code please!

DrLaszloJamfa at 2007-7-8 22:46:11 > top of Java-index,Java Essentials,New To Java...
# 13

> > Then I would as them: How would you do it in real

> life?

>

> This morning I wanted to reverse the order of 6 or 7

> cereal boxes on a shelf. I grabbed them as a bunch

> (by squeezing them all together) and put them

> together on the kitchen table. Then I walked around

> to the other size of the table and grabbed them as a

> bunch again, but from behind and placed them back on

> the shelf, now in the reverse order.

>

> Code please!

As soon as I finish building my Quantum computer. I'll give you the code.

Caffeine0001a at 2007-7-8 22:46:11 > top of Java-index,Java Essentials,New To Java...
# 14

I posted the cereal example because I was thinking back on all the times Ive gotten those sorts of answers when trying to help people come up with algorithms. I remember one time I was tring to help someone do something simple -- find the max int in an array -- and I asked him how he would do it in real life. He said, "I would stare at the numbers until I knew the answer." Then he stared at me. Presumably he would have stared until I gave him the answer...

DrLaszloJamfa at 2007-7-8 22:46:11 > top of Java-index,Java Essentials,New To Java...
# 15

> > Then I would as them: How would you do it in real

> life?

>

> This morning I wanted to reverse the order of 6 or 7

> cereal boxes on a shelf. I grabbed them as a bunch

> (by squeezing them all together) and put them

> together on the kitchen table. Then I walked around

> to the other size of the table and grabbed them as a

> bunch again, but from behind and placed them back on

> the shelf, now in the reverse order.

>

> Code please!

Seriously. It is the same as the example I gave. Your example just does the number swapping as you turn around.It just does each number switch (cereal switch) gradually over the 180% turn. It is just a two dimensional example of the same process.

Caffeine0001a at 2007-7-21 16:44:49 > top of Java-index,Java Essentials,New To Java...