Results 1 to 5 of 5
  1. #1
    Ersk is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Question Class Instances stored in an ArrayList

    Basicly I am trying to store multiple instances of a class in an Array List. I have already done this with a regular Array but I am in need of dynamic storage.

    I have managed to store the instances in the ArrayList but I can't get the methods to work.
    Java Code:
    ArrayList managers = new ArrayList();
    managers.add(   new Manager(...)    );
    
    System.out.println(   "User = "+manager.get(i).getUsername()   );
    I also tried:
    Java Code:
    Object obj1 = managers.get(i);
    System.out.println(   "User = "+obj1.getUsername()   );
    Any help would be greatly appreciated, thank you.

  2. #2
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    To fix this we would need a little more of your code
    including the error message

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    ArrayLists and other Collections store any type of Object and know nothing about their actual type. You have to explicitly cast an object from a container back to the type you want. Since Java 1.5 there's a compiler trick: generics: an ArrayList<T> stores objects of type T (or subtypes thereof). You fill in the actual type T, as in: ArrayList<Manager>. The compiler checks if you really store a Manager object in such an ArrayList and it knows that any retrieved object from such an ArrayList has to be a Manager (or a sub class thereof).

    So basically you have to do this:

    Java Code:
    ArrayList<Manager> managers= new ArrayList<Manager>();
    managers.add(new Manager( ... ));
    ...
    System.out.println(managers.get(0).getUserName());
    kind regards,

    Jos

  4. #4
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    364
    Rep Power
    5

    Default

    Hello :)

    It is right time for you to learn power and usability of "GENERICS" in java.
    Examples are all over net.

    This is simple code that solves your problem.

    As you can see there is NO casting and NO need to work with Object class.
    Code is very clean and with this approach you will not have possible errors in run-time.


    ---------------
    Java Code:
    package arrays;
    
    /**
     * simple class with only constructor setters and getters
     * @author dren
     *
     */
    public class Manager {
    
    	private String name;
    	private int age;
    	
    	public Manager(String n, int a) {
    		name = n;
    		age = a;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public int getAge() {
    		return age;
    	}
    
    	public void setAge(int age) {
    		this.age = age;
    	}
    	
    	
    	
    }
    --------------------
    Java Code:
    package arrays;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class ArrayWorks {
    
    	/*use Java generics!*/
    	private List<Manager> managerList = new ArrayList<Manager>();
    
    	/**
    	 * created concrete managers and add them to list
    	 */
    	private void createManagers() {
    		Manager manager1 = new Manager("John ", 34);
    		Manager manager2 = new Manager("Peter",25);
    
    		managerList.add(manager1);
    		managerList.add(manager2);
    
    	}
    
    	/**
    	 * display all managers in list
    	 */
    	private void displayManagers() {
    
    		for(Manager oneManager : managerList){
    			System.out.println(oneManager.getName());
    			System.out.println(oneManager.getAge());
    		}
    
    	}
    
    	public static void main(String[] args) {
    
    		ArrayWorks works = new ArrayWorks();
    
    		/*cretate list*/
    		works.createManagers();
    
    		/*iterate through list and display all managers*/
    		works.displayManagers();
    	}
    
    
    }
    ---------

    regards :)

  5. #5
    Ersk is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Replies: 0
    Last Post: 12-11-2009, 10:26 PM
  2. Problem with class instances
    By sdwinder in forum New To Java
    Replies: 7
    Last Post: 10-21-2009, 01:25 AM
  3. [newbie] getting class members from Arraylist
    By jon80 in forum New To Java
    Replies: 16
    Last Post: 05-15-2009, 07:45 AM
  4. Replies: 3
    Last Post: 03-28-2009, 05:02 AM
  5. Help passing arraylist to another class
    By adlb1300 in forum New To Java
    Replies: 3
    Last Post: 11-06-2007, 08:02 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •