当前位置:网站首页>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边栏推荐
- Berkeley, MIT, Cambridge, deepmind and other industry leaders' online lectures: towards safe, reliable and controllable AI
- 消息称腾讯正式宣布成立“XR”部门,押注元宇宙;谷歌前 CEO:美国即将输掉芯片竞争,要让台积电、三星建更多工厂...
- 《梦华录》“超点”,鹅被骂冤吗?
- Bean lifecycle flowchart
- 二叉树的基本性质与遍历
- C language to realize mine sweeping (simple version)
- VXLAN 与 MPLS:从数据中心到城域以太网
- Memo mode - game archiving
- Leetcode(135)——分发糖果
- DX12引擎开发课程进度-这个课程到底讲到哪里了
猜你喜欢

The four stages of cloud computing development have finally been clarified

微信小程序中使用vant组件

Material management system based on SSM (source code + document + database)

海泰前沿技术|隐私计算技术在医疗数据保护中的应用

Leetcode (135) - distribute candy

Background of master data construction

Basic properties and ergodicity of binary tree

Prototype mode -- clone monster Army

物联网?快来看 Arduino 上云啦

字节、腾讯也下场,这门「月赚3000万」的生意有多香?
随机推荐
网络安全审查办公室对知网启动网络安全审查
Steps of JMeter performance test
[multi thread performance tuning] multi thread lock optimization (Part 1): optimization method of synchronized synchronization lock
Berkeley, MIT, Cambridge, deepmind et d'autres grandes conférences en ligne: vers une IA sûre, fiable et contrôlable
Summary of idea practical skills: how to rename a project or module to completely solve all the problems you encounter that do not work. It is suggested that the five-star collection be your daughter
"Super point" in "Meng Hua Lu", is the goose wronged?
Material management system based on SSM (source code + document + database)
Is the waiting insurance record a waiting insurance evaluation? What is the relationship between the two?
[suggested collection] time series prediction application and paper summary
Undo log and redo log must be clear this time
Smooth live broadcast | analysis of key technologies for live broadcast pain points
Jd.com: how does redis implement inventory deduction? How to prevent oversold?
16 excellent business process management tools
京东一面:Redis 如何实现库存扣减操作?如何防止商品被超卖?
What does virtualization mean? What technologies are included? What is the difference with private cloud?
Image panr
伯克利、MIT、剑桥、DeepMind等业内大佬线上讲座:迈向安全可靠可控的AI
RF_ DC system clock setting gen1/gen2
[普通物理] 光栅衍射
“拯救”直播带货,一个董宇辉还不够