This program demonstrates the ArrayList class : ArrayList « Collections Data Structure « Java






This program demonstrates the ArrayList class

   
/*
 This program is a part of the companion code for Core Java 8th ed.
 (http://horstmann.com/corejava)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * This program demonstrates the ArrayList class.
 * 
 * @version 1.1 2004-02-21
 * @author Cay Horstmann
 */
public class ArrayListTest {
  public static void main(String[] args) {
    // fill the staff array list with three Employee objects
    ArrayList<Employee> staff = new ArrayList<Employee>();

    staff.add(new Employee("Carl Cracker", 75000, 1987, 12, 15));
    staff.add(new Employee("Harry Hacker", 50000, 1989, 10, 1));
    staff.add(new Employee("Tony Tester", 40000, 1990, 3, 15));

    // raise everyone's salary by 5%
    for (Employee e : staff)
      e.raiseSalary(5);

    // print out information about all Employee objects
    for (Employee e : staff)
      System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay="
          + e.getHireDay());
  }
}

class Employee {
  public Employee(String n, double s, int year, int month, int day) {
    name = n;
    salary = s;
    GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
    hireDay = calendar.getTime();
  }

  public String getName() {
    return name;
  }

  public double getSalary() {
    return salary;
  }

  public Date getHireDay() {
    return hireDay;
  }

  public void raiseSalary(double byPercent) {
    double raise = salary * byPercent / 100;
    salary += raise;
  }

  private String name;

  private double salary;

  private Date hireDay;
}

   
    
    
  








Related examples in the same category

1.Get the size of an arraylist after and before add and remove methods
2.Use the Iterator returned from ArrayList to loop through an array list
3.Get generic Iterator from generic ArrayList
4.Convert an ArrayList into an array.
5.Pre-generics example that uses a collection.
6.shows the modern, generic form of collection classes
7.Use set method to change the value in an array list
8.Store user-defined objects in arraylist
9.pass the actual object you want removed.
10.Convert a List (ArrayList) to an Array with zero length array
11.Convert a List (ArrayList) to an Array with full length array
12.Remove duplicate items from an ArrayList
13.Looping through a Collection object: while loop, iterator, and for each
14.Append all elements of other Collection to Java ArrayList
15.Copy all elements of Java ArrayList to an Object Array
16.Get Size of Java ArrayList and loop through elements
17.Add an element to specified index of Java ArrayList
18.Get Sub List of Java ArrayList
19.Insert all elements of other Collection to Specified Index of Java ArrayList
20.Iterate through elements Java ArrayList using Iterator
21.Iterate through elements Java ArrayList using ListIterator
22.Remove all elements from Java ArrayList
23.Remove an element from specified index of Java ArrayList
24.Search an element of Java ArrayList
25.Get element in an ArrayList by index
26.Replace an element at specified index of Java ArrayList
27.Sort elements of Java ArrayList
28.Copy Elements of ArrayList to Java Vector
29.Copy Elements of One Java ArrayList to Another Java ArrayList
30.Find maximum element of Java ArrayList
31.Find Minimum element of Java ArrayList
32.Get Enumeration over Java ArrayList
33.Get Synchronized List from Java ArrayList
34.Perform Binary Search on Java ArrayList
35.Replace All Elements Of Java ArrayList
36.Replace all occurrences of specified element of Java ArrayList
37.Reverse order of all elements of Java ArrayList
38.Shuffle elements of Java ArrayList
39.Swap elements of Java ArrayList
40.Iterate through a Collection using Java Iterator
41.Sort Java ArrayList in descending order using comparator
42.Remove an element from Collection using Java Iterator
43.Replace an element from ArrayList using Java ListIterator
44.How to Convert an ArrayList into an array
45.Sort items of an ArrayList
46.Rotate elements of a collection
47.Search collection element
48.If an ArrayList contains a given item
49.Convert Collection to ArrayList?
50.A boolean is being stored and then retrieved from an ArrayList
51.Traverse through ArrayList in reverse direction using Java ListIterator
52.Traverse through ArrayList in forward direction using Java ListIterator
53.Remove an element from ArrayList using Java ListIterator
54.Get Previous and next index using Java ListIterator
55.A Map implemented with ArrayLists
56.A variant of java.util.ArrayList in which all mutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array
57.Copy On Write ArrayList
58.ArrayList with sorted contents.