Reputation: 169
I am trying to come over to java from C++ and am having some difficulties. In C++ I could make a list of specific class instances (such as a list of an employee class, which would include name, bday etc..). In java, however I am having a harder time figuring out the same function. In C++ I would use pointers in such, in Java it appears that it is suggested to use an ArrayList. However creating an array list and feeding it class instances works, but I cant access those class instances again. I do not really have any code examples, because I am just looking to be pointed in the right direction at this time, and just try things out. So any suggestions would be more then appreciated.
Upvotes: 2
Views: 8704
Reputation: 9590
BTW in java it is not mandatory to use ArrayList. If your size is known and you want bit of performance you can use java arrays.
Regarding pointers in C++, in java everything (except primitives like int, float etc.) is a reference which is analogous to pointers in C++.
Say i use array as an example:
Java:
Employee emp = new Employee();
Employee[] empArray = new Employee[10];
// This is similar to C++ STL
List<Employee> empList = new ArrayList<Employee>();
// Array just points to the object as pointer in C++
empArray[0] = emp;
empList.add(emp);
C++:
Employee emp = new Employee();
Employee * empArray = new Employee[10];
list<Employee> L;
For more:
Java Tutorials Java Arrays To create an analogy b/w Java and C++
Upvotes: 0
Reputation: 718768
As other answers have pointed out, generics provide the best solution.
However, you can also solve the problem the old-fashioned way with explicit typecasts; e.g.
List employees = new ArrayList();
employees.add(new Employee());
...
Employee employee = (Employee) employee.get(0);
This is the way you had to solve this problem in Java prior to Java 1.5.
By the way, the code generated by the Java compiler would include a typecast instruction even if you used generics ... as in Binyamin Sharet's version of the code.
Upvotes: 1
Reputation: 51030
You can use a generified list as follows:
List<Employee> employees = new ArrayList<Employee>();
employees.add(new Employee());
Generics is all about compile time type safety. Compiler won't let you add anything but instances of Employee
in that list.
You can loop through the list as follows:
for(Employee employee : employees) {
// use employee
}
But if you want to modify the list itself while iterating, then you have to use an Iterator
.
Iterator<Employee> it = employees.iterator();
while(it.hasNext()) {
Employee emp = it.next();
//or it.remove(); to remove the element from the list
}
List
s in Java maintain the order of the elements in which they were added to the list.
Upvotes: 0
Reputation: 137312
You can do that using generics:
List<Employee> employees = new ArrayList<Employee>();
employees.add(new Employee());
Employee employee = employees.get(0);
Take a look at the documentation of ArrayList<E>
and List<E>
, which is the interface.
Upvotes: 3
Reputation: 23268
You can use an arraylist with templating as it would be called in c++ so it would look like
ArrayList<MyObject> list = new ArrayList<MyObject>();
This way any object you access from the list is already automatically cast to MyObject which avoids the manual cast you would have to do in your case.
Upvotes: 0