File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes arrays but ... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "arrays but ..." Watch "arrays but ..." New topic
Author

arrays but ...

A.Al-Ansari
Greenhorn

Joined: Oct 18, 2003
Posts: 16
its not working i want to reverse the array but its not working whats wrong with my method,...arrays are really hard


[edited to fix formatting --JM]
plzz
[ November 14, 2003: Message edited by: Jason Menard ]
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
If I understand it correctly, your printR method is supposed to print the array in its normal order.
If this is the case, I suggest you look real close at the for (...) statement in that method.
Also, you might want to rethink your reverseR method, which will not actually reverse the array. You might want to think about using a new temporary array in your implementation to hold the reversed version of your array.
Here's a hint to get you started

[ November 14, 2003: Message edited by: Jason Menard ]
A.Al-Ansari
Greenhorn

Joined: Oct 18, 2003
Posts: 16
sorry but i really lost more..! dose we need all this to reverse an array! i though that i'm wrong in my for statment! and i didnt get ur code at all.. what should j equal to! and how should use this instead of my method.. sorry but arrays r my biggest problem and i should lean it very well even i'm into advanced stuff but now i'm losr cos of the arrays thats why i'm starting to learn it again
Your Best Ever...smile
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
Did you find your error in printR() yet?
Getting back to reverseR(), let's take a look at the code you have to see exactly what it does:

You initialized an array m as follows, and passed it to your reverseR method.

Stepping through the code, with the above values passed to it, things would go as follows:
reverseR(int[] list = {0,1,2,3,4,5,6,7,8,9})
============================================
list.length = 10
// you have a for loop which will count down from 9 to 0:
for (int i = 9; i >= 0; i--)
----------------------------
i = 9 : list[9] = 9 (list[ i ] = i, where i = 9)
i = 8 : list[8] = 8
...
i = 0 : list[0] = 0
So you can see that when you are done with your for loop, your int[] list will look like the following: {0,1,2,3,4,5,6,7,8,9}.
Let's say now that instead of m being initialized to {0,1,2,3,4,5,6,7,8,9} that you initialized m to be {2,4,6,8}. Inputting that array into reverseR would go like this:
reverseR(int[] list = {2,4,6,8})
================================
list.length = 4
for (int i = 3; i >= 0; i--)
----------------------------
i = 3 : list[3] = 3
i = 2 : list[2] = 2
i = 1 : list[1] = 1
i = 0 : list[0] = 0
Now your int[] list contains the following values: {0,1,2,3}. Can you see what is happening here?
So what you really want to do is, given an array {2,4,6,8}, reverse it to that it is {8,6,4,2}. Am I correct? While it is true that you could do this using only one array (by swapping values), it is easier to illustrate using two arrays. That is, you take your initial array and copy it into another array in reversed order.
Let's look again at the code snippet I gave you earlier.

I'll trace through it using an input array {2,4,6,8}.
reverseR(int[] list = {2,4,6,8})
================================
len = 4
int[] b = new int[4]
for (int i = 0; i < 4; i++)
---------------------------
i = 0 : j = 3 (len = 4 and i = 0, so j = len - i - 1 = 4 - 0 - 1 = 3)
i = 1 : j = 2
i = 2 : j = 1
i = 3 : j = 0
Given my code with the input as array as shown above, what would your array b look like after going through the for(...) loop if we added the following line to the for(...) loop?
b[j] = a[ i ]
Now you have a reversed array, but it's a different array and not assigned to the array that you passed in as a parameter to the method. You'll want to take care of that as well by assigning the values in b to a.
For a decent tutorial on arrays, you may want to read this link.
[ November 16, 2003: Message edited by: Jason Menard ]
A.Al-Ansari
Greenhorn

Joined: Oct 18, 2003
Posts: 16
oh god..
i really dont know how to thank u..i always needed some explanation as this to understand what witch thing dose.. u really took alot of time in this thank alot really dont know how to thank u.
i'll read it carefully and i'll tell u if it worked..thanks alot again dear.
Your Best Ever...smile
A.Al-Ansari
Greenhorn

Joined: Oct 18, 2003
Posts: 16
sorry but here is the programe again no errors but its not reversing

sorry i'm really bothring u even its easy..
Your Best Ever...smile
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
To be honest, I was expecting you to analyse and use the code snippet I gave you as inspiration, not simply to copy it verbatim. I did slightly edit a portion of my previous message to hopefully make the wording a little more precise.
The problem is the assignment statement a = b. You can go ahead and get rid of it. What you actually need to do is copy the contents of the array b to the array a. Hint: You'll need another for (...) loop in reverseR().
[ November 16, 2003: Message edited by: Jason Menard ]
A.Al-Ansari
Greenhorn

Joined: Oct 18, 2003
Posts: 16
oh so i didnt get that cos not that good in english..so i red your post many times ...and i thoughi t would work..as it was and that was explanation for it.. so now i got it i'll try to find the soloution in ur way..
Your Best Ever...smile
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: arrays but ...
 
Similar Threads
Increasing Array Size
Sorting using Arrays and comparators
Sorting NestedLoops
PipeReader and PipeWriter
How to find the smallest element in an array?