0

I want to make a program where you can name a String("weapon" for example)and then add that String to a ArrayList. But without typing it yourself like:

MyArrayList.add(Egg); //for example

So that the new Object automatically add to the Arraylist.

So what I did, I created an Arraylist that will hold the weapon names. I made a method, where you can "make" in the main class a object with the weapon name.But how do i make something that when a object (in the main class)is created, it automatically add it self to the arraylist.

(Sorry for bad explaining, I'm from The Netherlands so... If it's a bad explaining please tell me so i can improve it)

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
kees mees
  • 11
  • 1
  • 1
  • 7
  • 1
    So you want to automatically keep a reference to all existing objects. My first question is why? – christopher Aug 19 '14 at 14:58
  • You could use a listener to handle that, which will execute *automatically* and may solve your problem. But before that, we should know more about why do you want to use this approach. – Luiggi Mendoza Aug 19 '14 at 14:59
  • There must be another way than doing that. Ask yourself, will you do that for every object you create ? – singe3 Aug 19 '14 at 15:01
  • 1
    Interesting, so far there are 3 answers, it seems that they are answering 3 different questions. so OP , could you make the question more clear? maybe some example will help? – Kent Aug 19 '14 at 15:03
  • Yes, try some pseudo code of how you would like to see it work. – Bohemian Aug 19 '14 at 15:40
  • I just want to know how to make it. But for example Battlefield 4, has lots of different guns.I want to make a ArrayList that will hold any gun name. So p90 for example(only the name not the weapon itself),if i write that name in setName or something, it automatically stores it in the ArrayList. And to be honest i have no idea how to make it, and I am a beginner so far I've learned how to make arraylist, add object and that stuff, making simple GUI's, polymorphisme, and how to use other Objects/Constructors. So please keep it to a simple answer ;) but thanks for your help. – kees mees Aug 20 '14 at 21:21

4 Answers4

0

Based on my interpretation of your problem, you want to have the user create a weapon name and add it to the ArrayList without having to manually add the code to add it?

A basic way to get String input from a user:

Scanner inputscan = new Scanner(System.in); //reads input from command line
String weapon = inputscan.nextLine(); //waits for user input
MyList.add(weapon);

That way, every time you call the "make" method with that code in it, it will prompt the user to type a weapon name, then the weapon name gets stored in the array.

elrobe
  • 555
  • 2
  • 16
0

Maybe I completely misunderstand it, but do you want to do something like this?

private ArrayList<YourObject> list;

public YourMainClass(){
    list = new ArrayList<YourObject>();
}

public void onAdd(String weaponName){
    list.add(new YourObject("weaponName")); // <- this being your issue
}

With YourObject being something like:

public class YourObject
{
    private String name;

    public YourObject(String n){
        setName(n);
    }

    public void setName(String n){
        // Perhaps an if-else to check if n isn't null, nor emtpy?
        name = n;
    }
    public String getName(){
        return name;
    }
}
Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
-1

I think you want to initialize the List with an object in it:

Try using an instance block, like this:

List<String> list = new ArrayList<String>() {{
  add("Egg");
}};
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • That doesn't sound like what the OP described, and that will retain a reference to the surrounding class, which can be problematic, and that is inferior to simply `new ArrayList(Arrays.asList("Egg"))` – David Conrad Aug 19 '14 at 15:03
  • That's not what is asked – singe3 Aug 19 '14 at 15:04
-2

Add the command to add the object to the collection in the constructor.( But this ill advise)

You can create an auxiliary class that will create that object and label to the collection.

class  WeaponFactory
{
  Collection c;
  public WeaponFactory(Collection coll){c=coll;}
  public Weapon makeWeapon(String text)   // Makes sense when the object is not String , like a Weapon class which also contains stats or something
  {
   Weapon w = new Weapon(text)
   c.add(w);
   return w;
  }
}


class Weapon
{
  String name;
  public Weapon(String text)
  {
    name = text;
  }
}
user3224416
  • 522
  • 5
  • 15
  • You shouldn't use this as a param in a constructor. I believe this is a bad practice: http://stackoverflow.com/questions/3921616/java-leaking-this-in-constructor/23069096#23069096 http://www.javapractices.com/topic/TopicAction.do?Id=252r – Michał Schielmann Aug 19 '14 at 15:12
  • Thanks, I cannot edit my previous comment, so here it is: javapractices.com/topic/TopicAction.do?Id=252 – Michał Schielmann Aug 19 '14 at 15:18