Results 1 to 14 of 14
  1. #1
    junpogi is offline Member
    Join Date
    Oct 2008
    Posts
    6
    Rep Power
    0

    Default 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!

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    junpogi is offline Member
    Join Date
    Oct 2008
    Posts
    6
    Rep Power
    0

    Default

    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?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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>();

  5. #5
    Join Date
    Apr 2008
    Posts
    64
    Rep Power
    0

    Default

    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]);
    }

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    emp[i].setWhatEver(); /////////// <----This is not working
    You are using array notation ([i]) with the variable emp.
    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.

  7. #7
    junpogi is offline Member
    Join Date
    Oct 2008
    Posts
    6
    Rep Power
    0

    Default

    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!

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    -How do I do this using ArrayList?
    Read the API doc for start.
    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.

  9. #9
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default 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...
    Java Code:
    emp[i] = new Employee();
    ... which means that you're trying to instantiate a class to an array (I don't think this is doable).
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    emp[i] = new Employee()
    trying to instantiate a class to an array (I don't think this is doable).
    Not sure what you are saying.
    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;

  12. #12
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default 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 ...

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    Meanwhile I guess the OP is reading the API doc for ArrayList.

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Norm View Post
    Meanwhile I guess the OP is reading the API doc for ArrayList.
    He has to do that. I'm sure with our all post in different explanation, our OP can confuse on Arrays and ArrayList.

Similar Threads

  1. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  2. ArrayList
    By kizilbas1 in forum New To Java
    Replies: 1
    Last Post: 01-12-2008, 08:48 PM
  3. Arraylist to a 2- dimension array conversion
    By mars123 in forum New To Java
    Replies: 1
    Last Post: 12-06-2007, 11:24 AM
  4. Converting ArrayList to Array
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-13-2007, 10:41 AM
  5. Array to ArrayList
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-12-2007, 12:46 AM

Posting Permissions

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