Thursday, August 18, 2011

Why We Use Java ArrayList

This time Ar-Rohman Software House Team will discuss about The ArrayList where before we had wrote about Framework Collection in Java. OK, What is ArrayList ? Which Class that implements the interface List. ArrayList advantages compared with other types of data structure is its ability to be flexible. Flexible mean here is the number of index can be changed automatically when adding or reducing the element.

Of course this is different from regular Array. where the number of array elements is declared when the object was created. But the trouble is when the array was removed one element, then sum of the index array is still, that means there is one index is empty. this which causes wasteful memory.

From the description of the two paragraphs above can be concluded that ArrayList is more flexible than regular Array. There are some methods below that are used to manipulate elements in ArrayListobject :

RETURN METHOD FUNCTION
boolean add(E e) Appends the specified element to the end of this list.
void clear() Removes all of the elements from this list.
Element get(int index) Returns the element at the specified position in this list.
boolean isEmpty() Returns true if this list contains no elements.
Element remove(int index) Removes the element at the specified position in this list.
Element set(int index, E element) Replaces the element at the specified position in this list with the specified element.
int size() Returns the number of elements in this list.

I would then try each of the methods above to make it easier to be understood about their usefulness. First, I'll create class Person to be stored in ArrayList Object
Code : Class Person
package person;
/**
*
* @author Ar-Rohman Software House 
*/
public class Person {
    private String nama ;
    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getNama() {
        return nama;
    }

    public void setNama(String nama) {
        this.nama = nama;
    }

}

1. add(E e)
Code :
List<Person> list = new ArrayList<Person>();
        Person p1 = new Person();
        p1.setNama("Ar-Rohman Software House");
        p1.setAddress("Malang");

        Person p2 = new Person();
        p2.setNama("Jasa Pembuatan Software");
        p2.setAddress("Malang");

        //Menambah Elemen
        list.add(p1);
        list.add(p2);

        for(Person p : list){
            System.out.println("Nama : "+p.getNama());
            System.out.println("Alamat : "+p.getAddress());
        }
Explanation:
- Create an ArrayList object named list
- Create two Person objects: p1, p2
- Syntax "list.add (p1);" function to add Person objects to the ArrayList object
- Displays the element of the ArrayList using the command "for"

2. set(int index, E element)
Code:
Person p3 = new Person();
       p3.setNama("Hire Us !");
       p3.setAddress("Malang");

       list.set(0, p3);
       for(Person p : list){
           System.out.println("Name : "+p.getNama());
           System.out.println("Addres : "+p.getAddress());
       }
Explanation:
- Create a Person object: p3
- Syntax "list.set (0, p3);" change the contents of index 0 (which is filled p1) with the object p3
-Displays the element of the ArrayList using the command "for"

3. remove(int index)
Code :
list.remove(1); //remove element on index 1
        for(Person p : list){
            System.out.println("Nama : "+p.getNama());
            System.out.println("Alamat : "+p.getAddress());
        }

4. get(int index)
Code :
System.out.println("Nama : "+list.get(0).getNama()); //get element on index 0
//output : Name : Ar-Rohman Software House

5. isEmpty()
Code :
if(list.isEmpty()){
            System.out.println("Array is Empty");
        }else{
            System.out.println("Array have element");
        }

6. size()
Code :
System.out.println("Jumlah Element : "+list.size());
//output : number of element on object list

7. clear()
Code :
list.clear(); //remove  all element from list
        System.out.println("Amount of Elemet : "+list.size());

0 comments:

Post a Comment

If you have something to say about this post, please fell free to write it in the comment form below. We will reply to all comments as soon as possible. Thanks ...