当前位置:网站首页>Summary of common methods of ArrayList
Summary of common methods of ArrayList
2022-07-27 12:39:00 【Rippling rippling】
aggregate
Concept :
Collections are also called containers , be used for Storage 、 extract 、 Delete data .( That is, increase , Delete , lookup ).JDK Provided collection API All contained in java.util In bag .
It's like an array .
Branch of set :
The relationship is as shown in the figure :
ArrayList
Common methods :
1.add()
effect :
Used to direct to List Add elements to the collection container .
Is a generic : Used to constrain the data types of elements in the collection container , If omitted , The default is Object type , because Object Is the parent of all classes .
example :
package list;
import java.util.ArrayList;
import java.util.Iterator;
public class Test {
public static void main(String [] args) {
ArrayList <String>list =new ArrayList<String>();
list.add("Tom");
System.out.println(list);
}
}
result :
[Tom]
2.size()
effect :
Used to get how many elements are in the collection .
example :
package list;
import java.util.ArrayList;
import java.util.Iterator;
public class Test {
public static void main(String [] args) {
ArrayList <String>list =new ArrayList<String>();
list.add("Tom");
int length=list.size();
System.out.println(length);
}
}
result :
1
3.get();
effect :
Gets the specified index ( from 0 Start ) The element of location .
example :
package list;
import java.util.ArrayList;
import java.util.Iterator;
public class Test {
public static void main(String [] args) {
ArrayList <String>list =new ArrayList<String>();
list.add("Tom");
list.add("lili");
list.add("array");
System.out.println(list.get(2));
}
}
result :
array
4.add( __, " ");
effect :
Add the element in the specified position , The element in its original position moves back .
example :
package list;
import java.util.ArrayList;
import java.util.Iterator;
public class Test {
public static void main(String [] args) {
ArrayList <String>list =new ArrayList<String>();
list.add("Tom");
list.add("lili");
list.add("array");
list.add(0, "Kate");
System.out.println(list.get(2));
}
}
result :
lili
5.set(int i, Object element)
effect :
Replace the element at the specified location .
example :
package list;
import java.util.ArrayList;
import java.util.Iterator;
public class Test {
public static void main(String [] args) {
ArrayList <String>list =new ArrayList<String>();
list.add("Tom");
list.add("lili");
list.add("array");
list.set(2, "Lucy");
System.out.println(list.get(2));
}
}
result :
Lucy
6.clear()
effect :
Empty List All elements in the collection .
example :
package list;
import java.util.ArrayList;
import java.util.Iterator;
public class Test {
public static void main(String [] args) {
ArrayList <String>list =new ArrayList<String>();
list.add("Tom");
list.add("lili");
list.add("array");
list.set(2, "Lucy");
System.out.println(list.get(2));
list.clear();
System.out.println(list);
}
}
result :
Lucy
[]
7.isEmpty()
effect :
Used to determine whether there are elements in the collection container , Returns a Boolean type .
If No, , be true.
Yes Then for flase.
example :
package list;
import java.util.ArrayList;
import java.util.Iterator;
public class Test {
public static void main(String [] args) {
ArrayList <String>list =new ArrayList<String>();
list.add("Tom");
list.add("lili");
list.add("array");
list.set(2, "Lucy");
System.out.println(list.get(2));
boolean flag = list.isEmpty();
System.out.println(flag);
list.clear();
System.out.println(list);
boolean flag2 = list.isEmpty();
System.out.println(flag2);
}
}
result :
Lucy
false
[]
true
8.contains(Object o)
effect :
Used to judge whether the collection container contains all parameter elements .
Without this element, it returns flase.
With the return true.
example :
package list;
import java.util.ArrayList;
import java.util.Iterator;
public class Test {
public static void main(String [] args) {
ArrayList <String>list =new ArrayList<String>();
list.add("Tom");
list.add("lili");
list.add("array");
list.set(2, "Lucy");
boolean flag = list.contains("Jim");
System.out.println(flag);
}
}
result :
false
reflection : What is the judgment process ?
We can hold Ctrl Click to enter contains Inside , Get the internal code as :
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
public int indexOf(Object o) {
if (o == null) {
for (int i = 0; i < size; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = 0; i < size; i++)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
o That is, the element you want to find jim. Because after entering o Defined as Object type , So at this time, he is the object of transformation . Because the element searched is not empty , So to enter else Branch . Get into if When the sentence is ,o.equals(elementData[i]) This code looks like the calling parent Object The sentence in , It's actually called String In type . This forms a polymorphism .
Polymorphism is to make it find all types , Because not all of your collections are of a specific type .
9.remove()
effect :
Delete specified index ( from 0 Start ) The element of location , And return the element to , And the following elements move forward .
example :
package list;
import java.util.ArrayList;
import java.util.Iterator;
public class Test {
public static void main(String [] args) {
ArrayList <String>list =new ArrayList<String>();
list.add("Tom");
list.add("lili");
list.add("array");
list.set(2, "Lucy");
String str = list.remove(0);
System.out.println(list);
}
}
result :
[lili, Lucy]
10.remove(Object o)
effect :
Delete List In the collection xx Elements , return boolean type , And move the following elements forward .
notes :
Attention comparison 9 and 10 The difference between .
example :
package list;
import java.util.ArrayList;
import java.util.Iterator;
public class Test {
public static void main(String [] args) {
ArrayList <String>list =new ArrayList<String>();
list.add("Tom");
list.add("lili");
list.add("array");
list.set(2, "Lucy");
String str = list.remove(0);
System.out.println(list);
list.remove("lili");
System.out.println(list.get(0));
}
}
result :
[lili, Lucy]
Lucy
11.iterator()
effect :
take List Elements in the collection go to iterator variable
principle :
Judge the present “ The pointer ” Are there any other elements below , If there is an element under the pointer , Then move the pointer and get the element at the corresponding position .
example :
package list;
import java.util.ArrayList;
import java.util.Iterator;
public class Test {
public static void main(String [] args) {
ArrayList <String>list =new ArrayList<String>();
list.add("Tom");
list.add("lili");
list.add("array");
list.set(2, "Lucy");
String str = list.remove(0);
System.out.println(list);
list.remove("lili");
System.out.println(list.get(0));
Iterator<String> iterator = list.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
result :
[lili, Lucy]
Lucy
Lucy
Traversal of the set
Three methods :
The first one is : Conventional methods
package list;
import java.util.ArrayList;
import java.util.Iterator;
public class Test {
public static void main(String [] args) {
ArrayList <String>list =new ArrayList<String>();
list.add("Tom");
list.add("lili");
list.add("array");
list.set(2, "Lucy");
for(int i=0;i<list.size();i++) {
System.out.println(list.get(i));
}
}
}
The second kind :for-each
package list;
import java.util.ArrayList;
import java.util.Iterator;
public class Test {
public static void main(String [] args) {
ArrayList <String>list =new ArrayList<String>();
list.add("Tom");
list.add("lili");
list.add("array");
list.set(2, "Lucy");
for(String name:list) {
System.out.println(name);
}
}
}
The third kind of : utilize iterator Method
package list;
import java.util.ArrayList;
import java.util.Iterator;
public class Test {
public static void main(String [] args) {
ArrayList <String>list =new ArrayList<String>();
list.add("Tom");
list.add("lili");
list.add("array");
list.set(2, "Lucy");
Iterator<String> iterator = list.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
边栏推荐
- Current situation and development trend of accounting computerization
- 2021-3-23-meituan-regular sequence
- 开关量输入输出模块DAM-5055
- 如何获取Class类对象
- 「游戏引擎 浅入浅出」4.1 Unity Shader和OpenGL Shader
- 初学者入门:使用WordPress搭建一个专属自己的博客
- About the problem that the onapplicationevent method of the custom listener is executed multiple times
- 详述HashSet的add方法
- (07) flask is OK if you have a hand -- flask Sqlalchemy
- Watermelon book chapter 3 (first & second)
猜你喜欢

HDU1698_ Just a Hook

Chapter 10 enumeration classes and annotations

20210518-Cuda

多表查询

最强分布式锁工具:Redisson

Watermelon book chapter 3 (first & second)

ArrayList常用方法总结

Detail throw and throws

What should I do if I can't see any tiles on SAP Fiori launchpad?
![[database data recovery] a data recovery case in which the disk partition where the SQL Server database is located is insufficient and an error is reported](/img/8a/478209854cb5ce139afc338d106d36.png)
[database data recovery] a data recovery case in which the disk partition where the SQL Server database is located is insufficient and an error is reported
随机推荐
Chapter 12 generics
Soft core microprocessor
Switching value input and output module dam-5055
POJ1611_ The Suspects
ArrayList常用方法总结
2021-3-23-meituan-regular sequence
Uniapp video video playback is not completed. It is forbidden to drag the progress bar fast forward
Set接口
2021-3-17-byte-hu Pai
Recursive method | Fibonacci sequence
Openpyxl drawing area map
Lambda 表达式
详述throw与throws
数据湖(二十):Flink兼容Iceberg目前不足和Iceberg与Hudi对比
The sparksubmit. Main () method submits external parameters and remotely submits the standalone cluster task
I/o instance operation
Redistemplate cannot get the value according to the key
An overview of kernel compilation system
Finally, I was ranked first in the content ranking in the professional field. I haven't been tired in vain during this period. Thanks to CSDN's official platform, I'm lucky and bitter.
NFT mall /nft blind box / virtual blind box /nft transaction / customizable second opening