List vs Object Array?

C

CSharp-Jay

So I am currently programming an RPG video game for a fun side
project. And originally I had intended the character's inventory to
be an object array. But a friend of mine suggested using a list
instead. I am only about an intermediate programmer and I haven't
worked with lists before. I was hoping somebody could tell me what
the differences and pros/cons are of using a List vs Object Array for
my intended use.
 
R

Roger Frost

First of all, I wouldn't use an object[] per say, your inventory items
should inherit a common base class so you can avoid constantly casting from
object to whatever which is very expensive. Depending on the difficulty of
the game now and later, you should probably consider a little interface
structure in there as well.

And I agree with your friend, but as for your original question...have you
even attempted "List vs. Object Array" in google yet? Lets do that first,
then lets ask specific questions about anything that we may not understand.
:)

No matter which route you take, it's time step up to the plate and learn
about some generic collections. Seriously, you will be thankful that you
did.
 
C

CSharp-Jay

First of all, I wouldn't use an object[] per say, your inventory items
should inherit a common base class so you can avoid constantly casting from
object to whatever which is very expensive.  Depending on the difficulty of
the game now and later, you should probably consider a little interface
structure in there as well.

And I agree with your friend, but as for your original question...have you
even attempted "List vs. Object Array" in google yet?  Lets do that first,
then lets ask specific questions about anything that we may not understand.
:)

No matter which route you take, it's time step up to the plate and learn
about some generic collections.  Seriously, you will be thankful that you
did.

--
Roger Frost
Logic Is Syntax Independent


So I am currently programming an RPG video game for a fun side
project.  And originally I had intended the character's inventory to
be an object array.  But a friend of mine suggested using a list
instead.  I am only about an intermediate programmer and I haven't
worked with lists before.  I was hoping somebody could tell me what
the differences and pros/cons are of using a List vs Object Array for
my intended use.

Thanks for the response Roger. And for the record I did & always
check Google before posting here.
 
P

Peter Duniho

CSharp-Jay said:
Thanks for the response Roger. And for the record I did & always
check Google before posting here.

If you could clarify what about the information you've already found you
already understand, versus what you are looking for more detail on, that
would be very helpful in allowing those of us answering your question to
focus on the information you really need.

The question you asked is fairly broad, and the _basic_ aspects of the
answer are well-documented, so you should easily be able to find those
basics. What beyond the basics are you looking for?

Pete
 
R

Roger Frost

In addition to what Peter said: Overly-generalized "pros/cons" questions
always sound like homework. :)

-Roger
 
S

Sharon

I would: List<InventoryItem> _inventory = new List<InventoryItem>.
Don't know about performance but I don't think it matters,
It's not likely that a character will have more than 10 - 20 items in the
inventory.
Sharon.
 
R

Roger Frost

It compiles but I didn't test it, there are probably some logic errors in
there.
It's a start though...you will either use Big Gun as a club or check to make
sure
there is ammo for it...etc.

using System;
//using System.Collections.Generic;
using System.ComponentModel;

namespace RPG.CharacterInventory
{
public class BindableInventoryItemCollection :
BindingList<IInventoryItem>
{
public IWeapon GetMostPowerfulWeapon()
{
int power = 0;
IWeapon mostPowerful = null;

foreach (IInventoryItem item in this)
{
if (item is IWeapon)
{
IWeapon weapon = item as IWeapon;

if (weapon.Power > power)
{
power = weapon.Power;
mostPowerful = weapon;
}
}
}

return mostPowerful;
}

public int GetInventoryValue()
{
int totalValue = 0;

foreach (IInventoryItem item in this)
{
totalValue += item.Value;
}

return totalValue;
}

public new bool Add(IInventoryItem item)
{
bool hasRequiredItem = false;

if (item.DependentUpon == null)
hasRequiredItem = true;
else if (this.ContainsType(item.DependentUpon) > 0)
hasRequiredItem = true;

if (hasRequiredItem)
{
base.Add(item);
item.ParentCollection = this;
}

return hasRequiredItem;
}

public new bool Remove(IInventoryItem item)
{
if (!this.Contains(item))
throw new InvalidOperationException(
"The item is not present in the collection"
);

foreach (IInventoryItem _item in this)
{
if (_item.DependentUpon == item.GetType() &&
(this.ContainsType(item.GetType()) <= 1)
)
throw new InvalidOperationException(
"Cannot remove. Item " + _item.Name + " depends on
" + item.Name
);
}

return base.Remove(item);
}

public new bool RemoveAt(int index)
{
return this.Remove(this[index]);
}

public int ContainsType(Type itemType)
{
int count = 0;

foreach (IInventoryItem item in this)
{
if (item.GetType() == itemType)
{
count++;
}
}

return count;
}
}

public interface IInventoryItem
{
string Name { get; }
int Value { get; }
Type DependentUpon { get; }
bool GetIsUsable();
BindableInventoryItemCollection ParentCollection { get; set; }
}

public interface IWeapon : IInventoryItem
{
int Power { get; }
}

public class InventoryItemBase : IInventoryItem
{
public string Name { get; protected set; }
public int Value { get; protected set; }
public int Power { get; protected set; }
public Type DependentUpon { get; protected set; }
public BindableInventoryItemCollection ParentCollection { get;
set; }

public bool GetIsUsable()
{
if (this.DependentUpon == null)
{
return true;
}
else if (this.ParentCollection != null &&
(this.ParentCollection.ContainsType(this.DependentUpon) >
0))
{
return true;
}

return false;
}
}

public class Axe : InventoryItemBase
{
public Axe()
{
this.Name = "Axe";
this.Value = 5;
this.Power = 5;
this.DependentUpon = null;
}
}

public class BigGun : InventoryItemBase
{
public BigGun()
{
this.Name = "Big Gun";
this.Value = 25;
this.Power = 0;
this.DependentUpon = null;
}
}

public class BigBullet : InventoryItemBase
{
public BigBullet()
{
this.Name = "Big Bullet";
this.Value = 1;
this.Power = 10;
this.DependentUpon = typeof(BigGun);
}
}
}
 
R

Roger Frost

Hi Sharon,

I've been thinking about this...and I believe how often a character uses an
item is just as important as how many it has.

Hmmm...what say you?
 
S

Sharon

3 parameters you should consider:
1. how many items the collection will contain.
2. how often will the collection be written to.
3. how often the collection will be read from.

Different types of collections behave differently to these parameters.
"how often a character uses an item" refers to the 3rd. parameter.
But as I said, if you have 1 or several characters each holding several
items,
It doesn't really matter.
I would go on with development and handle performance issues as they occur.
They might occur in places you didn't expect.
Sharon.
 
P

Peter Duniho

Sharon said:
3 parameters you should consider:
1. how many items the collection will contain.
2. how often will the collection be written to.
3. how often the collection will be read from.

Actually, the most important parameter is missing from the above list:
how an element of the collection is identified.

If elements can easily be identified by index, or do not need to be
accessed in a random way at all, then a List<T> or other indexed or
linear collection is most appropriate. If, on the other hand, elements
are represented by some non-linear key, a Dictionary<TKey, TValue> is
more appropriate.

Occasionally, other considerations (memory, portability, context)
outweigh the basic maintenance considerations, and then alternatives to
the above should be explored. But generally, the semantics of the
access wins over implementation-detail aspects, such as those you list.

Pete
 
V

vanderghast

With a list you can remove/insert elements (that appear to be) in the middle
of the sequence. You have also nice pre-defined methods,
http://msdn.microsoft.com/en-us/library/d9hw1as6.aspx, including ToArray. I
don't use Array much. Even for coordinates, as example, I prefer a list of a
structure (with fields .x, .y, .z) rather than an array with two indices
(the second running from 0 to 2, 0 for x, 1 for y and 2 for z) because the
code become definitively more self documented, which is, in my opinion, more
important than a hard to see speed difference (if any).

Vanderghast, Access MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top