当前位置:网站首页>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边栏推荐
- 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.
- Set up your own website (14)
- Vxlan and MPLS: from data center to Metro Ethernet
- Procedural life: a few things you should know when entering the workplace
- IDEA Dashboard
- 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
- 首个大众可用PyTorch版AlphaFold2复现,哥大开源OpenFold,star量破千
- Selenium crawl notes
- Apple, Microsoft and Google will no longer fight each other. They will work together to do a big thing this year
- The JS method parameter passed a number beginning with 0. A magical problem occurred and bothered me for a long time
猜你喜欢

How Fiddler works

Sequence stack version 1.0

What are the problems with traditional IO? Why is zero copy introduced?

I feel that I am bald again when I help my children with their homework. I feel pity for my parents all over the world

Mapstacks: data normalization and layered color layer loading

微信小程序中使用vant组件

"Super point" in "Meng Hua Lu", is the goose wronged?

Several common command operations in win system

Berkeley, MIT, Cambridge, deepmind et d'autres grandes conférences en ligne: vers une IA sûre, fiable et contrôlable

微信小程序自定义tabBar
随机推荐
Basic operation of sequence table
C語言實現掃雷(簡易版)
Coinbase will launch the first encryption derivative for individual investors
Grating diffraction
Material management system based on SSM (source code + document + database)
Stackoverflow 年度报告 2022:开发者最喜爱的数据库是什么?
How to apply agile development ideas to other work
Hongxiang Yunteng is compatible with dragon lizard operating system, and the product runs stably
Popupwindow touch event transparent transmission scheme
I feel that I am bald again when I help my children with their homework. I feel pity for my parents all over the world
微信小程序中使用vant组件
Intermediary model -- collaboration among departments
Background of master data construction
Otaku can't save yuan universe
DX12引擎开发课程进度-这个课程到底讲到哪里了
Apple doesn't need money, but it has no confidence in its content
How Fiddler works
顺序栈遍历二叉树
刚购买了一个MYSQL数据库,提示已有实例,控制台登录实例要提供数据库账号,我如何知道数据库账号。
Is the waiting insurance record a waiting insurance evaluation? What is the relationship between the two?