Collection ArrayList and common methods in Java

1, Create an empty collection object

Different from array, set provides a storage model with variable storage space, and the data capacity can be changed.

If you want to use ArrayList < > to store string types, write it as ArrayList < string >, that is, within angle brackets.

Here we take the string type as an example:

ArrayList<String> array = new ArrayList<String>();
System.out.println(array);
//The output result is: []

2, Common methods of ArrayList

The following common method examples use the empty collection object array we just created.

1. Add elements

Use the add method

Add elements to the end of the set.

Code example:

array.add("hello");
//The element hello is added to the empty collection array

System.out.println(array.add("hello"));
//The output is boolean type, and the element is added here, so the output is true

System.out.println(array);
//The element hello in the output set array, output style: [Hello]

Note: if you output array Add (xxx), the return value is of boolean type, and the output of true can be regarded as the element we have successfully added, so we don't have to output it, we can use it directly; The way to add elements with add is to add them successively from the end of the collection.

We can use the method of adding elements many times. Code example:

array.add("hello");
array.add("hey");
array.add("hi");

System.out.println(array);
//Output [hello,hey,hi]

Note: the output of the set has "[]", and each element is between "[]", and elements are separated by ",". It can be seen from the code that the addition method is added to the end of the queue in turn.

We can also directly specify the subscript and add the specified element to the corresponding subscript. Code example:

array.add("My");
array.add("is");
array.add("LiHua");
System.out.println(array);
//Output set array [my, is, Lihua]

array.add(index: 1, element:"name");
System.out.println(array);
//Output set array [my, name, is, Lihua]

Note: the index of the collection starts from 0, so the subscript is 1, and the element is added in the second position. Specify the location to add. If there are elements, step back one position in turn. In addition, the specified subscript cannot exceed the maximum index position of the set, otherwise the set index exceeds the limit and an error is reported.

2. Delete element

Use the remove method

You can delete the specified element or the element at the specified index position.

Delete the specified element code example:

array.add("My");
array.add("name");
array.add("is");
array.add("LiHua");
System.out.println(array);
//Output set array [my, name, is, Lihua]

array.remove("name");
System.out.println(array);
//Output set array [my, is, Lihua]

Code example for deleting an element at a specified index location:

array.add("My");
array.add("name");
array.add("is");
array.add("LiHua");
System.out.println(array);
//Output set array [my, name, is, Lihua]

array.remove(1);
System.out.println(array);
//Output set array [my, is, Lihua]

Note: the maximum index of the collection cannot be exceeded when deleting elements.

3. Modify elements

Using the set method

We can modify the element that specifies the index position.

Code examples are as follows:

array.add("My");
array.add("name");
array.add("is");
array.add("LiHua");
System.out.println(array);
//Output set array [my, name, is, Lihua]
        
System.out.println(array.set(3,"ZhangSan"));
//Output the modified element LiHua

System.out.println(array);
//Output set array [my, name, is, Zhangsan]

4. Return element

Use get method

Returns the element at the specified index position.

Code examples are as follows:

array.add("My");
array.add("name");
array.add("is");
array.add("LiHua");
System.out.println(array);
//Output set array [my, name, is, Lihua]

System.out.println(array.get(0));
System.out.println(array.get(1));
//Output My
//Output name

System.out.println(array);
//Output set array [my, name, is, Zhangsan]

Note: after using get to get the elements at the index position we specify, the original set will not be changed, that is, it will not be affected.

5. Number of elements

Using the size method

Returns the number of elements in the collection.

Code example:

array.add("My");
array.add("name");
array.add("is");
array.add("LiHua");
System.out.println(array);
//Output set array [my, name, is, Lihua]
        
System.out.println(array.size());
//The number of elements in the output set array is 4

Keywords: Java

Added by mcl on Sun, 06 Feb 2022 08:22:03 +0200