Home > Java > javaTutorial > Usage of ArrayList class in java

Usage of ArrayList class in java

巴扎黑
Release: 2016-12-02 09:31:53
Original
2023 people have browsed it

Usage of the ArrayList class in java:
Add: ①Add data "a" and "b" to ArrayList, usage:
List.add("a");
List.add("b");// In this case, "a" is the first data in the ArrayList's List, and "b" is the second, which means that data will be added sequentially into the ArrayList without marking the position number.
②Add a data after the Nth data, usage:
List.add(2, "c");//Add the string data "c" after the second data; at this time the ArrayList must have enough If the data is List.(4, "d"); a java.lang.lndexOutOfBoundsException exception will occur because there is no data at position three.
③Add all the data in one ArrayList to another ArrayList. Usage:
ArrayList< > r1=new ArrayList< >();
ArrayList< > r2=new ArrayList< >();
r1.add(r2);//will allow the data of r2 to be added directly r1, r1 data is in front and r2 data is in the back.
④Add the data in one ArrayList to the Nth element of another ArrayList. Usage:
r1.add(2,r2);
Query the size of ArrayList, size:
int theSize=r1.size();
Query specific element contains:
Boolean isIn=r1.contains(1);
// Query whether 1 in the ArrayList refers to an object. If it refers to an object, it returns true, if not, it returns false.
Determine whether the ArrayList is empty, isEmpty:
boolean empty=r1.isEmpty();
//If it is empty, return true, otherwise return false.
get() returns the object of the current index parameter
r1.get(1);
returns the object referenced by 1.
remove() deletes the element,
r1.remove(1); deletes the object referenced by 1.
The ArrayList class has more complex methods, which are not mentioned in the book, so I won’t mention them for now.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template