Results 1 to 3 of 3
  1. #1
    Aries. is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

    Default 2D Array Problem

    Ok guys, I'm having a problem on how to mix this with aryNum with cars.

    So here is my output :



    As expected output is :



    Here's ma code ::

    Java Code:
    package SecYr;
    
    
    public class multiDimensional {
    
    
    	
    	public static void main(String[] args) {
    		String color[] = { "Red" , "Brown" , "Black" , "White" , "Gray" } ;
    		String cars[] = { "GM" , "FORD" , "TOYOTA" , "BMW" , "NISSAN" , "VOLVO" } ;
    		int[][] aryNum = { {10,7,12,10,4}, 
    				{18,11,15,17,10} ,
    				{12,10,9,5,12},
    				{16,6,13,8,3},
    				{10,7,12,6,4},
    				{9,4,7,12,11} } ;
    		
    		
    		for ( String element : color) {
    			System.out.print ("\t" + element ) ;
    		}
    		System.out.print ("\n") ;
    		
    		for ( String car : cars){
    			System.out.print ("\n"+car);
    		}
    		System.out.print("\n");
    		int rows = 6;
    		int columns = 5;
    
    
    		int i, j;
    
    
    		for (i=0; i < rows ; i++) {
    
    
    		for (j=0; j < columns ; j++) {
    
    
    		System.out.print("\t[" + aryNum[ i ][ j ] + "] " );
    
    
    		}
    
    
    		System.out.println( "" );
    
    
    		}
    		
    		
    
    
    	}
    
    
    }

    Thanks for the advance tips.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,525
    Rep Power
    5

    Default Re: 2D Array Problem

    Ok. You used nested for loops to print out the numbers. Place the print statement for the cars after the first of the nested for loops. And index it like an array using i.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Aries. is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

    Default Re: 2D Array Problem

    Quote Originally Posted by jim829 View Post
    Ok. You used nested for loops to print out the numbers. Place the print statement for the cars after the first of the nested for loops. And index it like an array using i.

    Regards,
    Jim
    I got it now, it is just this statement :
    Java Code:
    for ( String car : cars){
                System.out.print ("\n"+car);
            }
            System.out.print("\n");
    create an another lining and doesn't connected to nested loop.

    Sorry I'm noob.


    Thanks and now its okay.

Similar Threads

  1. Replies: 3
    Last Post: 12-28-2013, 09:39 PM
  2. Really need help with an Array problem
    By ndsmith20 in forum New To Java
    Replies: 4
    Last Post: 11-07-2012, 10:57 PM
  3. Replies: 0
    Last Post: 05-12-2011, 07:04 PM
  4. Array problem
    By Red727 in forum New To Java
    Replies: 2
    Last Post: 12-19-2010, 03:26 AM
  5. Array Problem?
    By noobgrammer in forum New To Java
    Replies: 4
    Last Post: 06-19-2010, 11:25 PM

Posting Permissions

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