1. What is ArrayList
ArrayList is the legendary dynamic array, which, in MSDN parlance, is a complex version of array that provides some of the following benefits:
(1) Dynamic increase and decrease of elements
(2) ICollection and IList interfaces are implemented
(3) Flexibility to set the size of the array
2, the creation of Arraylis and adding and deleting methods
(1) Creation of ArrayList and addition of elements ArrayList < variable name > = new ArrayList (); () can also be used to transmit parameters.
Note: The above is an empty list of ArrayList created. When we want to pass an element to the list, we use the. Add () method to assign the value. Take a look at the following case. In addition, you want to output the elements of the list to be traversed through a for loop.
public class Test {public static void main (string[] args) {ArrayList lis = new ArrayList (); Lis.add ("Tony"); Lis.add ("Tom"); Lis.add ("Jack"); Lis.add ("Mary"); Lis.add ("even"); for (int i=0;i<lis.size (); i++) {String result = (string) lis.get (i ); SYSTEM.OUT.PRINTLN (result);}
The result: the output is as follows.
Tony
Tom
Jack
Mary
Even
(2) Deletion of elements in ArrayList
Note: variable name. remove (int Index); Delete by using the. Remove (int Index) method. Here, you pass directly to the subscript where you want to delete the element. Of course, you can also directly pass in the value to delete. If you feel like you're in trouble with the bottom of the way, you can enter the value of the item you want to delete directly.
Package Com.java.demo_9;import Java.util.arraylist;public class ArrayList {public static void main (string[] args) { ArrayList lis = new ArrayList (), Lis.add ("Tony"), Lis.add ("Tom"), Lis.add ("Jack"); Lis.add ("Mary"); Lis.add ("even"); System.out.println ("---------------------------------"); System.out.println ("<arraylist original list element;:"); for (int i = 0; I < lis.size (); i++) {String result = (string) lis.get (i); SYSTEM.OUT.PRINTLN (result);} System.out.println ("---------------------------------"); System.out.println ("< list of ArrayList after deletion of elements;:"); Lis.remove (1); for (int i = 0; I < lis.size (); i++) {String result = (string) lis.get (i); SYSTEM.OUT.PRINTLN (result);} System.out.println ("---------------------------------");}}
Result: Because I specified the Lis.remove (1); Index is starting from 0 and therefore 1 represents the 2nd element. So the deletion of this element is Tom.
<arraylist original list element;:
Tony
Tom
Jack
Mary
Even
---------------------------------
< ArrayList list after deleting an element:
Tony
Jack
Mary
Even
---------------------------------
(3) Modification of elements in ArrayList Note: The ArrayList method has a. Set method (variable. Set (index, Element);). You can modify the values in the list by this method. (index specifies the subscript, element specifies the value of the elements to be modified). Take a look at the following example.
Package Com.java.demo_9;import Java.util.arraylist;public class ArrayList {public static void main (string[] args) { ArrayList lis = new ArrayList (), Lis.add ("Tony"), Lis.add ("Tom"), Lis.add ("Jack"); Lis.add ("Mary"); Lis.add ("even"); System.out.println ("---------------------------------"); System.out.println ("<arraylist original list element;:"); for (int i = 0; I < lis.size (); i++) {String result = (string) lis.get (i); SYSTEM.OUT.PRINTLN (result);} System.out.println ("---------------------------------"); System.out.println ("< modified ArrayList list;:");//lis.remove ("Tom"); Lis.set (2, "Lucy"); for (int i = 0; I < lis.size (); i++) {String result = (string) lis.get (i); SYSTEM.OUT.PRINTLN (result);} System.out.println ("---------------------------------");}}
Result: The value in the 3rd subscript has been changed by the following results. Jack is now a change to Lucy.
<arraylist original list element;:
Tony
Tom
Jack
Mary
Even
---------------------------------
< modified arraylist list;:
Tony
Tom
Lucy
Mary
Even
---------------------------------
(3) Searching for elements in ArrayList
Note:. Contains () is used to determine whether an element is contained within another element. So here is the decision to determine if the found element is contained in the list. The return value aspect I directly uses is the printing method. It can also be returned in Boolean,return way.
Import Java.util.arraylist;public class TEs {public static void main (string[] args) {ArrayList lis = new ArrayList (); LIS.A DD ("Tony"), Lis.add ("Tom"), Lis.add ("Jack"), Lis.add ("Mary"), Lis.add ("even"), if (Lis.contains ("Vivian")) { System.out.println ("contains this element!");} ELSE{SYSTEM.OUT.PRINTLN ("The element does not exist!");}}
Result: Returns the result reference code. I found it. Print "contains this element, not found on the display element does not exist.
There are many ways to implement the above example, and this is only the most basic method of ArrayList. For reference only!
"Simple Version" Java ArrayList (add and revise)