ArrayList

Java ArrayList is a resizable array which implements List interface. ArrayList provides all operation defined by List interface. Internally ArrayList uses an array to store its elements. ArrayList provides additional methods to manipulate the array that actually stores the elements.

ArrayList is equivalent to Vector, but ArrayList is not synchronized.

Java ArrayList Capacity

Capacity of an ArrayList is the size of the array used to store the list elements. It grows automatically as we add elements to it. Every time this happens, the internal array has to be reallocated. This increases the load.

We can set the initial capacity of the ArrayList using following method.

ArrayList arrayList = new ArrayList();
arrayList.ensureCapacity(100);

Java ArrayList Iterators

Java ArrayList provides two types of Iterators.
1) Iterator
2) ListIterator

Iterator iterator = arrayList.iterator();

Returns object of Iterator.

ListIterator listIterator = arrayList.listIterator();

Returns object of ListIterator.

ListIterator listIterator = arrayList.listIterator(int startIndex);

Returns object of ListIterator. The first next() method call on this ListIterator object will return the element at the specified index passed to get the ListIterator object.

Iterators returned by these methods are fail-fast. That means if the list is modified after getting the Iterator by using some other means rather than Iterators own add or remove method, Iterator will throw ConcurrentModificationException.

ArrayList Constructors

1) ArrayList()
Creates an empty ArrayList.

For example,
ArrayList arrayList = new ArrayList();

2) ArrayList(int capacity)
Creates an ArrayList with specified initial capacity.

For example,
ArrayList arrayList = new ArrayList(10);

3) ArrayList(Collection c)
Creates an ArrayList containing elements of the collection specified.

For example,
ArrayList arrayList = new ArrayList(myCollection);

Where myCollection is an object of the type Collection. This creates an ArrayList of elements contained in the myCollection, in the order returned by the myCollection’s Iterator.

ArrayList

22 Comments

  • hiee first of all We would like to thank you for ur excellent work in making this website. This is the best website which i have seen where each and every topic is covered clearly with a lot of examples. please cover exceptions concept also, we couldn’t get it from any textbook, the approach is not clear. we are working in MNC. We need it as early as possible. Hope u wil consider it soon.
    Thanks alot… :))
    with regards,
    JRC

  • ·
    Question 3 / 4 (ATM Cash Machine)

    You are to write a program for a
    bank ATM machine. The machine dispenses notes as as well as coins, in the
    standard Indian denominations: 1, 2, 5, 10, 20, 50, 100, 500, 1000. Your
    program takes the amount to be withdrawn as the input. As output, it should
    output (to standard output) the breakup of that amount into the denominations
    above, using the FEWEST POSSIBLE NUMBER of notes and coins, in the text format
    specified below. The solution template has some code written already, as a
    possible starting point for the solution. You may use this or you may choose
    not to, the choice is yours!

    Here are the test cases your program
    should pass. Note that each line of the output should be: a number, followed by
    a space, followed by the character “x”, followed by another space,
    and then another number.

    1. Input:

    4177
    Output:
    4x 1000
    1x 100
    1x 50
    1x 20
    1x 5
    1x 2

    2. Input:

    9999

    Output:

    9x 1000

    1x 500
    4x 100
    1x 50
    2x 20
    1x 5
    2x 2
    3. Input:
    50
    Output:
    1 x 50
    please help me to solve it……

Sponsors

Facebook Fans