creating and assigning an object to an array in java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • boomba
    New Member
    • Apr 2008
    • 5

    creating and assigning an object to an array in java

    hi
    i have a 2 dimensional array where im trying to create an object (territory) in an if statement when the array is searched and when a certain number is found eg [0][0] a territory is created and placed there?

    so far i have writen a few lines but im stumped on creating the object? any help would be create thanks


    private Territory[][] createTerritori es()
    {
    final int _ = -1;
    final int[][] map = { { _, _, _, _, _, _, _, _, _, _, _, _, _, _ },
    { _, _, _, _, 0, 0, _, _, _, _, 2, _, _, _ },
    { 0, 0, 0, 0, 0, 0, _, 2, 2, 2, 2, 2, 2, 2 },
    { _, 0, 0, 0, _, _, _, 2, 2, 2, 2, 2, _, _ },
    { _, _, 0, 0, _, _, 2, _, 2, 2, 2, 2, _, _ },
    { _, _, 0, _, _, _, 3, 3, 2, 2, 2, _, _, _ },
    { _, _, _, 1, 1, _, 3, 3, 3, _, _, 4, _, _ },
    { _, _, _, 1, 1, _, _, 3, 3, _, _, 4, 4, _ },
    { _, _, _, 1, 1, _, _, 3, _, _, _, 4, 4, _ },
    { _, _, _, _, 1, _, _, _, _, _, _, _, _, _ } };
    Territory[][] territories = new Territory[TERRITORY_ROWS][TERRITORY_COLS];

    for (int row = 0; row < TERRITORY_ROWS; row++)
    {
    for (int col = 0; col < TERRITORY_COLS; col++)
    {
    if (int[row][col] map = 0;)
    ????
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by boomba
    hi
    i have a 2 dimensional array where im trying to create an object (territory) in an if statement when the array is searched and when a certain number is found eg [0][0] a territory is created and placed there?

    so far i have writen a few lines but im stumped on creating the object? any help would be create thanks


    private Territory[][] createTerritori es()
    {
    final int _ = -1;
    final int[][] map = { { _, _, _, _, _, _, _, _, _, _, _, _, _, _ },
    { _, _, _, _, 0, 0, _, _, _, _, 2, _, _, _ },
    { 0, 0, 0, 0, 0, 0, _, 2, 2, 2, 2, 2, 2, 2 },
    { _, 0, 0, 0, _, _, _, 2, 2, 2, 2, 2, _, _ },
    { _, _, 0, 0, _, _, 2, _, 2, 2, 2, 2, _, _ },
    { _, _, 0, _, _, _, 3, 3, 2, 2, 2, _, _, _ },
    { _, _, _, 1, 1, _, 3, 3, 3, _, _, 4, _, _ },
    { _, _, _, 1, 1, _, _, 3, 3, _, _, 4, 4, _ },
    { _, _, _, 1, 1, _, _, 3, _, _, _, 4, 4, _ },
    { _, _, _, _, 1, _, _, _, _, _, _, _, _, _ } };
    Territory[][] territories = new Territory[TERRITORY_ROWS][TERRITORY_COLS];

    for (int row = 0; row < TERRITORY_ROWS; row++)
    {
    for (int col = 0; col < TERRITORY_COLS; col++)
    {
    if (int[row][col] map = 0;)
    ????
    The syntax of that last if statment doesn't make any sense. Better use a switch
    statement here like this:

    [code=java]
    switch (map[row][col]) {
    case 0: territories[row][col]= ...; break;
    case 1: territories[row][col]= ...; break;
    ...
    }
    [/code]

    kind regards,

    Jos

    Comment

    Working...