Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2009
    Posts
    27
    Rep Power
    0

    Default displaying 2D-Matrix

    To display 2D-Matrix we use two loops.but I want to display by using only one loop.Pls help me in this issue.

  2. #2
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default This is easy, but difficult to put in print

    You must set up your single for-loop
    to run the duration of the entire 2D array.

    !!!???

    Example:

    int[][] my2D = new int[5][8];

    for(int i=0; i<40; i++){
    ...
    }

    Now YOU must figure out the arithmetic
    within the braces that will increment two
    indexes that reference the individual elements.

  3. #3
    jurka is offline Member
    Join Date
    Jul 2008
    Posts
    67
    Rep Power
    0

    Default

    Hm there should be possible to write this with for loop, but this makes condition line much longer then it would be with two loops.

    Why not use some own made looping method ? Or use just for-each loop this doesn't need so much typing.

    Java Code:
            int array[][] = new int[3][3];
    
            for (int rows = 0; rows < array.length; rows++) {
                for (int cols = 0; cols < array[0].length; cols++) {
                    array[rows][cols] = cols + rows;
                }
            }
    
    
            for (int[] rows : array) {
                for (int columns : rows) {
                    System.out.print(columns);
                }
                System.out.println("");
            }

Similar Threads

  1. Help with dox matrix printer
    By Albert in forum Advanced Java
    Replies: 7
    Last Post: 09-06-2011, 08:50 AM
  2. [SOLVED] Help with Matrix
    By Bernard Robitaille in forum JCreator
    Replies: 10
    Last Post: 02-14-2009, 02:19 AM
  3. Matrix variations with repetition
    By jnovice in forum New To Java
    Replies: 4
    Last Post: 01-19-2009, 08:42 AM
  4. Displaying Data
    By skiing in forum New To Java
    Replies: 4
    Last Post: 11-15-2008, 02:56 AM
  5. Help with matrix
    By susan in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 04:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •