3 Replies - 171 Views - Last Post: 08 February 2012 - 12:37 PM Rate Topic: -----

Topic Sponsor:

#1 metalfacedev  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-February 12

Moving an Object strored in an Array to another Array.

Posted 08 February 2012 - 07:51 AM

okay so heres the deal. I have an object created to resemble a room in which i've created an array to store Animal objects within. Now in this program there will be several rooms and the Animal objects must be able to move between these rooms when a certain condition arises. What sort of algorithm should i be thinking about implementing into this?

To be clear I want the Animal objects to be able to remove themselves from the Array stored within their current Room object and be able to append themselves to another existing array in another existing Room object.any ideas?

Is This A Good Question/Topic? 0
  • +

Replies To: Moving an Object strored in an Array to another Array.

#2 SwiftStriker00  Icon User is offline

  • Microsoft Insider
  • member icon

Reputation: 299
  • View blog
  • Posts: 1,337
  • Joined: 25-December 08

Re: Moving an Object strored in an Array to another Array.

Posted 08 February 2012 - 08:29 AM

There is no "algorithm" for this problem, it is all dependent on how you write your code. You stated the requirments for a animal to move from room to room. It leaves the array of the first room and enters the array of the next. So we need to define a move() for your Room object.

public void moveTo( Animal a, Room r ){
  //find a in this.animalArray
  //remove a from this.animalArray
  //add to r.animalArray
}

Was This Post Helpful? 0
  • +
  • -

#3 metalfacedev  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-February 12

Re: Moving an Object strored in an Array to another Array.

Posted 08 February 2012 - 12:28 PM

good stuff but i guess my next question is finding Animal a within the array. how can i write something that automatically finds what index the Animal is in within the array?
Was This Post Helpful? 0
  • +
  • -

#4 SwiftStriker00  Icon User is offline

  • Microsoft Insider
  • member icon

Reputation: 299
  • View blog
  • Posts: 1,337
  • Joined: 25-December 08

Re: Moving an Object strored in an Array to another Array.

Posted 08 February 2012 - 12:37 PM

That varies by implementation. The most basic approach would be to loop through array and check each index until you find the one you wanted. However, think about what happens when you remove the object

if you have a basic array like this Animal[] animals, that will most likely mean you will be setting that index to null, which could cause problems unless you know how to handle it. If your allowed to use ArrayList, you will find other tools to help you search the array, and handle adding and removing much easier
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1