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();
    }
}

Recommended Answers

All 12 Replies

Start at array.length - 1 instead of 0, and make sure that you don't actually access the -1th index. ;)

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 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?

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]);
              }
    }
}

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();
}

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?

>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. :)

Mines better than yours :cheesy:

joking!

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

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

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.