Results 1 to 7 of 7
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,070
    Rep Power
    8

    Default array versus arraylist

    Imaging I declare a class with three data fields
    and its getters and setters (see next example in three phases);
    Next I declare an array of the type of the class and initialize the array;
    The point is that I have to know on forehand the size of the array: right!

    How would that example look like when using the array list class, were one
    can add etc. without having to declare the size right away?


    Java Code:
    1) private VacationDataList vacationdatalist[];
    
    2) vacationdatalist = new VacationDataList[15]; // initialize the vacation list of 15 entries
                for (int i = 0; i < vacationdatalist.length; i++) { // data array
                        vacationdatalist[i] = new VacationDataList();
    
                }
    
    
    
    3) private class VacationDataList { // vacation list array of  unlimited entries
             private String vacationinfo;
             private double vacationbegin, vacationend; 
             
             public void setInfo(String inf)  {
                    vacationinfo = inf;
                }
             public String getInfo()  {
                    return vacationinfo;
                }
             
             public void setBeginTime(double bt)  {
                    vacationbegin = bt;
                }
             public double getBeginTime()  {
                    return vacationbegin;
                }
             
             public void setEndTime(double et)  {
                    vacationend = et;
                }
             public double getEndTime()  {
                    return vacationend;
                }
         }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: array versus arraylist

    Quote Originally Posted by willemjav View Post
    Imaging I declare a class with three data fields
    and its getters and setters (see next example in three phases);
    Next I declare an array of the type of the class and initialize the array;
    The point is that I have to know on forehand the size of the array: right!

    How would that example look like when using the array list class, were one
    can add etc. without having to declare the size right away?


    Java Code:
    1) private VacationDataList vacationdatalist[];
    
    2) vacationdatalist = new VacationDataList[15]; // initialize the vacation list of 15 entries
                for (int i = 0; i < vacationdatalist.length; i++) { // data array
                        vacationdatalist[i] = new VacationDataList();
    
                }
    
    
    
    3) private class VacationDataList { // vacation list array of  unlimited entries
             private String vacationinfo;
             private double vacationbegin, vacationend; 
             
             public void setInfo(String inf)  {
                    vacationinfo = inf;
                }
             public String getInfo()  {
                    return vacationinfo;
                }
             
             public void setBeginTime(double bt)  {
                    vacationbegin = bt;
                }
             public double getBeginTime()  {
                    return vacationbegin;
                }
             
             public void setEndTime(double et)  {
                    vacationend = et;
                }
             public double getEndTime()  {
                    return vacationend;
                }
         }
    Steps 1) and 2) would look something like this:

    Java Code:
    1) private List<VacationDataList) vacationDataList;
    
    2)vacationdatalist = new ArrayList<VacationDataList>(); // initialize the vacation list with no entries
                // add 'n' items
                for (int i = 0; i < n; i++) { // data array list
                        vacationdatalist.add(new VacationDataList());
     
                }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,070
    Rep Power
    8

    Default Re: array versus arraylist

    Thanks jos looking into it right now (have to figure out what < > means)
    there is one missing at List<VacationDataList) vacationDataList;
    ?

  4. #4
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,070
    Rep Power
    8

    Default Re: array versus arraylist

    The point is that I need to know "n"
    But what if I'd like to add (during runtime) a unlimited number of
    the three class' fields?

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: array versus arraylist

    Quote Originally Posted by willemjav View Post
    Thanks jos looking into it right now (have to figure out what < > means)
    there is one missing at List<VacationDataList) vacationDataList;
    ?
    Sorry, that's my fault (note to self: test the stupid code before posting, you moron).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: array versus arraylist

    Quote Originally Posted by willemjav View Post
    The point is that I need to know "n"
    But what if I'd like to add (during runtime) a unlimited number of
    the three class' fields?
    That's the fun with lists: add all you want without having to worry about the size of the list.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: array versus arraylist

    You don't need to know 'n'. If you want to add a VacationDataList, you create one set it's data and add it to the List.
    An unlimited number is not possible. You'll run out of memory sooner or later and if not that you'll be limited by Integer.MAX_VALUE.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. Array or ArrayList?
    By rhym1n in forum New To Java
    Replies: 1
    Last Post: 03-08-2013, 01:08 PM
  2. 32-bit versus 64-bit
    By newbie123 in forum New To Java
    Replies: 4
    Last Post: 09-06-2012, 05:52 PM
  3. Array vs ArrayList
    By Lund01 in forum New To Java
    Replies: 5
    Last Post: 10-14-2010, 10:41 AM
  4. 2D array of ArrayList (technically a 3D array)
    By Lil_Aziz1 in forum New To Java
    Replies: 3
    Last Post: 05-15-2010, 08:11 PM
  5. ArrayList versus Array
    By junpogi in forum New To Java
    Replies: 13
    Last Post: 11-08-2008, 02:04 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
  •