当前位置:网站首页>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边栏推荐
- 【建议收藏】时间序列预测应用、paper汇总
- Stop using system Currenttimemillis() takes too long to count. It's too low. Stopwatch is easy to use!
- Mapstacks: data normalization and layered color layer loading
- 浅谈MySql update会锁定哪些范围的数据
- 物联网?快来看 Arduino 上云啦
- 传统的IO存在什么问题?为什么引入零拷贝的?
- Stackoverflow annual report 2022: what are developers' favorite databases?
- The four stages of cloud computing development have finally been clarified
- 物聯網?快來看 Arduino 上雲啦
- 得物多活架构设计之路由服务设计
猜你喜欢

Agency mode -- Jiangnan leather shoes factory

网络安全审查办公室对知网启动网络安全审查

Intermediary model -- collaboration among departments

Grating diffraction

苹果、微软、谷歌不再掐架,今年要合力干一件大事

Behind Tiantian Jianbao storm: tens of millions in arrears, APP shutdown, and the founder's premeditated plan to run away?

微信小程序中使用vant组件

使用gorm查询数据库时reflect: reflect.flag.mustBeAssignable using unaddressable value

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

Builder mode -- Master asked me to refine pills
随机推荐
Where is 5g really powerful? What is the difference with 4G?
Dx12 engine development course progress - where does this course go
红象云腾完成与龙蜥操作系统兼容适配,产品运行稳定
CVPR 2022缅怀孙剑!同济、阿里获最佳学生论文奖,何恺明入围
Stop using system Currenttimemillis() takes too long to count. It's too low. Stopwatch is easy to use!
主数据建设的背景
Learn together and make progress together. Welcome to exchange
使用gorm查询数据库时reflect: reflect.flag.mustBeAssignable using unaddressable value
Handling of garbled JMeter response data - three solutions
A/B测试助力游戏业务增长
Grating diffraction
Material management system based on SSM (source code + document + database)
The largest DPU manufacturer in history (Part 1)
I just purchased a MySQL database and prompted that there are already instances. The console login instance needs to provide a database account. How do I know the database account.
科创人·味多美CIO胡博:数字化是不流血的革命,正确答案藏在业务的田间地头
浅谈MySql update会锁定哪些范围的数据
VMware virtual machine setting static IP
等保备案是等保测评吗?两者是什么关系?
Use the transparent [x] cross button image in the dialog
基于QT+MySQL的相机租赁管理系统