当前位置:网站首页>List set Introduction & common methods
List set Introduction & common methods
2022-06-24 20:57:00 【Platonic】
java.util.list Interface extends Collection Interface .
list Interface features :
- 1. Ordered interface , The order in which the elements are stored is the same as the order in which they are taken out ( Storage 123 Take out 123)
- 2. There is an index , Contains some indexed methods
- 3. Allows the storage of duplicate elements
Common methods are :
List Method with index in interface ( specific )
--public void add(int index,E element): Will specify the element , Add to the specified location in the collection
--public E get(int index): Returns the specified element in the collection
--public E remove(int index): Remove the element at the specified location in the list , What is returned is the removed element ,
--public E set(int index,E element): Replace the element at the specified location in the collection with the specified element , Returns the pre update element of the value ,
Be careful :
When operating the index, be sure to prevent the index from crossing the boundary ;
Demonstration of examples :
public class Demo01LIst {
public static void main(String[] args) {
// Create a list A collection of objects , polymorphic
ArrayList<String> list=new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
// Print set
System.out.println(list);
// --public void add(int index,E element): Will specify the element , Add to the specified location in the collection
// stay c and d Add an element between
list.add(2,"bbb");
System.out.println(list);
//-public E get(int index): Returns the specified element in the collection
String s = list.get(4);
System.out.println(s);
// --public E remove(int index): Remove the element at the specified location in the list , What is returned is the removed element ,
String remove = list.remove(1);
System.out.println(" What was removed was :"+remove);
// --public E set(int index,E element): Replace the element at the specified location in the collection with the specified element , Returns the pre update element of the value ,
String set = list.set(0, "aaa");
System.out.println(" The change is :"+set);
//list There are three ways to traverse sets
for (int i = 0; i < list.size(); i++) {
String s1 = list.get(i);
System.out.println(s1);
}
// Use iterators to traverse
Iterator<String> it = list.iterator();
while(it.hasNext()){
String next = it.next();
System.out.println(next);
}
// Use enhancements for Loop traversal
for (String l :list) {
System.out.println(l);
}
}
}Results, :
[a, b, c, d, e]
[a, b, bbb, c, d, e]
d
What was removed was :b
The change is :a
aaa
bbb
c
d
e
aaa
bbb
c
d
e
aaa
bbb
c
d
e边栏推荐
- Wait for the victory of the party! After mining ebb tide, graphics card prices plummeted across the board
- 微信小程序中使用vant组件
- Undo log and redo log must be clear this time
- Selenium crawl notes
- 用手机摄像头就能捕捉指纹?!准确度堪比签字画押,专家:你们在加剧歧视
- Bean lifecycle flowchart
- Vant component used in wechat applet
- Leetcode(455)——分发饼干
- What does virtualization mean? What technologies are included? What is the difference with private cloud?
- Stackoverflow 年度报告 2022:开发者最喜爱的数据库是什么?
猜你喜欢

Stackoverflow 年度报告 2022:开发者最喜爱的数据库是什么?

云计算发展的 4 个阶段,终于有人讲明白了

Hongxiang Yunteng is compatible with dragon lizard operating system, and the product runs stably

天天鉴宝暴雷背后:拖欠数千万、APP停摆,创始人预谋跑路?

Stackoverflow annual report 2022: what are developers' favorite databases?

Apple doesn't need money, but it has no confidence in its content

Dongyuhui is not enough to bring goods to "rescue" live broadcast

两位湖南老乡,联手干出一个百亿IPO

Otaku can't save yuan universe

Wechat applet custom tabbar
随机推荐
Undo log and redo log must be clear this time
消息称腾讯正式宣布成立“XR”部门,押注元宇宙;谷歌前 CEO:美国即将输掉芯片竞争,要让台积电、三星建更多工厂...
物联网?快来看 Arduino 上云啦
Visitor model -- generation gap between young and middle-aged people
开放可编程基础设施(OPI)项目,重新定义DPU/IPU
基于QT+MySQL的相机租赁管理系统
苹果、微软、谷歌不再掐架,今年要合力干一件大事
Otaku can't save yuan universe
浅谈MySql update会锁定哪些范围的数据
图像PANR
云计算发展的 4 个阶段,终于有人讲明白了
Open programmable infrastructure (OPI) project, redefining dpu/ipu
Docker deploy mysql5.7
Mr. Hu Bo, CIO of weiduomei, a scientific innovator: digitalization is a bloodless revolution, and the correct answer lies in the field of business
Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading
使用gorm查询数据库时reflect: reflect.flag.mustBeAssignable using unaddressable value
微信小程序自定义tabBar
Where is 5g really powerful? What is the difference with 4G?
Bridging mode -- law firm
[cloud resident co creation] ModelBox draws your own painting across the air