2

Arraylists use the following syntax: ArrayList<Object> = new ArrayList<Object>; I need to add a constructor to my GameList class that allows you to specify what type of list to create. I don't understand how to make my class capable of being defined like this:

GameList<objectType> = new GameList();

All of my objects in the game will descend from the gameobject class.

public class GameObject
{
    String name;
    public GameObject
    {
        name = "Stat";
    }
    public String getName()
    {
        return name;
    }
    public void setName(String newName)
    {
        name = newName;
    }
}

public class GameList
{
    GameObject[] theList;
    public GameList(int size)
    {
        theList = new GameObject[size];
    }
    public GameObject parseList(String objectName)
    {
        for(int i = 0; i < theList.length; i++)
            if(theList[i].getName() == objectName)
                return theList[i];
        return null;
    }
}
6
  • 2
    Do you really need to create your own custom list? It looks more like you'd want a HashMap, if you're going to fetch objects by their name. Commented Jul 23, 2013 at 13:28
  • I'm sure your correct but I'm not familiar with a hashMap I just figured I would make my own class for the matter. Can you tell me more about them? Commented Jul 23, 2013 at 13:31
  • Maps allow you to associate keys to values. Since you're using objectName as a key, you don't want a List type data structure. You can simply do map.put("someName", myObject) and map.get("someName"); Implementing the basic data structures yourself is good for practice, but they have been implemented already in the language. Commented Jul 23, 2013 at 13:34
  • Awesome! Thanks for that information! Being a programmer is about the "Right kind of lazy", and I definitely need more experience using the data structures that already exist. Commented Jul 23, 2013 at 13:37
  • How does this syntax look to you? Map myMap = new Map(GameObject myObject, String objectName); Commented Jul 23, 2013 at 13:43

1 Answer 1

5

What you're looking for is Generics. The syntax would be

public class GameList<T> {
    T[] theList;
    ...
Sign up to request clarification or add additional context in comments.

4 Comments

Interesting. I have more experience with C++ so I learned this as "templating". Now I know how to template in java also! Thanks!
How will the class create a new T[], similar to the OP's constructor GameList(int size)?
One way would be to not type it as a T, but instead as an Object[] (this is how ArrayList does it). Another way is to use a Class object passed in to create the array via reflection (this is more cumbersome).
Agreed, it would be simplest and easiest to understand to replace the T[] field declaration in the answer with Object[], and to create an Object[] in the constructor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.