| | |
how to set up my for loop to print my array in reverse
![]() |
•
•
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: 7
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]); } }
I'm here to prove you wrong.
![]() |
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
| Thread Tools | Search this Thread |
account android api applet application array arrays automation bidirectional binary birt bluetooth class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors exception expand fractal game givemetehcodez graphics gui guidancer homework html ide image inetaddress inheritance integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield jtree julia linux list loop map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source oracle plazmic print problem program project property recursion ria scanner search server set sharepoint smart sms smsspam sort sourcelabs splash sql sqlite static string subclass support swing testautomation threads tree unlimited webservices windows






