aspose file tools*
The moose likes Beginning Java and the fly likes Array in reverse order......? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array in reverse order......?" Watch "Array in reverse order......?" New topic
Author

Array in reverse order......?

Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
ok....i have to put an array in reverse order right...

heres my code....its really complicated...and i know there has to be an easier way.



now even if this is correct, which i doubt.... the compiler is giving me an error at the first { of the first if statement...

thx for the help again! lol


You down with OOP? Yeah you know me!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

you don't have an "if" statement. you have an "If" statement.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Here's my suggestion:

Create a new empty array for the reversed data. Then iterate through the original array from "front to back," assigning values to the reverse array from "back to front."

If you think carefully about how the Nth element in the original array relates to the Nth from the last element in the reversed array, this can be done in a single line within a for loop.

PS: Did you change your display name? To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names. Thanks!


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Do you really need an array? That is, is this homework? I suspect so, in which case ignore the remainder of this post.

Arrays contain a devastational abstraction leak in more than one place. There is one that you are observing at the moment - that to represent the elements of an ordered sequence in some other way requires an element copy of that sequence. An array exposes three operations (getElementAtIndex, setElementAtIndex, getElementsLength) all of which are bound (symbiotic operations on contract) - which implies other abstraction leaks - but they also bind you to an in-memory representation of the data. For example, a byte[] does more than "hold bytes" it "holds bytes in memory". This is the abstraction leak that I refer to.

If you want your array to look different, you need to change the "in-memory" representation - by way of an element copy. Another example is where you want to append to an array - since elements are fixed in-memory, you are forced to reallocate memory (new array) and perform an element copy. I refer back to the three operations of arrays - if you could simply create an appropriate abstraction (such as an interface (referred to as interface Array)) that represents an array, then write another interface that, for example, "appends" or "reverses" (referred to as OperationArray) and returns an Array that represents your operation, then no element copy is required.

Let's look at an example - for simplicity, we will remove the setElementsAtIndex operation of the Array interface:



You could easily implement the ReverseArray method without requiring an element copy - let's look at one (not the nicest) way of doing it:



So my question comes back to: is it really an array that you want? ...or do you want a structure that has a tighter level of encapsulation (clearly, arrays are flawed - this reason is one of many)? It all comes down to requirements analysis - do you want "an ordered sequence of elements"? ...or "an ordered sequence of elements that are bound to memory"? There are some very odd corner cases where the latter is a valid requirement, but these fall outside of my definition of "software" - which leads me to the conclusion that using arrays for "software" implies a forced requirement defect (excessive contract and excessive requirements implementation). I observe this commonality for many of the things that most developers take for granted, but I use it to my advantage by allowing me to make an informed decision about the correct approach to take - is this what you need for your case?

ContractualJ provides an alternative to arrays (and collections), which I believe are inherently flawed such that they cause great pain due to constantly conceding to their intrinsic requirement defects. The above example code has been implemented more extensively in the net.tmorris.adt.sequence package and has been tested thoroughly (100% code coverage) - in fact, the code above may not even compile - I didn't try it.

I hope this rants helps.


Tony Morris
Java Q&A (FAQ, Trivia)
Karl Svensson
Greenhorn

Joined: Jan 26, 2006
Posts: 9
I suck at both English and Java, but I will try to explain how I think I would do. Begin with swapping the first and the last element, then the second and the "last but one" (last-1), and so on, until you reach an index which already has been involved in this swapping process. So, maybe a for loop which iterates until it reaches B.length/2.
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
Yeah that is a lot of code for a simple task. Using another array is kind of cheating since the point of these sorts of assignments are manipulating arrays, which is a requirement to learning how sorting algorithms work. Arrays are rarely used, but are a good teaching tool, and help you learn things like sorting, which may not be a requirement to use an API, but learning syntax and some API stuff doesn't make you a professional.

simple code for an array of ints:


This is off the top of my head but should be close. Buyer beware.


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Originally posted by Rusty Shackleford:
Yeah that is a lot of code for a simple task.

The workaround code is verbose ala Java and its inherent defects. It's much much less code in any case:
Given Array<E> a,
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
The point is learning how to write the reverse method in the API. Using an API is all well and good, but there is a rather large difference between a Java Programmer(one who knows the language and the associated API's) and a complete programmer(Someone who finds API's useful but doesn't actually need them if push came to shove).
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
ok ive been working on it lol, i guess im retarded or something, it keeps f'n printing the same array twice....heres the code




what the heck is wrong with it....im going quite insane over this..
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
i tried that reverse code for java, and it said that didnt exist, lol...
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

//Get the first and last values from array and assign to variables...
int first = B[ i ];
int last = B[B.length-1];

//Exchange variable values...
int swap = first;
first = last;
last = swap;

//So far, so good, but the array itself hasn't changed -- only the variables "first" and "last."

//And now what's happening here?
last --;
//Did you really want to decrement the value of last?
[ January 28, 2006: Message edited by: marc weber ]
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
The first problem is in your logic in the for loop used to reverse the array.

You assign to the variable first the value of B[i], then you assign to the variable last the value B[B.length - 1], then all you are doing is reassigning these variables to different values, but B[i], and B[B.length -1] are staying the same, you're not doing anything to the elements of the array. Not to mention decrementing the variable last (last --) isn't helping you get anywhere because at the beginning of the loop you reassign the varaible to be the last element in the array over and over again. You should rewrite this loop altoghether because as it stands now, its not going to get you where you want to go.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802


ok, on this, how do i decrement from the bottom up??....i tried....

Dec -=1; but say i put in four ints, after the first loop, it keeps the variable C at B[2] util i = Dec;....

lol im almost there, plz help me out thx..
Craig Tyler
Ranch Hand

Joined: Jan 15, 2006
Posts: 52
Why don't you try incrementing one variable in the loop and decrementing another at the same time so they meet in the middle?


[ January 29, 2006: Message edited by: Craig Tyler ]
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
omg...i had no idea you could do that!!!...

your a genius guy....and i love you mucho

thank you sooo much
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Another solution would be to have a single counter (say i) and calculate the other index based on the array's length and the first counter.

Layne


Java API Documentation
The Java Tutorial
Karl Svensson
Greenhorn

Joined: Jan 26, 2006
Posts: 9
Originally posted by Layne Lund:
Another solution would be to have a single counter (say i) and calculate the other index based on the array's length and the first counter.


That's what I was trying to suggest. But who listens to a n00b?
[ January 30, 2006: Message edited by: Karl Svensson ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Karl Svensson:


That's what I was trying to suggest. But who listens to a n00b?

[ January 30, 2006: Message edited by: Karl Svensson ]


Or perhaps great minds think alike.

Sorry for missing your earlier post. Of course, I hope that stating the same thing in a different way might help the OP understand it more clearly.

Layne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array in reverse order......?
 
Similar Threads
Why can't I sort Integer Array in reverse order
How many objects are created by the following two statements?
write byte array to a file
Merge Sort
Finding Nth Largest element of an array without sorting