There are three forms of creating collection objects
1. There is no need to pass construction parameters, just new. At this time, the underlying array is an empty array.
2. Construct parameters need to pass a value of an INT type for the length of the underlying array
3. Construct parameters need to convey the value of a collection set type to place the data in the set in the current set.
Create an array in the first way
package goodStudy;
import ;
public class goodStudy{
public static void main(String[] args) {
ArrayList arrList = new ArrayList();
//The output is an empty array
(arrList);
}
}
Add data to the collection add
Package Goodstudy;
// ArrayList in Java-> Util package
Import;
Public Class Goodstudy {
Public Static Void Main (String [] args) {{
Arraylist arrlist = New ArrayList ();
("Add a data");
// The output is an empty array
(Arrlist);
}
}
Add data to the collection add('Add to which position', 'Added value')
Arraylist arrlist = New ArrayList ();
("Hey 01");
("Hee hee 02");
("Haha 03");
(0, "item 1");
// The output is: Arrlist: [No. 1, hehe 01, hee hee 02, haha 03]
("Arrlist:" + Arrlist);
Add another collection addall
ArrayList arrList = new ArrayList();
("Heyhey01");
ArrayList otherArr = new ArrayList();
("otner_01");
(arrList);
//The output is: otherArr:[otner_01, hehe01]
("otherArr:" + otherArr);
The default length of the collection is 10
When adding data, if there is no data in the collection, the bottom layer will create an array with a length of 10
If the number of items added to the set is greater than the length of the set, will an error be reported?
Package Goodstudy;
// ArrayList in Java-> Util package
Import;
Public Class Goodstudy {
Public Static Void Main (String [] args) {{
Arraylist arrlist = New ArrayList (3);
("Hey 01");
("Hee hee 02");
("Haha 03");
(") 04");
// Will you report an error?
(Arrlist);
}
}
No error will be reported. Why?
Because: Created a larger array at the bottom
Get the length of the collection size
ArrayList arrList = new ArrayList();
("Heyhey01");
("heehee02");
("Haha03");
("Bleak04");
(());
// output 4
Get the data of the specified location in the set.
ArrayList arrList = new ArrayList();
("Heyhey01");
("heehee02");
("Haha03");
("呲呲04");
((2));
// Output time: Haha 03
Iterate through the data in the collection for loop
public class goodStudy{
public static void main(String[] args) {
ArrayList arrList = new ArrayList();
("Heyhey01");
("heehee02");
("Haha03");
("Bleak04");
for(int i =0;i<();i++){
("The current value is" + (i));
}
}
}
Traverse the data in the collection for loop [regardless of the order of the collection] special for loop
When we traverse the collection, if we do not consider the sequence of the collection, we can use the following for object loop
ArrayList arrList = new ArrayList();
("Heyhey01");
("heehee02");
("Haha03");
("呲呲04");
for(Object obj:arrList ){
("The current value is" + obj);
}
Update data set
('Location of new data', 'update value')
The return value of Set represents: the current data before the update
ArrayList arrList = new ArrayList();
("Heyhey01");
("heehee02");
("Haha03");
("Bleak04");
//The return value of set represents: the data before the current item is updated
Object oldValue = (0, "Item 1");
//The output is hey 01
(oldValue);
(arrList);
Delete data remove
(The location of the data to be deleted)
The return value of Remove represents: the deleted data
Arraylist arrlist = New ArrayList ();
("Hey 01");
("Hee hee 02");
("Haha 03");
(") 04");
Object Oldvalue = (0);
// Delete the value before: Hehe 01
("Delete the value before:" + Oldvalue);
// Arrlist: [Hee hee 02, haha 03, 呲呲 04]
("Arrlist:" + Arrlist);
Clear the collection and determine whether the collection is empty
ArrayList arrList = new ArrayList();
("Heyhey01");
// Clear this collection
();
// Determine whether the collection is empty
("Whether the collection is empty:" + ());
Removeal () method removes all elements that exist in another collection from one set.
ArrayList arrList = new ArrayList();
("Heyhey01");
("Haha02");
ArrayList list = new ArrayList();
("Haha02");
// Delete elements existing in the list collection from the arrList collection
(list);
// Output ["Heyhey01"]
("arrList: "+ arrList);
Whether an element contains, returns a Boolean value
Arraylist arrlist = New ArrayList ();
("Hey 01");
// Judging whether the array arrist contains the element of hehe 01
Boolean flag = ("Hey 01");
// The output is true
("Flag:" + Flag);
The first position of a certain element in the collection indexof
indexOf: The position (index) of the first occurrence of the element in the collection. If the element does not exist, the returned value is -1
ArrayList arrList = new ArrayList();
("Heyhey01");
("Haha02");
// Find where an element is in the collection
int index = ("Haha02");
//The output is 1
(index);
int notIndex = ("Haha");
//The output is -1
(notIndex);
The position of the last occurrence of an element in the collection lastIndexOf
It is used to find the position of the last time (index) that the specified element appears in the list
Similar to the indexof method,
If the element is found, the LastIndexof method will return its index (starting from 0), and if not found, return -1.
toArray converts a collection into an array
ArrayList arrList = new ArrayList();
("Heyhey01");
("Haha02");
Object[] arr = ();
(arr);
A collection of cloning [There are 3 forms in the creation of a collection object]
public class goodStudy{
public static void main(String[] args) {
ArrayList arrList = new ArrayList();
("Heyhey01");
("Haha02");
// Clone an item
ArrayList otherList = new ArrayList(arrList);
(otherList);
(0, "First Hahaha");
//The output is: otherList[first hahaha, haha02]
("otherList" +otherList);
//The output is: arrList[Heyhey01, Haha02]
("arrList"+ arrList);
}
}