Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the common methods of list in java

Shulou Source: shulou.com Published: 2022-06-02 18:48:18 09月14日 Update

This article mainly introduces what are the common methods of list in java. It is very detailed and has a certain reference value. Friends who are interested must finish it!

1.list adds, gets, and deletes elements.

Add: .add (e)

Get: .get (index)

Delete: .remove (index); delete according to the index;

.remove (Object o); delete by element content

Code example:

List str=new ArrayList ()

Str.add ("A"); / / Index is 0 / / .add (e)

Str.add ("B"); / / Index is 1

Str.add ("C"); / / Index is 2

Str.add ("D"); / / Index is 3

Str.add ("E"); / / Index is 4

Str.remove (3); / / .remove (index)

Str.remove ("E"); / / .remove (Object o)

String per= ""

Per=str.get (1)

System.out.println (per); / .get (index)

For (int I = 0; I < str.size (); iTunes +) {

System.out.println (str.get (I)); / / .get (index)

}

Whether an element is included in the 2.list.

Method: .resume (Object o); returns true or false

Code example:

List str=new ArrayList ()

Str.add ("A")

Str.add ("B")

Str.add ("C")

/ / for loop traverses list

For (int I = 0; I < str.size (); iTunes +) {

System.out.println (str.get (I))

}

String word= "A"

/ / true or false

System.out.println ("does str contain A:" + str.contains (word))

If (str.contains (word)) {

System.out.println ("including A")

} else {

System.out.println ("not including A")

}

Using index to change or replace element value in 3.list

.set (index, element); / / place the element element in the list where the index is index, replacing the original element

.add (index, element); / / place the element element in the list with the index index, and move the element of the original location back one bit

Code example:

String a = "Zhang San", b = "Li Si", c = "Wang Wu", d = "Zhao Liu"

List str=new ArrayList ()

Str.add (a)

Str.add (b)

Str.add (c)

Str.set (0, d); / / .set (index, element); / / put d Zhao Liu to the position in list with index 0, replacing a Zhang San str.add (1, c); / / .add (index, element); / / put c Wang Wu into the position with index 1 in list, and move back one for (int I = 0) from the original position I < str.size (); iTunes +) {

System.out.println (strI)

}

Query in 4.list determines the index of the element

.indexOf ()

/ / cycle from the 0th element of the sequence (List), and call the equals () method of each element to compare with the parameter object. If the equals () method of an element returns a value of true, then the index position of the current element is returned as a result. If there is more than one duplicate element in the sequence, only the value of the index where the repeating element first appeared is returned.

LastIndexOf ()

/ / in contrast to the indexOf () method, it returns the value of the last index position of an element, that is, it traverses from the end of the sequence to the head of the line.

Note: if the parameter objects of both methods do not appear in the sequence, then both methods return-1.

5. Split the collection into new collections according to the index

.subList (fromIndex, toIndex);

.size (); this method gets the sum of the number of elements in list

Code example:

List phone=new ArrayList ()

Phone.add ("Samsung"); / / Index is 0

Phone.add ("Apple"); / / Index is 1

Phone.add ("hammer"); / / Index is 2

Phone.add ("Huawei"); / / Index is 3

Phone.add ("Xiaomi"); / / Index is 4

/ / the original list is used to traverse

For (String pho:phone) {

System.out.println (pho)

}

/ / generate a new list

Phone=phone.subList (1,4); / / .sublist (fromIndex, toIndex) / / rebuilds a list using objects with indexes 1-4, but does not contain elements with index 4, 4-1-3

For (int I = 0; I < phone.size ()) {/ / phone.size () this method obtains the sum of the number of elements in list

System.out.println ("the elements contained in the new list are" + phone.get (I))

}

6. Compare all the elements in the two list

.equals () .hashCode ()

/ / the equals method of two equal objects must be true, but two objects with equal hashcode are not necessarily equal objects.

7. Determine whether the list is empty

.isEmpty ()

/ / returns true if it is empty, and false if it is not empty.

8. Returns the Iterator collection object

.out.println (Obj)

9. Convert a collection to a string

.toString ()

10. Convert a collection to an array

.toArray ()

11. Deweighting

.frequency (Obj, element)

/ / the number of times the query element appears in Obj

The above is all the content of the article "what are the common methods of list in java". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Element index method location object two code sequence example content common use parameter article Zhang San Li Si Wang Wu backward loop query generate Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Tech Info OPPO Reno Docker MariaDB