Results 1 to 8 of 8
  1. #1
    fanle is offline Member
    Join Date
    Jul 2009
    Posts
    6
    Rep Power
    0

    Default Question about 2D arrays

    Hi guys I wonding if you could possibly help me out with this this problem I have been having.

    I have the following 2D array:
    Java Code:
    int[][] processes = {{1,2,3,4,5}, {10,1,2,1,5}, {3,1,3,4,2}};
    What I am trying to accomplish is to repeat the first rows values the number of times stated in the corresponding row of the column below.
    For eg.
    1111111111233 etc.

    Any tips would be greatly appreciated

    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    What have you tried so far? Please post your best attempt and we can go over it.

  3. #3
    fanle is offline Member
    Join Date
    Jul 2009
    Posts
    6
    Rep Power
    0

    Default

    Basically so far i have built a for loop and read each of the rows and columns and outputs them. I know in my head how I think it should work but I just cant get my head around the code.

    Java Code:
    int[][] processes = {{1,2,3,4,5}, {10,1,2,1,5}, {3,1,3,4,2}};
    			
    for (int row = 0; row < processes.length; row++)
    	{
    		for (int column = 0; column < processes[row].length; column++)
    			{	
    //if
    				System.out.print(processes[column] + " ");
    			}
    	}
    Where i have typed if I believe there should be an if statement there, however i believe that i need to insert a for loop somewhere else to get the value repeating.
    Last edited by fanle; 07-22-2009 at 04:08 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    This code:
    Java Code:
        for (int row = 0; row < processes.length; row++) {
          for (int column = 0; column < processes[row].length; column++) {
            // if
            //System.out.print(processes[column] + " ");
          }
        }
    is almost there.

    First, consider the first row of the array to be filled with constants, and the other two rows will tell you how to act on these constants. The first thing you need to figure out is which row to start looping on. My feeling is since you'll need the information from rows 1 and 2 to help tell you what to do with the constants in row 0, that your outer for loop should loop through rows 1 and 2 (the second and third rows), and then loop through every column as your inner loop is doing.

    Then you need to figure out what needs to be done inside the inner loop above, where your comments are. Thinking that you'll have to print a number repeatedly -- and every time I think repeatedly, I think of a loop, and since we'll know how many times we need to print each number (it'll be the processes[row][column] value) another inner for loop might fit the bill.

    Can you make anything of these suggestions? Please give it a go.

  5. #5
    fanle is offline Member
    Join Date
    Jul 2009
    Posts
    6
    Rep Power
    0

    Default

    Thanks for the reply made alot of sense when you think of the first row as constants.
    I think it is now looping the second row in the outter loop (the third row is now used at this point in the code)
    I cant however seem to get the numbers repeating on the printout. I added another inner for loop however i am clearly doing it wrong as it will not repeat :)

    Java Code:
    		int[][] processes = {{1,2,3,4,5}, {10,1,2,1,5}, {3,1,3,4,2}};
    		
    		
    		for (int row = 1; row < 2; row++)
    		{
    			for (int column = 0; column < processes[row].length; column++)
    			{
    				for (int loop = 0; loop < processes[row][column]; loop++);
    				{
    					System.out.print(processes[0][column]);
    				}
    			}
    		}

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    My lord but you are so close.

    Your outer-most loop starts fine at index number 1, but ends prematurely. Try looping until process.length not "2" (it should be 3, but better to get in the habit of using array lengths here).

    Your inner-most loop is being foiled by a misplaced semicolon. Can you find it? Have you tried to remove it?

  7. #7
    fanle is offline Member
    Join Date
    Jul 2009
    Posts
    6
    Rep Power
    0

    Default

    :p
    Those damn semi-colons.

    Java Code:
    				for (int loop = 0; loop < processes[row][column]; loop++)
    				{
    					System.out.print(processes[0][column]);
    				}
    :)

    Thanks a million for your help

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You're entirely welcome. It's always nice to help someone who shows a willingness to do some work.

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  2. Byte arrays and MIDI - simple question?
    By Ravaa in forum New To Java
    Replies: 1
    Last Post: 03-23-2009, 09:47 PM
  3. arrays
    By hasysf in forum New To Java
    Replies: 12
    Last Post: 07-28-2008, 02:38 AM
  4. question about arrays
    By broganm1 in forum New To Java
    Replies: 3
    Last Post: 02-13-2008, 02:29 AM
  5. Question about arrays
    By nhlfan in forum New To Java
    Replies: 4
    Last Post: 11-15-2007, 11:38 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
  •