当前位置:网站首页>Collections and Iterators
Collections and Iterators
2022-08-07 03:33:00 【l'amourХэ рутилин】
Collection
When programming, you can use an array to store multiple objects, but the length of the array cannot be changed. Once the array length is specified when the array is initialized, the length of the array is immutable.If you need to hold a varying amount of data, arrays are a bit powerless.Moreover, arrays cannot store data with mapping relationships. In order to store an indeterminate amount of data and data with mapping relationships, Java provides a collection class.Collection classes are mainly responsible for storing and holding other data, so collection classes are also called container classes.
The collection class is different from the array. The elements of the array can be either the value of the basic type or the object, and the collection can only store the object.
Java collection types are divided into Collection and Map, which are the root interfaces of Java collections, and these two interfaces contain some sub-interfaces or implementation classes.
Collection interface basic structure

Basic structure of Map interface

Collection common functions

Code Examples:
public static void main(String[] args) {ArrayList
Iterator
In program development, it is often necessary to traverse all elements in a collection.In response to this demand, JDK provides an interface Iteration is a way to traverse Collection elements.Before taking the element, first determine whether there is an element in the collection, and if so, take the element out.Continue to judge, if there is, continue to obtain elements until all elements in the collection are taken out.The technical term for this retrieval method is called Iterative.
boolean hasNext(): Determine if there is a next elementE next(): returns the next elementvoid remove(): delete the next elementGet iterator
Iterator iter = collection.iterator();Note:
Iterable: Iterable, indicating an iterable ability (provides a method to obtain an iterator)
Iterator: The iterator implements the iteration function (real iterator)
Using iterators
1. Get the iterator: iterator
2. Determine if there is a next element: hasNext
3. If it exists, get the next element: next
4. Repeat steps 2 and 3 until there is no next element
Code example:
public static void main(String[] args) {ArrayList arrayList = new ArrayList();arrayList.add(56341);arrayList.add(87465);arrayList.add(94864);Iterator iterator = arrayList.iterator();while (iterator.hasNext()){System.out.println(iterator.next());}}Note:
1. When the collection element is acquired, if there is no element in the collection, and the next method of the iterator is continued to be used, a java.util.NoSuchElementException will be thrown. There is no collection element exception.
2. During collection element acquisition, if you add or remove elements in the collection, it will not be able to continue to iterate, and ConcurrentModificationException will be thrown.
Enhanced for loop [foreach]
Enhanced for loop (also called for each loop) is an advanced for loop after JDK1.5, which is specially used to traverse arrays and collections.Its internal principle is actually an Iterator iterator, so in the process of traversal, you cannot add or delete elements in the collection.
Syntax:
for(variable type variable: array/collection){The traversed data is processed, and the data is stored in this variable}Tips: array name/collection name.for
Code example:
public static void main(String[] args) {ArrayList arrayList = new ArrayList();arrayList.add(56341);arrayList.add(87465);arrayList.add(94864);for (Object o : arrayList) {System.out.println(o);}}边栏推荐
- The sword refers to Offer II 029. Sorted circular linked list - pure linked list implementation
- [Swift] Add copy function to custom object
- 【RF】Radio Frequency Integrated Circuit and System Design
- haproxy实验
- The process of kafka-flink-mysql
- pat甲级考试+pat1051+1056
- [transfer] swig
- mysql8修改密码
- haproxy experiment
- CefSharp方法汇总
猜你喜欢

Rainwater automatic monitoring telemetry terminal

activiti7入门教程

Definition and operation process of OAuth2

微信小程序的民宿客房预订uniapp小程序

84-MongoDB高级介绍

Wechat applet online learning daily check-in and punch-in project source code introduction

TensorFlow学习记录(五):卷积神经网络

Web Vulnerability Scanner - Burpsuite Routine Test

Wonderful Review|Cloud Native Meetup Guangzhou Station

Implement caching mechanism using soft references
随机推荐
跨行求职数分的面试经验 + 未来职业规划
How to create a replica database
Coco data set analysis and reading method
1008: series summation
MQ的概述及优缺点及使用场景
JSP第一篇 -----JSP浅聊EL表达式第一篇: 基础操作,以及域搜索顺序以及EL隐式对象
LVS负载均衡集群
Polygon Ventures或有意与Genesis合作
406. 根据身高重建队列-排序+动态规划
OAuth2的定义和运行流程
82-FastDFS detailed explanation
STM32F103ZET6突然下载不了程序??
虚拟摄像头之四: 谁在调用 v4l2_camera_HAL 摄像头驱动
Chat room code backup
[Swift]对自定义对象添加复制功能
POST请求
Rainwater automatic monitoring telemetry terminal
【诡秘之主】真神篇
Scrollbar for TreeView in wpf
scala object class基础语法讲解