954,123 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

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

SyLk
Light Poster
27 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

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

Narue
Bad Cop
Administrator
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
 

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?

SyLk
Light Poster
27 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

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

SyLk
Light Poster
27 posts since Oct 2004
Reputation Points: 10
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(); }

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

SyLk
Light Poster
27 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

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
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You