| | |
how to set up my for loop to print my array in reverse
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2004
Posts: 26
Reputation:
Solved Threads: 0
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();
}
}
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();
}
}
•
•
Join Date: Jun 2004
Posts: 609
Reputation:
Solved Threads: 8
Hi everyone,
Do you want the question instead :mrgreen:
Just kidding
Is this what you want
Let me know if it works
Yours Sincerely
Richard West
•
•
•
•
Originally Posted by SyLk
I don't want the answer
Just kidding
Is this what you want
Java Syntax (Toggle Plain Text)
public static void printMatrixReverse(int [][]matrix) { int row, col; for (row = matrix.length; row >= 0 ; row--) { for (col = matrix[row].length; col >= 0; col--) { System.out.print(matrix[row][col] + "\t"); System.out.println(); } }
Let me know if it works
Yours Sincerely
Richard West
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
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)
import java.util.*; class DescendingOrder { public static void main(String[] arguments) { String[] names = {"a", "c", "f", "d", "e"}; Arrays.sort(names); System.out.println("Descending order:"); for (int a = 0; a < names.length; a++) { System.out.println(a + names[names.length - 1 - a]); } } }
•
•
Join Date: Oct 2004
Posts: 26
Reputation:
Solved Threads: 0
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();
}
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();
}
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
•
•
•
•
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();
}
>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:
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?
Now what happens when you do the same thing in a different way?
Now, just for grins, try this and see how it gives you the last item in the array:
That's why I used matrix.length-1.
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)
int[] a = {7,3,5,8,1};
Java Syntax (Toggle Plain Text)
public class ja { public static void main(String[] args) { int[] a = {7,3,5,8,1}; System.out.println(a[5]); } }
Java Syntax (Toggle Plain Text)
public class ja { public static void main(String[] args) { int[] a = {7,3,5,8,1}; System.out.println(a[a.length]); } }
Java Syntax (Toggle Plain Text)
public class ja { public static void main(String[] args) { int[] a = {7,3,5,8,1}; System.out.println(a[a.length - 1]); } }
New members chased away this month: 4
![]() |
Other Threads in the Java Forum
- Previous Thread: Loading a Java String into a TextArea in an External Application - Please Help
- Next Thread: good tutorial site for this
Views: 4158 | Replies: 12
| Thread Tools | Search this Thread |
Tag cloud for Java
-xlint android animated api appinventor apple applet application arguments array arrays automation bi binary blackberry bluetooth chat class classes client code compile compiler component database draw eclipse error event exception file fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui helpwithhomework html ide image input integer j2me java javaprojects jetbrains jmf jni jpanel jtable julia learningresources linux list login loop main map method methods mobile myregfun netbeans newbie notdisplaying number object oracle page print problem program programming project qt recursion scanner screen server set size sms socket sort spamblocker sql string swing system test threads time transfer tree variablebinding windows xor






