Results 1 to 14 of 14
Thread: ArrayList versus Array
- 11-07-2008, 06:28 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 6
- Rep Power
- 0
ArrayList versus Array
Hello gurus,
Could you guys help me on translating regular array to using ArrayList?
Let's say I have an array of Employee. Employee has a method called setWhatEver().
Using regular array:
Employee emp = new Employee[5];
for (int i=0; i <5; i++){
emp[i] = new Employee();
emp[i].setWhatEver(); //// How do I do this using ArrayList?
}
So far I can only do:
ArrayList<Employee> emp = ArrayList<Employee>(5);
How can I use the method setWhatEver() using ArrayList in a for-loop similar to above?
Thanks!
- 11-07-2008, 06:33 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
What you have tried so far? Did you get any error messages?
- 11-07-2008, 06:43 AM #3
Member
- Join Date
- Oct 2008
- Posts
- 6
- Rep Power
- 0
Hello Eranga,
Thank you for replying. This is what I tried:
ArrayList<Employee> emp = ArrayList<Employee>(5);
for (int i=0; i<5; i++)
{
emp.set(i , new Employee());
emp[i].setWhatEver(); /////////// <----This is not working
}
Any ideas?
- 11-07-2008, 07:03 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
First of all, you should study more about ArrayList. Did you read about it on Java doc.
Java Code:ArrayList<Employee> emp = new ArrayList<Employee>();
- 11-07-2008, 11:11 AM #5
Java Code:Employee emp = new Employee[5]; ArrayList emp_list = new ArrayList(); for(int i=0;i<emp.length;i++){ emp_list.add(emp[i]); }
- 11-07-2008, 02:42 PM #6
You are using array notation ([i]) with the variable emp.emp[i].setWhatEver(); /////////// <----This is not working
Is emp an array?
If not, then you need to read the API doc for the type/class that it is to see how to get an element out of that class.
- 11-07-2008, 04:35 PM #7
Member
- Join Date
- Oct 2008
- Posts
- 6
- Rep Power
- 0
Thank you guys for replying. What I want to do is "change my array program" using an ArrayList. I want to be able to expand my arrays of emp of type Employee and be able to use its method.
I want to recode a typical array as shown below and use ArrayList instead. Please note that I need to access the method of the Employee. Please see below.
***my regular array program. i would like to recode this using arraylist***
Employee emp = new Employee[5];
for (int i=0; i <5; i++){
emp[i] = new Employee();
emp[i].setWhatEver(); //<----How do I do this using ArrayList?
}
Can this be done? Please read original note. Thanks!
- 11-07-2008, 05:01 PM #8
Read the API doc for start.-How do I do this using ArrayList?
you need to read the API doc for the type/class that it is to see how to get an element out of that class.
- 11-07-2008, 05:21 PM #9
hhhmmm...
Yes you can turn arrays into arraylist (lists):
String array to arraylist (Java in General (intermediate) forum at JavaRanch)
But the way you're doing it doesn't look right...
... which means that you're trying to instantiate a class to an array (I don't think this is doable).Java Code:emp[i] = new Employee();
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 11-07-2008, 05:21 PM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You must read about ArrayList in Java doc. Completely mess with what you are going to do. So first of all read it and try to get a complete idea.
- 11-07-2008, 06:09 PM #11
emp[i] = new Employee()
Not sure what you are saying.trying to instantiate a class to an array (I don't think this is doable).
The above statement is how you would set the ith element in the emp array to an instance of the Employee class. That assumes that emp is an array of Employee, such as defined by: Employee[] emp;
- 11-07-2008, 06:35 PM #12
true...
yep Norm... your right. I wasn't seeing it as an array of Employee classes... duh... sorry...my bad :(. Just goes to show what 6 hours of sleep can do over a 40 hr period ...
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 11-07-2008, 08:05 PM #13
Meanwhile I guess the OP is reading the API doc for ArrayList.
- 11-08-2008, 02:04 AM #14
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Similar Threads
-
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM -
ArrayList
By kizilbas1 in forum New To JavaReplies: 1Last Post: 01-12-2008, 08:48 PM -
Arraylist to a 2- dimension array conversion
By mars123 in forum New To JavaReplies: 1Last Post: 12-06-2007, 11:24 AM -
Converting ArrayList to Array
By Java Tip in forum Java TipReplies: 0Last Post: 11-13-2007, 10:41 AM -
Array to ArrayList
By javaplus in forum New To JavaReplies: 2Last Post: 11-12-2007, 12:46 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks