/** * Write a description of class EmployeeArrayListDemo here. * * @author (your name) * @version (a version number or a date) */ import java.util.ArrayList; public class EmployeeArrayListDemo { public static void main ( ) { ArrayList <String> firstName = new ArrayList<String>(); ArrayList <String> lastName = new ArrayList<String> (); ArrayList <Integer> salary = new ArrayList<Integer> ();
firstName.add("Bart "); lastName.add("Smith"); salary.add(200);
firstName.add("Jane "); lastName.add("Smart"); salary.add(100);
firstName.add("Bill "); lastName.add("Jones"); salary.add(300);
output ( firstName, lastName, salary ) ;
// firstName.set(0, firstName.get(0)+ lastName.get(0)); // System.out.println(firstName.get(0)); System.out.println("Total employees = " + salary.size() + ", Total salary = " + total(salary));
int randIndex = (int)(lastName.size() * Math.random()); lastName.remove (randIndex); firstName.remove (randIndex); salary.remove (randIndex);
output(firstName, lastName, salary); System.out.println( "Total employees = " + salary.size ( ) + ", Total salary = " + total ( salary ) ) ; }
static public int total ( ArrayList<Integer> s ) { int sum = 0;
for ( int x=0; x< s.size(); x++ ) { sum += ( int ) ( s.get ( x ) ) ; } return sum ; }
static void output ( ArrayList<String> fN, ArrayList<String> lN, ArrayList<Integer> s ){ System.out.println(); System.out.println("Name Salary "); for( int x=0; x< fN.size ( ); x++ ) { System.out.println(fN.get ( x ) + lN.get ( x ) + " " + s.get ( x ) ) ; } } }