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 - 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
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
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
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: -
// construct the matrix:
-
int[][] map= new int[8][10];
-
...
-
// put a value in the map:
-
map[3][4]= 1;
-
Read up on arrays in a tutorial.
kind regards,
Jos
Why don't you simply make your map an array of ints (or better: bytes) then: -
// construct the matrix:
-
int[][] map= new int[8][10];
-
...
-
// put a value in the map:
-
map[3][4]= 1;
-
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
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? - 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] + " ");
-
}
-
}
-
}
-
}
- /**
-
* 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();
-
}
-
-
}
-
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: -
int[][] map; // you have the remote now
-
map= new int[8][10]; // and now you have the tv as well.
-
You can shorten this a bit to one step: -
int[][] map= new int[8][10]; // a remote and a tv
-
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
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... - System.out.print(" " + intMap[x][y] + " ");
Thanks for the help and suggestion though :-D
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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
|
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>
|
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;
| |
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...
|
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
|
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...
|
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...
|
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...
|
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. ...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |