Results 1 to 9 of 9
  1. #1
    hadenkl is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default 2D array - how do I create one with 20 rows and columns?

    Hi everyone,

    I'm really new to java and I'm not comfortable with it AT ALL.
    I need to create a 2d array that has rows and columns in it. I've got a gridsize of 20 x 20. I'm just a bit confused as to how to declare the array in the first place: I've got this below at the moment:

    int [][] hits = new int [20][20]

    But I thought it might need to have something like [COL_ARRAY][ROW_ARRAY] and I declare the two arrays first.

    Totally, totally confused. I need to use the array to jump through the grid and pick up random samples of each cell later on (which I think I've got under control).

    Any help is good help!! Thanks!!

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    > I've got this below at the moment:

    Does it work? What problems are you having?

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    Java Code:
    int [][] hits = new int [20][20]
    This is correct.
    But I thought it might need to have something like [COL_ARRAY][ROW_ARRAY] and I declare the two arrays first.
    This makes no sense. A 2D array is simply a 1D array of arrays. So what you actually have is 20 rows in your 2D array. Therefore you can do something like this:
    Java Code:
    int[] temp = {1,2,3,4};
    int[][] matrix = new int[4][4];
    matrix[0] = temp;
    matrix[1] = temp;
    matrix[2] = temp;
    matrix[3] = temp;
    Or
    Java Code:
    int[][] matrix = new int[4][4];
    for(int x = 0; x < matrix.length; x++) {
        for(int y = 0; y < matrix[x].length; y++) {
            matrix[x][y] = x * y;
        }
    }
    Last edited by Junky; 05-16-2011 at 05:29 AM. Reason: The perils of copy and paste

  4. #4
    hadenkl is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    Okay, thanks; good to know I had it in the first place!! I've been tidying up my program and getting things indented properly etc; my main problem now is that I have to set the array to 0 and it keeps telling me it's already been declared earlier. this is what I have as pseudocode:

    For column = 0 to gridsize -1[/INDENT]For row = 0 to girdsize -1[/INDENT][/INDENT]Set hits [column][hits] to 0
    Repeat samples times

    Samples is a constant I've already declared, it = 100. It's the random samples in each cell that get tested.

    This is what I've got; but it's not working?!?! Is it supposed to look like the last example you just posted?:-S

    //set column and row for loops
    for (int column = 0; column < gridSize-1; column++) { [/INDENT]for (int row = 0; row < gridSize-1; row++){
    //set hits to 0
    double hits = 0;
    for (double hits= 0; hits < 100; hits++){
    double x = minx + (column + Math.random()) * ((maxx-minx)/gridSize);
    double y = miny + (row + Math.random()) * ((maxy-miny)/gridSize);
    Last edited by hadenkl; 05-16-2011 at 05:51 AM. Reason: Indentation did not show correctly

  5. #5
    hadenkl is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    apologies for the abysmal copy and paste; as an afterthought, can you tell me how to post indentation in codes properly too, to save headaches? :-)

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    When you want to ask questions about your code you need to post the actual code. To make it readable place it between [ code ] and [ /code ] tags (without the spaces). Also post the EXACT error message you get. Do not paraphrase.

    There is no need to set all the values in the array to zero. When you create an array all values are set to the default value. For primitives (int, long, double etc) the default value is zero.

  7. #7
    hadenkl is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    Java Code:
    //set column and row for loops
    
    for (int column = 0; column < gridSize-1; column++) {
     
    [INDENT][/INDENT]for (int row = 0; row < gridSize-1; row++){
    
    [INDENT][INDENT][/INDENT][/INDENT]double hits = 0; //set hits to 0
    
    [INDENT][INDENT][/INDENT][/INDENT]for (double hits= 0; hits < 100 hits++){   [COLOR="Red"]- getting error here[/COLOR]
    
    [INDENT][INDENT][INDENT][/INDENT][/INDENT][/INDENT]double x = minx + (column + Math.random()) * ((maxx-minx)/gridSize);
    
    [INDENT][INDENT][INDENT][/INDENT][/INDENT][/INDENT]double y = miny + (row + Math.random()) * ((maxy-miny)/gridSize);
    My indentation isn't coming through correctly; basically everything after the second line is indented again, with 'x' and 'y' indented one more place over after that (last two lines of code). This isn't the whole code, but it's the bit that isn't compiling - everything else is working.

    I'm getting an 'array is already defined' error on the extra loop I've placed in there that relates exactly to the pseudocode I pasted above. I should also explain that I'm writing this based on a pseudocode that was given to me, not that I created. I know it's probably really simple, but I've been staring at it for so long I can't see the problem anymore. Help!!! :-) Thank you!!

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    You are declaring 2 variables with the same name: hits. You cannot do this in the same scope.

  9. #9
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    Java Code:
    for (double hits= 0; hits < 100 hits++){
    You are missing the ";" after the "100"

    Why are you using a "double" for a loop counter? Generally you would use an "int".

    Also, there is no need to define the loop variable separately since you are defining the variable inside the for loop.

Similar Threads

  1. JSlider change JPanel[][] rows and columns
    By Charlie161 in forum AWT / Swing
    Replies: 3
    Last Post: 02-28-2011, 05:44 PM
  2. How to create 3 columns and 2 rows tablelayout in Java
    By newbiejava in forum New To Java
    Replies: 5
    Last Post: 07-17-2010, 03:24 AM
  3. Need a loop for rows and columns
    By Ceasar in forum New To Java
    Replies: 4
    Last Post: 10-09-2009, 03:03 PM
  4. Counting Rows and Columns from Excel Sheet
    By Anish in forum Advanced Java
    Replies: 3
    Last Post: 12-24-2008, 06:56 AM
  5. Two diM aRRay and add rows and columns....
    By filly444 in forum New To Java
    Replies: 2
    Last Post: 08-30-2008, 05:24 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
  •