943,899 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 6646
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 10th, 2005
0

how to set up my for loop to print my array in reverse

Expand Post »
im having problems figuring out how to print this array in reverse.I believe it might be a logical issue. I don't want the answer. I'm thinkin my row must be initialized to the matrix.length. can someone give me a few hints as to go about this.



public static void printMatrixReverse(int [][]matrix)
{
int row, col;
for (row = 0 row < matrix.length;row++)
{
for (col=0;col < matrix[row].length;col++)
System.out.print(matrix[row][col] + "\t");
System.out.println();
}
}
Reputation Points: 10
Solved Threads: 0
Light Poster
SyLk is offline Offline
27 posts
since Oct 2004
Mar 10th, 2005
0

Re: how to set up my for loop to print my array in reverse

Start at array.length - 1 instead of 0, and make sure that you don't actually access the -1th index.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 10th, 2005
0

Re: how to set up my for loop to print my array in reverse

Hi everyone,

Quote originally posted by SyLk ...
I don't want the answer
Do you want the question instead :mrgreen:

Just kidding

Is this what you want

Java Syntax (Toggle Plain Text)
  1.  
  2. public static void printMatrixReverse(int [][]matrix)
  3. {
  4. int row, col;
  5.  
  6. for (row = matrix.length; row >= 0 ; row--)
  7. {
  8.  
  9. for (col = matrix[row].length; col >= 0; col--)
  10. {
  11.  
  12. System.out.print(matrix[row][col] + "\t");
  13. System.out.println();
  14.  
  15. }
  16. }

Let me know if it works

Yours Sincerely

Richard West
Reputation Points: 25
Solved Threads: 10
Practically a Master Poster
freesoft_2000 is offline Offline
623 posts
since Jun 2004
Mar 10th, 2005
0

Re: how to set up my for loop to print my array in reverse

freesoft thats far from correct.

Narue can u explain to me why i would initialize row to matrix.length-1?

1 2 3
4 5 6
would'nt that cause my numbers to start at 5 instead of 6?
Reputation Points: 10
Solved Threads: 0
Light Poster
SyLk is offline Offline
27 posts
since Oct 2004
Mar 10th, 2005
0

Re: how to set up my for loop to print my array in reverse

Here's a way of doing it, works best if you sort the array first, but you don't have to.

Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2.  
  3. class DescendingOrder
  4. {
  5. public static void main(String[] arguments)
  6. {
  7. String[] names = {"a", "c", "f", "d", "e"};
  8. Arrays.sort(names);
  9.  
  10. System.out.println("Descending order:");
  11.  
  12. for (int a = 0; a < names.length; a++)
  13. {
  14. System.out.println(a + names[names.length
  15. - 1 - a]);
  16. }
  17. }
  18. }
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Mar 10th, 2005
0

Re: how to set up my for loop to print my array in reverse

Narue I got it done through a little bit of tial and error
but the concept of setting row = to matrix.length-1 is still puzzling
if u could clear this up for me i'd be very thankful.....TY


for (row=matrix.length-1;row>=0;row--)
{
for (col=matrix[row].length-1;col>=0;col--)
System.out.print(matrix[row][col] + "\t");
System.out.println();
}
Reputation Points: 10
Solved Threads: 0
Light Poster
SyLk is offline Offline
27 posts
since Oct 2004
Mar 10th, 2005
0

Re: how to set up my for loop to print my array in reverse

Quote originally posted by SyLk ...
Narue I got it done through a little bit of tial and error
but the concept of setting row = to matrix.length-1 is still puzzling
if u could clear this up for me i'd be very thankful.....TY


for (row=matrix.length-1;row>=0;row--)
{
for (col=matrix[row].length-1;col>=0;col--)
System.out.print(matrix[row][col] + "\t");
System.out.println();
}
I just gave you a clear example?
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Mar 10th, 2005
0

Re: how to set up my for loop to print my array in reverse

>Let me know if it works
If you tested it then you would find out that it accesses your arrays out of bounds.

>Narue can u explain to me why i would initialize row to matrix.length-1?
Arrays in Java are zero based, which means that the indices go from 0 to length-1. If you initialize the index variable to matrix.length then you'll be accessing the array out of bounds and it will throw an exception. Consider this array:
Java Syntax (Toggle Plain Text)
  1. int[] a = {7,3,5,8,1};
The first item is 7 at a[0]. The fifth item is 1 at a[4]. a.length evaluates to 5, so what happens when you do this?
Java Syntax (Toggle Plain Text)
  1. public class ja {
  2. public static void main(String[] args)
  3. {
  4. int[] a = {7,3,5,8,1};
  5. System.out.println(a[5]);
  6. }
  7. }
Now what happens when you do the same thing in a different way?
Java Syntax (Toggle Plain Text)
  1. public class ja {
  2. public static void main(String[] args)
  3. {
  4. int[] a = {7,3,5,8,1};
  5. System.out.println(a[a.length]);
  6. }
  7. }
Now, just for grins, try this and see how it gives you the last item in the array:
Java Syntax (Toggle Plain Text)
  1. public class ja {
  2. public static void main(String[] args)
  3. {
  4. int[] a = {7,3,5,8,1};
  5. System.out.println(a[a.length - 1]);
  6. }
  7. }
That's why I used matrix.length-1.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 10th, 2005
0

Re: how to set up my for loop to print my array in reverse

Mines better than yours :cheesy:

joking!
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Mar 11th, 2005
0

Re: how to set up my for loop to print my array in reverse

Hi everyone,

I FORGOT THE -1 SO SUE ME

For the love of god any programmer would know that was a careless mistake.

Seriously sylk don't tell me you didn't realize that?

Richard West
Reputation Points: 25
Solved Threads: 10
Practically a Master Poster
freesoft_2000 is offline Offline
623 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Loading a Java String into a TextArea in an External Application - Please Help
Next Thread in Java Forum Timeline: good tutorial site for this





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC