Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Creating an object dynamically in an ArrayList or LinkedList?

  • 30-04-2008 1:27pm
    #1
    Closed Accounts Posts: 20,759 ✭✭✭✭


    I have a scenario.

    I've created a class. I want to create an object dynamically in an ArrayList or LinkedList, so I don't have to manually create objects.

    For example. Let's create a person. We'll pretend I've created a Person class with set and get methods for the various attributes. Now normally, I'd create the object, set the attributes and then add that object to a LinkedList.
    Person a = new Person();
    System.out.print("First name: ");
    a.setFirstName();
    System.out.print("Last name: ");
    a.setSurname();
    System.out.print("Age: ");
    a.setAge();
    LinkedList<Person> people = new LinkedList<Person>()
    people.addLast(a);

    But I find this to be too manual - so ideally, what I want to do is to create the object instance as I'm tranversing the ArrayList or LinkedList.

    Ideas? :)


Comments

  • Closed Accounts Posts: 413 ✭✭sobriquet


    I'm not quite sure what you want to do. Is it the case that you want to add Persons as you go? Well isn't that just something like (C# 1.1):
    ArrayList people = new ArrayList();
    while (havenewpeople == true) {
        people.Add(new Person("joe", "bloggs", 30));
    }
    
    All that'll give you of course is n Person objects all of the same name and age. Modify to take in values from whatever input vector (user, list of them etc), and you'll need to have an appropriate constructor for your Person object.

    Otherwise, it sounds somewhat like you're describing a generator, though I'm not sure how it'd apply in such a circumstance.

    No idea whether you're a beginner or not so apologies if the above is too obvious.


  • Closed Accounts Posts: 20,759 ✭✭✭✭dlofnep


    Tis grand, I got it. I just wasn't referencing the object correctly.
    people.add(new Person());
    Person.setFirstName();
    Person.setSurname();
    Person.setAge();
    
    That should do it :)


  • Registered Users, Registered Users 2 Posts: 2,013 ✭✭✭lynchie


    dlofnep wrote: »
    Tis grand, I got it. I just wasn't referencing the object correctly.
    people.add(new Person());
    Person.setFirstName();
    Person.setSurname();
    Person.setAge();
    
    That should do it :)

    Your making static calls against Person. Dont see how that would work? You should be calling the setter methods on the object instance. Also, your setFirstName() method takes no arguments? How do you intend to set this property?


  • Closed Accounts Posts: 20,759 ✭✭✭✭dlofnep


    lynchie wrote: »
    Your making static calls against Person. Dont see how that would work? You should be calling the setter methods on the object instance. Also, your setFirstName() method takes no arguments? How do you intend to set this property?

    I should of posted the class - It's defined within the class file.

    Example.
    public class Person{
    	static int age;
    	static String firstName;
    	static String surname;
    
    	public static void setAge(){
    		System.out.print("Age: ");
    		age = Keyboard.readInt();
    	}
    
    	public static int getAge(){
    		return age;
    	}
    
    	public static void setSurname(){
    		System.out.print("Surname: ");
    		surname = Keyboard.readString();
    	}
    
    	public static String getSurname(){
    		return surname;
    	}
    
    	public static void setFirstName(){
    		System.out.print("Name: ");
    		firstName = Keyboard.readString();
    	}
    
    	public static String getFirstName(){
    		return firstName;
    	}
    
    }
    

    I want to allow the user to manually enter information, not hard code it.


  • Closed Accounts Posts: 20,759 ✭✭✭✭dlofnep


    Oh sorry, that was supposed to be people.setFirstName() etc and not Person.. typo when I was typing it here.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 15,443 ✭✭✭✭bonkey


    Hmmm. You seem to have some misunderstandings...

    people, in the code you've presented, is a List of some sort. It doesn't support the setFirstName() etc. methods.

    person, in the code you've presented uses static methods. static variables store one value per class, not per instance. So you'll walk yourself into trouble if/when you ever try having two 'person' instances. If you don't want to have two 'person' instances, then wanting a LinkedList or ListArray of them seems pointless.

    So, first things first, I'm almost certain you want to get rid of every occurrence of the word 'static' from your person class. Don't take my word for it. Try creating two Person variables (person1 and person2) with your code, set a name into person1, then read the name from person2.

    After that, your complaint seems to be that its unwieldy to create a new object, then set all the properties, then add it to a list. If thats the case, then the solution is to find ways to do multiple steps together.

    The easiest way to "cobine" such intiialisation steps is firstly to add a constructor to the class which lets you supply the variables, so you can do :

    Person myPerson = new Person(someAge, someFirstName, someLastName);

    To add this to some implementation of List , you'd then do (obviously) :

    myList.add(myPerson).

    Down to two lines...far less "unwieldy".

    Now, if you don't need the myPerson variable for anything else at this point (i.e. all you want to end up with is the object in a List, then you can combine the two lines (as some poster mentioned earlier), ending up with :

    myList.add( new Person(someAge, someFirstName, someLastName) );


  • Registered Users, Registered Users 2 Posts: 2,013 ✭✭✭lynchie


    As bonkey said, get rid of all the static stuff.. its a definite No No!!

    Also, you should not have the Keyboard stuff in the setXX methods. You should ask the user for the data outside of the class and then set populate the class with those values afterwards.


  • Closed Accounts Posts: 20,759 ✭✭✭✭dlofnep


    Got it, thanks :)


Advertisement