473,513 Members | 3,621 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

2D Array population and possible Access Control

14 New Member
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

Expand|Select|Wrap|Line Numbers
  1. class Map
  2. {
  3.     private Map[][] map;
  4.  
  5.     public Map()
  6.     {
  7.         Map map[][] = new Map[8][10];
  8.     }
  9.  
  10.     public void populateMap(Map[][] map)
  11.     {
  12.         map[1][0] = 1;
  13.     }
  14.  
  15.     public Map getMapData(int x, int y)
  16.     {    
  17.         return map[x][y];  
  18.     }
  19. }
Thanks
Apr 2 '08 #1
7 1841
JosAH
11,448 Recognized Expert MVP
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
Apr 2 '08 #2
Lost Prophet
14 New Member
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
Apr 2 '08 #3
JosAH
11,448 Recognized Expert MVP
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:

Expand|Select|Wrap|Line Numbers
  1. // construct the matrix:
  2. int[][] map= new int[8][10];
  3. ...
  4. // put a value in the map:
  5. map[3][4]= 1;
  6.  
Read up on arrays in a tutorial.

kind regards,

Jos
Apr 2 '08 #4
Lost Prophet
14 New Member
Why don't you simply make your map an array of ints (or better: bytes) then:

Expand|Select|Wrap|Line Numbers
  1. // construct the matrix:
  2. int[][] map= new int[8][10];
  3. ...
  4. // put a value in the map:
  5. map[3][4]= 1;
  6.  
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
Apr 2 '08 #5
Lost Prophet
14 New Member
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 nullPointerException. 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?

Expand|Select|Wrap|Line Numbers
  1. class ComplexScript
  2. {
  3.     public ComplexScript script;
  4.     public int intMap[][];
  5.  
  6.     public static void main(String args[]) 
  7.     {           
  8.         ComplexScript test = new ComplexScript();
  9.         test.run();
  10.     }
  11.  
  12.     public void run()
  13.     {            
  14.  
  15.         //set up thread for initial explosion
  16.         Runnable initialExplosion = new InitialExplosion(this);
  17.         Thread t1 = new Thread(initialExplosion);
  18.     }
  19.  
  20.     public void print()
  21.     {       
  22.         for (int y = 0;y<10;y++)
  23.         {
  24.             for (int x=0 ; x<8 ; x++)
  25.             {
  26.                 //map[x][y]=10*x+y;
  27.                 //map[x][y] = 0;
  28.  
  29.                 //System.out.print("( "+x+","+y+")  ");
  30.                 System.out.print("  " + intMap[x][y] + "   ");
  31.             }
  32.         }      
  33.     }
  34. }

Expand|Select|Wrap|Line Numbers
  1. /**
  2.  * Write a description of class array3 here.
  3.  * 
  4.  * @author (your name) 
  5.  * @version (a version number or a date)
  6.  */
  7. public class InitialExplosion implements Runnable
  8. {
  9.     private ComplexScript script;
  10.  
  11.     public InitialExplosion(ComplexScript c)
  12.     {
  13.         script = c;
  14.         run();
  15.     }
  16.  
  17.     public void run()
  18.     {
  19.         populate();   
  20.     }
  21.  
  22.     public void populate()
  23.     {        
  24.         int intMap[][] = new int[8][10];
  25.         //Location of team1
  26.         intMap[0][0] = 1;
  27.         intMap[0][1] = 1;
  28.         intMap[0][2] = 1;
  29.         intMap[1][0] = 1;
  30.         intMap[1][1] = 1;
  31.         intMap[2][0] = 1;
  32.  
  33.         //Location of team2
  34.         intMap[0][7] = 2;
  35.         intMap[0][8] = 2;
  36.         intMap[0][9] = 2;
  37.         intMap[1][8] = 2;
  38.         intMap[1][9] = 2;
  39.         intMap[2][9] = 2;
  40.  
  41.         //Location of team3
  42.         intMap[7][7] = 3;
  43.         intMap[7][8] = 3;
  44.         intMap[7][9] = 3;
  45.         intMap[6][8] = 3;
  46.         intMap[6][9] = 3;
  47.         intMap[5][9] = 3;
  48.  
  49.         //Location of Civilian team
  50.         intMap[5][4] = 4;
  51.         intMap[2][5] = 4;
  52.         intMap[7][4] = 4;
  53.         intMap[7][3] = 4;
  54.         intMap[4][4] = 4;
  55.         intMap[7][0] = 4;
  56.         intMap[3][2] = 4;
  57.  
  58.         script.print();
  59.     }
  60.  
  61. }
  62.  
