集合
来源:互联网 发布:帝国cms 伪静态 编辑:程序博客网 时间:2024/05/16 14:58
使用ArrayList集合,对其添加100个不同的元素:1.使用add()方法将元素添加到ArrayList集合对象中;2.调用集合的iterator()方法获得Iterator对象,并调用Iterator的hasNext()和next()方法,迭代的读取集合中的每个元素;3.调用get()方法先后读取索引位置为50和102的元素,要求使用try-catch结构处理下标越界异常;要求:1. 使用非泛型版的集合。本课程按照大纲要求讲授非泛型版的集合框架。2. 使用面向对象方式解决问题,不允许把所有处理逻辑都写在main()方法中*/
package 集合;
import java.util.ArrayList;
import java.util.Iterator;
public class ArraylistTest {
public static void main(String[] args) {
ArrayList array = new ArrayList();
System.out.println("打印ArrayList中的所有元素:");//添加100个不同的元素
for(int i=1;i<=100;i++){
array.add(i);
}
Iterator it = array.iterator();//Iterator 迭代数组
while(it.hasNext()){
System.out.println(it.next()); }
try {
System.out.println("调用get()读取索引位置为50:");
System.out.println(array.get(50));
System.out.println("调用get()读取索引位置为102:");
System.out.println(array.get(102));
}
catch (IndexOutOfBoundsException e) {
System.out.println("数据越界");
}
}
}
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- Could not autowire field
- yum安装nginx
- 1073. 多选题常见计分法(20) PAT乙级真题
- python里使用enum库枚举类型时枚举值唯一
- smb&nfs
- 集合
- android studio集成极光推送
- jqgrid编辑行数据及保存行数据的取值
- LeetCode 692. Top K Frequent Words
- 【mysql 进阶篇】学习 HAVING 子句的真正价值
- [linux]挂载硬盘安装mysql和mono进行.net开发
- Enum枚举的原理及各种使用方法
- 那只我没见过的小狗崽,希望你能安好
- 计算机视觉学习笔记:图像特征提取