Start at array.length - 1 instead of 0, and make sure that you don't actually access the -1th index. ;)
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Hi everyone,
I don't want the answer
Do you want the question instead :mrgreen:
Just kidding
Is this what you want
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
freesoft_2000
Practically a Master Poster
623 posts since Jun 2004
Reputation Points: 25
Solved Threads: 10
Here's a way of doing it, works best if you sort the array first, but you don't have to.
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]);
}
}
}
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
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?
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
>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:
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?
public class ja {
public static void main(String[] args)
{
int[] a = {7,3,5,8,1};
System.out.println(a[5]);
}
}
Now what happens when you do the same thing in a different way?
public class ja {
public static void main(String[] args)
{
int[] a = {7,3,5,8,1};
System.out.println(a[a.length]);
}
}
Now, just for grins, try this and see how it gives you the last item in the array:
public class ja {
public static void main(String[] args)
{
int[] a = {7,3,5,8,1};
System.out.println(a[a.length - 1]);
}
}
That's why I used matrix.length-1. :)
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Mines better than yours :cheesy:
joking!
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
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
freesoft_2000
Practically a Master Poster
623 posts since Jun 2004
Reputation Points: 25
Solved Threads: 10
For the love of god any programmer would know that was a careless mistake.
WELL FREE UR RIGHT, ANY PROGRAMMER "SHOULD KNOW"
ALSO, ANYONE WITH 5TH GRADE READING ABILITIES "SHOULD KNOW" THAT I SPECIFICALLY ASKED THAT U NOT SEND ME THE CODE TO MY LAB ASSIGNMENT.
THIS DOES NO GOOD FOR ME, AND THEN IT REALLY DOES NO GOOD WHEN U MAKE A CARELESS MISTAKE WHEN I'M TRYIN TO UNDERSTAND THE CONCEPT.
HOWEVER I DO THANK YOU FOR THE TIME U TOOK TO SHARE YOUR "WHIZ POSTING" WITH ME.
PS no disrespect intended
I also learned how to plug in values for variables and such in third grade. So just posting code should have been as helpful as explaining a beginners concept.
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
Please don't use huge fonts or all upper case when you post. A well reasoned reply would make you look better than whining that everyone "should have known" you made a typo...twice.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401