Results 1 to 5 of 5
- 12-12-2009, 01:21 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 2
- Rep Power
- 0
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() );
Java Code:Object obj1 = managers.get(i); System.out.println( "User = "+obj1.getUsername() );
- 12-12-2009, 01:40 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
To fix this we would need a little more of your code
including the error message
- 12-12-2009, 01:53 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
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());
Jos
- 12-12-2009, 02:06 PM #4
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 366
- Rep Power
- 11
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 :)
- 12-12-2009, 05:13 PM #5
Member
- Join Date
- Dec 2009
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Purse Class ArrayList using for each loop for reverse, transfer method
By CEgan in forum New To JavaReplies: 0Last Post: 12-11-2009, 11:26 PM -
Problem with class instances
By sdwinder in forum New To JavaReplies: 7Last Post: 10-21-2009, 02:25 AM -
[newbie] getting class members from Arraylist
By jon80 in forum New To JavaReplies: 16Last Post: 05-15-2009, 08:45 AM -
Using arrayList to store data from one class to another.
By nickc88 in forum New To JavaReplies: 3Last Post: 03-28-2009, 06:02 AM -
Help passing arraylist to another class
By adlb1300 in forum New To JavaReplies: 3Last Post: 11-06-2007, 09:02 PM
Bookmarks