0

Suppose I have a class. It is called Item.

public class Item {

public boolean usable = false;
protected boolean usage;    
public int id;
public String name;
public String type;
public int stacknum;
protected int tier;
public String rarity;
boolean equipable;
boolean equiped;
String altName;


public Item(int idIn, String nameIn, String typeIn) {

    usage = false;
    id = idIn;
    name = nameIn;
    type = typeIn;
    stacknum = 0;
    tier = 0;
    rarity = "Common";

}//end of constructor
}//end of class

Lets say I have an array called:

Inventory = new Item[5];

It contains these elements:

Item elementOne = new Item(1, "Element One", "Array Element");
Item elementTwo = new Item(2, "Element Two", "Array Element");

etc.

Inventory[0] = elementOne;
Inventory[1] = elementTwo;
Inventory[2] = elementThree;

and so forth. How would I go about writing a method to find out which element in array an Item(or anything in general) is I.e.

elementOne.findPlace

would return the int value of 0.

thanks!

CCD
  • 470
  • 1
  • 7
  • 19
  • Why not override `equals(Object o)` and use a `List` with `contains(T item)` or `indexOf(T item)` ? – NiziL Aug 22 '13 at 06:27
  • I don't fully understand what you mean. Can you please explain? – CCD Aug 22 '13 at 18:44
  • There are some useful method in [`List`](http://docs.oracle.com/javase/7/docs/api/java/util/List.html) which do what you want (i.e. `indexOf(I item)`). You can use it if you implements the `equals(Object o)` method in your class `Item`. Am I understandable ? – NiziL Aug 23 '13 at 06:22
  • Yes, that makes more sense, thank you – CCD Aug 23 '13 at 21:21

3 Answers3

3

You might not be able to do so in this case, due to the scope of the array and the fact that the class is not aware of its surroundings.

use a list of objects, and use:

myList.indexOf(item)

to get an int index.

The Item class should also include an equals( method.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
  • +1. Similar thing can be done with an array, too. But the point is you need to go through the container, as the element itself is "not aware of its surroundings". – Thilo Aug 22 '13 at 06:33
  • and need to provide `Item.equals()` method – Nandkumar Tekale Aug 22 '13 at 06:35
  • I'm sorry, I dont't fully understand what you mean when you say "go through the container. Also can you please provide an example of the Item.equals() method? – CCD Aug 22 '13 at 18:43
0

You can do this with arrays, but it can be errorprone and so requires alot of defensive coding. In class Item: public int getID(){return this.id;}

int index = Inventory[element.getID()-1];

If the element is not in the inventory an error will be thrown. It is better to use a list but if you insist on arrays.

public static int getIndexOfItem(Item item, Item[] inventory){
  if(inventory == null || item == null)
    return -1;
  if (item.getID()-1 > inventory.length)
    return -1;
  return inventory[item.getID()-1];
}
arynaq
  • 6,710
  • 9
  • 44
  • 74
0

How would I go about writing a method to find out which element in array an Item (or anything in general) is

Based on this quoted part of the question, please consider the following answer:

Item result= null ;
for(Item e: Inventory) {
    if( <whatever-your-search-condition-is> ) {
        result= e ;
        break;
    }
}
if( result != null )
    <found>
else
    <not found>

A more general solution:

List<Item> result= new LinkedList<Item>() ;
for(Item e: Inventory) {
    if( <whatever-your-search-condition-is> )
        result.add( e );
}
if( result.size() > 0 )
    < result.size() found >
else
    < none found >

Note: this code works both for Inventory being an array, a List, or in general a Collection.

  • Thank you, I will look into using lists as this is a new concept to me and seems to be a popular solution. – CCD Aug 22 '13 at 18:49