Java Generic Array list

Source: Internet
Author: User
Tags float double

The following code:

1  PackageCom.company;2 3 ImportJava.lang.reflect.Array;4 Importjava.util.ArrayList;5 6  Public classArray_list {7      Public Static voidMain (string[] args) {8Arraylist<string> arr_a=NewArraylist<>();9          for(inti=0;i<20;i++){TenArr_a.add ("OK"); One         } AString[] Int_ar=NewString[20]; -String[] New_ar=Arr_a.toarray (int_ar); -System.out.println (Arr_a.get (1)); the          for(String i:arr_a) {System.out.println (i);} -     } -}

1  PackageCom.company;2 3 ImportJava.lang.reflect.Array;4 Importjava.util.ArrayList;5 6  Public classArray_list {7      Public Static voidMain (string[] args) {8Arraylist<integer> arr_a=NewArraylist<>();9          for(inti=0;i<20;i++){Ten Arr_a.add (i); One         } AInteger[] Int_ar=NewInteger[20]; -System.out.println (Arr_a.get (1)); -          for(Integer i:arr_a) {System.out.println (i);} the     } -}

Generic Array list:

Before we touched the original array, it was not possible to determine dynamically determining the size of the array, to define its size when initializing the array, and then if it was troublesome to change its size. Then there is the ArrayList class to implement the dynamic array size.

When the current array list exceeds the current length, the current array element is copied to the large array.

1  Public classArray_list {2      Public Static voidMain (string[] args) {3Arraylist<integer> arr_a=New arraylist<> (ten);4          for(inti=0;i<20;i++){5 Arr_a.add (i);6         }7System.out.println (Arr_a.get (1));8          for(Integer i:arr_a) {System.out.println (i);}9     }Ten}

Grammar:

arraylist< parameter type > variable name = new arraylist< parameter type can be no > (capacity size can also be not written)

Note: Only reference objects can be used ArrayList if the base data type wants to use it, you need to use a Packer to use!!!

Method:

Methods such as add, delete, Fetch, and so on.

Where to get and set (GET, set method): If the data list is empty or the index set exceeds the error. We can use the ArrayList when we insert the object first, and get the elements of the array using the [number] style when we convert to a normal array. As follows:

1  PackageCom.company;2 3 ImportJava.lang.reflect.Array;4 Importjava.util.ArrayList;5 6  Public classArray_list {7      Public Static voidMain (string[] args) {8Arraylist<integer> arr_a=NewArraylist<> (10);9          for(inti=0;i<20;i++){Ten Arr_a.add (i); One         } A         integer[] ar2= new Integer[arr_a.size ()]; -         integer[] new_ar= (integer[]) Arr_a.toarray (AR2); -        System.out.println (new_ar[0]); the     } -}

In arraylist<>, the parameter type does not allow basic data types such as int, float, double, etc., but the base data type also corresponds to the object, such as: Int=>integer float=>float double- >double Short=>short bytes=>bytes Boolean=>boolean I will call the corresponding class wrapper.

The object wrapper class is immutable, the generic constructor is generated, it is not allowed to change its value, and the wrapped class is final, so you cannot define subclasses.

Automatic Boxing:

In the above basic type of wrapper, when calling ArrayList, we can directly use: Arr_a.add (3); But the actual compiler helps us implement this process: Arr_a.add (Integer. ValueOf (4)); This process is called auto-boxing autoboxing

1  PackageCom.company;2 3 ImportJava.lang.reflect.Array;4 Importjava.util.ArrayList;5 6  Public classArray_list {7      Public Static voidMain (string[] args) {8Arraylist<integer> arr_a=NewArraylist<> (10);9          for(inti=0;i<20;i++){Ten Arr_a.add (i); One         } AArr_a.add (3); -Arr_a.add (Integer.valueof (4)); -Integer[] Ar2=Newinteger[arr_a.size ()]; theInteger[] New_ar=(integer[]) Arr_a.toarray (AR2); -System.out.println (New_ar[arr_a.size ()-1]); -System.out.println (New_ar[arr_a.size ()-2]); -     } +}

Automatic unpacking:

For example, we get the element value of the array above. System.out.println (Arr_a.get (3));  Equivalent to System.out.println (Arr_a.get (3). Intvalue ()); We call it automatic unpacking. including the self-increment of variables are automatically unboxing in the auto-boxing!

1 Importjava.util.ArrayList;2 3  Public classArray_list {4      Public Static voidMain (string[] args) {5Arraylist<integer> arr_a=NewArraylist<> (10);6          for(inti=0;i<20;i++){7 Arr_a.add (i);8         }9Arr_a.add (3);TenArr_a.add (Integer.valueof (4)); One         System.out.println (Arr_a.get (3)); A         System.out.println (Arr_a.get (3). Intvalue ()); -Integer[] Ar2=Newinteger[arr_a.size ()]; -Integer[] New_ar=(integer[]) Arr_a.toarray (AR2); theSystem.out.println (New_ar[arr_a.size ()-1]); -System.out.println (New_ar[arr_a.size ()-2]); -     } -}

Java Generic Array list

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.