Author Nomad: ArrayLists

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #1

    Author Nomad: ArrayLists

    What is an ArrayList

    In Java, Arrays are used to store multiple items of the same time in an easy to
    use structure. When you want to store multiple pieces of data and have data
    types relate to each other (such as Employee name, address, hours that you can
    use a set of parallel arrays or create an Employee class and use a single array
    that holds the objects.

    The problem with using Arrays is that you must set the size of the array when
    it is created. For example:

    [code=java]
    Employee[] emparray = new Employee[10];
    [/code]

    This creates an array that can only hold 10 employee objects. If you try to
    store more than 10 elements in this array, Java throws an error exception.
    Having to initially set the size of such arrays is very limiting since you may
    not know that you will have only at most (or exactly) 10 Employees.

    One way to solve this problem is to use some of the built-in Data Structure
    classes found in Java. The main purpose of such data structures is to make a
    place you can store things like Employee objects that is not constrained by
    size and that makes it easy to retrieve data from the data structure.

    Java provides a data structure called an ArrayList. This works like a
    combination of Linked List and Array in that you can store as much as you like
    in this structure and it will grow accordingly. You can also access elements in
    this structure by using an index value, much as you would with an Array.

    There are some restrictions using ArrayLists is that they can only handle Class
    objects and cannot easily handle basic data types such as integers and doubles.
    If you want to store such information in an ArrayList you will have to first
    convert it to object data types.

    Java 1.4 and before wanted you to wrap ints into Integers, doubles into Doubles
    etc.etc. This is what a fragment of Java 1.4 looked like:

    [code=java]
    List array= new ArrayList();
    for (int i= 0; i < 10; i++) {
    array.add(new Integer(i));
    int j= ((Integer)array .get(i)).intVal ue(); // get value back
    [/code]

    ... and this is what the same sources look like in Java 1.5 or higher:

    [code=java]
    List<Integer> array= new ArrayList<Integ er>();
    for (int i= 0; i < 10; i++) {
    arrray.add(i);
    int j= array.get(i); // get value back
    [/code]

    The second limitation of an ArrayList is that to use search features you must
    add some special methods to your Classes. These methods are the equals(),
    hashCode() and compareTo() methods. This will be explained later on..

    Creating an ArrayList is done by importing the java.util.*; package or just the
    single class itself: import java.util.Array List class and then creating an
    instance of the ArrayList class:

    [code=java]
    import java.util.*;
    ...
    List<PersonClas s> arlist = new ArrayList<Perso nClass>();
    [/code]

    Basically, what you do when you create an ArrayList object (in this case arlist)
    is to use the name of the class that will be used to create the objects that
    will be stored in the list. This class name is placed between a pair of < >
    (<Employee> ) symbols and placed after the ArrayList class name both places it
    is used when creating the arraylist object. In this case, the class that will
    be used to create objects to be stored in this arraylist is the Employee class.

    Using the Arraylist

    Once an ArrayList is created, you can create the objects that will be added and
    then add them to the list. This is done with the add() command:

    [code=java]
    PersonClass temp = new PersonClass ("NW123");
    arlist.add(temp );
    [/code]

    This command adds the object temp to the end of the list. If you wanted to add
    it to a particular index position in the list you would use the command:

    [code=java]
    PersonClass temp = new PersonClass ("NW123");
    arlist.add(3, temp);
    [/code]

    This shifts all elements 'down' and places this new object at position 3.
    If you wanted to replace the element at position 3 with the new object just
    created, you would use:

    [code=java]
    PersonClass temp = new PersonClass ("E123");
    arlist.set(3, temp);
    [/code]

    If you wanted to remove the element 5 you would use:

    [code=java]
    arlist.remove(5 );
    [/code]

    To get an object at position 5 you would use:

    [code=java]
    Employee x;
    x = arlist.get(5);
    [/code]

    To get the size of the ArrayList object and how many elements it is currently
    holding) you would use:

    [code=java]
    int empsize = arlist.size();
    [/code]

    If you want to view the size you would use the:

    [code=java]
    System.out.prin tln("The size is "+empsize () );
    [/code]

    To loop through a list and visit each element you can use the for() loop:

    [code=java]
    for(PersonClass e : arlist ) {
    System.out.prin tln("The name is "+e.getLnam e() );
    }
    [/code]

    The for() loop will go through all elements in the arraylist, in the same order
    as an Iterator would do. Iterators iterate over a List starting at the first
    element, ending at the last element; so the obove loop visits each element in
    the same order as the loop below would do:

    [code=java]
    for( int i=0; i<arlist.size() ; i++) {
    PersonClass e = arlist.get( i );
    System.out.prin tln("The info is "+e.getEmpI D(), +e.getLname() );
    }[/code]

    Here the size() method on the ArrayList is used to mark the end of the loop.
    The i variable is used as the ArrayList index and used with the get() method
    to return the object. The PersonClass object e is only used to hold the
    reference to the object in the list.

    This completes this short introduction of the ArrayList class. There's another
    List implementation: the LinkedList that behaves similar to the ArrayList.
    Both type of lists have their pros and cons: an ArrayList is extremely fast
    when you want to access elements given an index value. A LinkedList is very
    fast when it comes to deletion or insertion of elements anywhere in the list.

    Given the characteristics of your algorithm select the correct type of list.
    Note that your algorithm itself should refer to the particular list as an object
    of type 'List', i.e. just the interface type. This makes it very easy to select
    another type of List when needed; just the construction part of the List would
    know about what type of List will be actually constructed.

    There are more datastructures available in the Collections framework; they are
    the subject of other articles.
  • srisaimayu
    New Member
    • Mar 2008
    • 3

    #2
    need a class
    which is same as ArrayList
    and it's toString() should return size of arraylist

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by srisaimayu
      need a class
      which is same as ArrayList
      and it's toString() should return size of arraylist
      Just extend the normal ArrayList class and override it's toString method to return size().

      Comment

      Working...