2D Array population and possible Access Control

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Lost Prophet
    New Member
    • Mar 2008
    • 14

    2D Array population and possible Access Control

    Hi

    I have the below code which is to generate a 2D Array... The problem is that I need to populate it by part of array... not by complete array.

    For example...
    map[1][0] = 1;
    map[1][1] = 1;
    map[1][2] = 1;

    map[3][5] = 2;
    map[3][5] = 2;
    map[3][5] = 2;

    map[6][5] = 3;
    map[6][6] = 3;
    map[5][5] = 3;

    Whenever I am trying to populate the individual element (or any element for that matter), an error is returned saying 'Incompatible types - found int but expected Map'. I know it has something to do with the constructor but I need this as I need the array to be widely available to a number of classes

    All the other values can be the default 0 (this would make things easy!). The idea behind it is I need to create a 'map' where the array elements represent a group of individuals and Access Control requests are passed around the determine if the group of individuals can or cannot move to. Essentially its a game like battleships, but more complex

    Code:
    class Map
    {
        private Map[][] map;
        
        public Map()
        {
            Map map[][] = new Map[8][10];
        }
        
        public void populateMap(Map[][] map)
        {
            map[1][0] = 1;
        }
    
        public Map getMapData(int x, int y)
        {    
            return map[x][y];  
        }
    }
    Thanks
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Your 'map' can store 'Map's (note the lower and uppercase) so you can't put an
    int in there. Make up your mind of what you want to store in every element of
    your two dimensional array.

    kind regards,

    Jos

    Comment

    • Lost Prophet
      New Member
      • Mar 2008
      • 14

      #3
      Originally posted by JosAH
      Your 'map' can store 'Map's (note the lower and uppercase) so you can't put an
      int in there. Make up your mind of what you want to store in every element of
      your two dimensional array.

      kind regards,

      Jos
      All I need in every element is a 1, 2, 3 or 4 so an int but im confused by the above. I used notation from a past example and think that I have got the wrong idea from it

      Whenever I try certain things I get different errors so im confused

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by Lost Prophet
        All I need in every element is a 1, 2, 3 or 4 so an int but im confused by the above. I used notation from a past example and think that I have got the wrong idea from it

        Whenever I try certain things I get different errors so im confused
        Why don't you simply make your map an array of ints (or better: bytes) then:

        [code=java]
        // construct the matrix:
        int[][] map= new int[8][10];
        ...
        // put a value in the map:
        map[3][4]= 1;
        [/code]

        Read up on arrays in a tutorial.

        kind regards,

        Jos

        Comment

        • Lost Prophet
          New Member
          • Mar 2008
          • 14

          #5
          Originally posted by JosAH
          Why don't you simply make your map an array of ints (or better: bytes) then:

          [code=java]
          // construct the matrix:
          int[][] map= new int[8][10];
          ...
          // put a value in the map:
          map[3][4]= 1;
          [/code]

          Read up on arrays in a tutorial.

          kind regards,

          Jos
          I did this but then the array didnt become available to other methods or classes so thought it would need the constructor or some kind of return value. I'll try again anyway and reply to this if I have any problems with it again

          Comment

          • Lost Prophet
            New Member
            • Mar 2008
            • 14

            #6
            Two classes below to test the passing of the array from one class to another, its all working but the array in ComplexScript comes out null (so a nullPointerExce ption. This is furthest I have got with it but dont understand why its doing it as I have set the code up in the exact same way that the code was in the workbench that I produced (and that worked). Any pointers?

            Code:
            class ComplexScript
            {
                public ComplexScript script;
                public int intMap[][];
                
                public static void main(String args[]) 
                {           
                    ComplexScript test = new ComplexScript();
                    test.run();
                }
                
                public void run()
                {            
                        
                    //set up thread for initial explosion
                    Runnable initialExplosion = new InitialExplosion(this);
                    Thread t1 = new Thread(initialExplosion);
                }
                
                public void print()
                {       
                    for (int y = 0;y<10;y++)
                    {
                        for (int x=0 ; x<8 ; x++)
                        {
                            //map[x][y]=10*x+y;
                            //map[x][y] = 0;
            
                            //System.out.print("( "+x+","+y+")  ");
                            System.out.print("  " + intMap[x][y] + "   ");
                        }
                    }      
                }
            }

            Code:
            /**
             * Write a description of class array3 here.
             * 
             * @author (your name) 
             * @version (a version number or a date)
             */
            public class InitialExplosion implements Runnable
            {
                private ComplexScript script;
              
                public InitialExplosion(ComplexScript c)
                {
                    script = c;
                    run();
                }
                
                public void run()
                {
                    populate();   
                }
                
                public void populate()
                {        
                    int intMap[][] = new int[8][10];
                    //Location of team1
                    intMap[0][0] = 1;
                    intMap[0][1] = 1;
                    intMap[0][2] = 1;
                    intMap[1][0] = 1;
                    intMap[1][1] = 1;
                    intMap[2][0] = 1;
                            
                    //Location of team2
                    intMap[0][7] = 2;
                    intMap[0][8] = 2;
                    intMap[0][9] = 2;
                    intMap[1][8] = 2;
                    intMap[1][9] = 2;
                    intMap[2][9] = 2;
                            
                    //Location of team3
                    intMap[7][7] = 3;
                    intMap[7][8] = 3;
                    intMap[7][9] = 3;
                    intMap[6][8] = 3;
                    intMap[6][9] = 3;
                    intMap[5][9] = 3;
                            
                    //Location of Civilian team
                    intMap[5][4] = 4;
                    intMap[2][5] = 4;
                    intMap[7][4] = 4;
                    intMap[7][3] = 4;
                    intMap[4][4] = 4;
                    intMap[7][0] = 4;
                    intMap[3][2] = 4;
                    
                    script.print();
                }
            
            }

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              When you declare an identifier think of it that you have the remote control
              to a television but you haven't got the television yet. You have to explicitly new
              the TV set so your remote can control/manipulate it. So:

              [code=java]
              int[][] map; // you have the remote now
              map= new int[8][10]; // and now you have the tv as well.
              [/code]

              You can shorten this a bit to one step:

              [code=java]
              int[][] map= new int[8][10]; // a remote and a tv
              [/code]

              If you don't new a television then all you have is a remote and if you want
              to do something with it you get this NullPointerExce ption.

              kind regards,

              Jos

              Comment

              • Lost Prophet
                New Member
                • Mar 2008
                • 14

                #8
                Oh sorry. The error was in a different part. I put print statements in other areas and all works fine but in the ComplexScript class the NullPointer is in...

                Code:
                System.out.print("  " + intMap[x][y] + "   ");
                Thanks for the help and suggestion though :-D

                Comment

                Working...