Apr 2 '08 #6
JosAH
11,448 Recognized Expert MVP
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:

Expand|Select|Wrap|Line Numbers
  1. int[][] map; // you have the remote now
  2. map= new int[8][10]; // and now you have the tv as well.
  3.  
You can shorten this a bit to one step:

Expand|Select|Wrap|Line Numbers
  1. int[][] map= new int[8][10]; // a remote and a tv
  2.  
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 NullPointerException.

kind regards,

Jos
Apr 2 '08 #7
Lost Prophet
14 New Member
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...

Expand|Select|Wrap|Line Numbers
  1. System.out.print("  " + intMap[x][y] + "   ");
Thanks for the help and suggestion though :-D
Apr 2 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

4
4337
by: MS | last post by:
Hi, In my Genetic Algorithm program, I have a class called Genome. Another class called GA has a class variable called 'population' which is an array of type Genome. One of the methods in GA, called CreateNextGeneration(), creates a local array of class Genome called 'nextGeneration', and at the end of that method I want the class...
22
4573
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last month I had enough of extra proof that the cat doesn't hount mice anymore in more and more situations. And the surrent sicretisme among array and...
10
2400
by: Tom | last post by:
Hi I am looking for an optimal data-structure in order to replace a map<int,float>. I use it as some kind of sparse array representation. The facts: - the population in the data-structures can be bigger than 10^6 - the maximum number of elements (upper bound) is known and fixed - the key is an integer, the element is a float value
46
3202
by: RoSsIaCrIiLoIA | last post by:
Write a function that gets an array of unsigned int fill it with random values all differents, and sorts it. It should be faster than qsort too. Do you like my solution? _______________________ #include <stdio.h> #include <stdlib.h> #include <time.h>
5
3859
by: Diffident | last post by:
Hello All, I have a 2-dimensional array that I am storing as a session variable. I have no idea on how I can cast the session variable back to 2-dimensional array. Any pointers? Reference code below... Array declaration: DateTime DateRangesForDataLists = new DateTime;
5
5792
by: Paul | last post by:
Hi, I am a self taught VBA programmer, and I'm trying to learn VB2005 Express (The price was right). I like the look of the treeview control, and I'd like to use it as a menu system for my users, the options they are allowed to see are all different and specified in a MSACCESS table. Can I populate a Treeview directly from my MSAccess...
3
3521
by: Ron | last post by:
Hi, If it's possible, a quick example would REALLY help. I'm clueless which makes me boxless as well. ::grin:: TIA ron
0
1385
by: guerrerofarias | last post by:
I have a "population" of objects that is suppose to do a lot of stuff during its lifecycle... I had everything working fine (not fancy code, though) with an array, but now I've received instructions to let the population change size over generations. This means my array no longer works...I want to change it to a vector (or map), keeping the basic...
14
16779
by: neonman14 | last post by:
Hello I am in Intro to Java Programming and I am having problems with assignment. The Homework assignment is called Population. Population Write a program that will predict the size of a population of organisms. The program should ask for the starting number of organisms, their average daily population increase (as a percentage), and the...
0
7269
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7394
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7123
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5701
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3248
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3237
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1611
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
470
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.