In the programming, the dynamic manipulation array is often encountered, such as increasing and deleting an array element at runtime, and sometimes in compiling, it is time to determine the array size I hope it can be dynamically expanded, and the method of solving this problem in Java is to use Java. The ArrayList class in the .UTIL package, the class provides many ways to achieve dynamic manipulation of arrays, I am a Java beginner here only want to share their learning results with everyone, I will write a lot about Java. Article, I hope everyone will give more advice! There is not much empty talk, we must pay attention to the effectiveness to see an example! (Debugging at Win2000)
Import java.util. *;
public class ArrayListTest {public static void main (String [] args) {ArrayList staff = new ArrayList (); Employee emp = new Employee ( "Ivan", 60000,1984,10,6); Employee emp1 = new Employee ( "Jack ", 35000, 1982, 8, 14); Staff.Add (New Employee (" Carl Cracker ", 75000, 1987, 12, 15)); Staff.Add (New Employee (" Harry Hacker ", 50000, 1989, 10 , 15); Staff.Add (New Employee ("Tony Tester", 40000, 1990, 12, 15));
// Use this method to add an object to the specified location of the list without overwriting the original value, the original value will automatically move to move a staff.Add (1, EMP);
// Use this method to add an object to the specified location of the list, which will overwrite the original value Staff.Set (2, EMP1);
// Use this method to delete the object of the specified location from the list, and the subsequent object will automatically move to adjust the list size at the same time Staff.Remove (2);
// size () Returns the number of elements in the current list system.out.println (staff.size ()); for (int i = 0; i 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;} public string toString () {Return getClass (). Getname () "[name =" name ", Salary =" Salary ", HIREDAY =" HIREDAY "]"; } Private string name; private double sales;} Can you guess the results?