473,397 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

creating and assigning an object to an array in java

5
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[][] createTerritories()
{
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;)
????
May 20 '08 #1
1 1527
JosAH
11,448 Expert 8TB
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[][] createTerritories()
{
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:

Expand|Select|Wrap|Line Numbers
  1. switch (map[row][col]) {
  2.    case 0: territories[row][col]= ...; break;
  3.    case 1: territories[row][col]= ...; break;
  4.    ...
  5. }
  6.  
kind regards,

Jos
May 20 '08 #2

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

Similar topics

7
by: Sree | last post by:
If class B derives from class A and we create an array of class B like B *arrayOfB = new B; and // Instance of A A firstInstanceOfA; How can we store firstInstanceOfA in arrayOfB ? I tried the...
5
by: Brett Kelly | last post by:
Sorry, for the somewhat confusing subject, but my problem is equally odd (imo). So, I have an assembly that contains an abstract class (File) and 5 other classes that all subclass it (Exe, Dll,...
0
by: Dan A | last post by:
I have a SOAP written in Java, and being served by Apache Axis - using the wrapped mode. I then used VS 2003 to generate C# client stubs. I have many complex types that are returned - and these...
2
by: ryoung | last post by:
I receive the following error when I attempt to assign values to a web service array. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. ...
11
by: AZRebelCowgirl73 | last post by:
here is the instructiions I am to use Ask the user to input the number of effective cars in the shop (this number should be between 1 and 100, inclusively). In a loop, controlled by the inputted...
6
sammyboy78
by: sammyboy78 | last post by:
I'm trying to display my array of objects in a GUI. How do I get JLabel to refer to the data in my objects? I've read my textbook and some tutorials online I just cannot get this. Plus all the...
8
by: getmeidea | last post by:
Hi, I am using JDK 1.5. I have a program like this. Here i am directly assigning value to one object. It does'nt give me any compile time or run time error. In java we dont have access to any...
37
by: miken32 | last post by:
In PHP, if a function returns an array it's fairly common to capture its return values like this: <?php list($foo, $bar, $baz) = some_function_that_return_an_array(); ?> In Javascript, would...
1
by: stevedub | last post by:
I am having some trouble configuring my array to read from a sequential file, and then calling on that to fill an array of interests. I think I have the class set up to read the file, but when I run...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
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,...

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.