Arraylist
Arraylist is a generic class that uses type parameters, for example, arraylist <employee>. This is a list of generic arrays that store employee objects.
Constructor:
ArrayList<Employee> staff = new ArrayList<Employee>();
The following is an example of arraylist, which implements the constructor, add, remove, and set methods of the Generic Array list.
Package COM. xujin; import Java. util. arraylist; public class main {public static void main (string... ARGs) {arraylist <employee> Staff = new arraylist <employee> (10); // Add the element staff. add (new employee ("Bob", 4000); staff. add (new employee ("Jim", 5000); staff. add (new employee ("July", 8000); staff. add (new employee ("aliy", 6000); employee e = staff. get (0); system. out. println (E. tostring (); // COM. xujin. employee [name = Bob, salary = 4000.0] // set an element, which must be an existing staff. set (0, new employee ("Lily", 10000); system. out. println (staff. get (0); // COM. xujin. employee [name = Lily, salary = 10000.0] system. out. println (staff. size (); // 4 // cut the array list to the current size of staff. trimtosize (); // remove function to delete an element staff. remove (0); system. out. println (staff. get (0); // COM. xujin. employee [name = Jim, salary = 5000.0] system. out. println (staff. size (); // 3 // insert an element in the add method. The index is 3, indicating that all the elements 3 and 3 are moved to the staff. add (3, new employee ("Ted", 6000); For (employee EM: Staff) {system. out. println (EM);}/** COM. xujin. employee [name = Jim, salary = 5000.0] COM. xujin. employee [name = July, salary = 8000.0] COM. xujin. employee [name = aliy, salary = 6000.0] COM. xujin. employee [name = Ted, salary = 6000.0] */} class employee {public employee (string name, double salary) {This. name = Name; this. salary = salary;} Public String tostring () {return getclass (). getname () + "[name =" + name + ", salary =" + salary + "]" ;}// defines the variable private double salary; private string name ;}
Object wrappers and automatic Packaging
Each basic data type has a corresponding class, which is called the object Wrapper class.
Object Wrapper class: integer, double, float, long, short, byte, character, void, Boolean. Note: Red is derived from the number class.
Autoboxing ):
List. Add (3); // automatically changes to:; List. Add (New INTEGER (3 ));
Automatic package Splitting:
Integer n = 3;n++;
The above two statements will be implemented: the compiler Automatically splits the integer type object N, performs the auto-increment operation, and finally packs the result into the object package.
In addition, there are several common static integer methods, such as the parseint method in the following example.
package com.xujin;public class Main{public static void main(String...args){Integer a = 1000000;System.out.println(a.intValue());//1000000int x = Integer.parseInt("124324");int y = Integer.parseInt("2333", 8);System.out.println("x:" + x + "\ny:" + y);//x:124324 y:1243}}
Enumeration class
Package COM. xujin; public class main {public static void main (string... ARGs) {size S = (size) enum. valueof (size. class, "small"); // The values method returns all the enumerated values of this enumeration class. Size [] values = size. values (); For (size: values) {system. out. println (size. getabb ();} // the location of the enumerated constant returned by the ordinal () method, starting from 0. out. println (size. large. ordinal (); // 2} Enum size {// this class has five instances: the following five small ("S "), midium ("M"), large ("L"), extra_large ("XL"); Private size (string abbreviation) {This. abbreviation = abbreviation;} Public String getabb () {return abbreviation;} private string abbreviation ;}