关于集合的遍历
来源:互联网 发布:淘宝游戏专营类目 编辑:程序博客网 时间:2024/06/05 07:37
A:存储字符串并遍历
import java.util.Collection;
import java.util.ArrayList;
import java.util.Iterator;
public class CollectionDemo {
public static void main(String[] args) {
//创建集合对象
Collection c = new ArrayList();
//创建并添加元素
c.add("hello");
c.add("world");
c.add("java");
//迭代器遍历集合
Iterator it = c.iterator();
while(it.hasNext()) {
String s =(String) it.next();
System.out.println(s);
}
}
}
B:存储自定义对象并遍历
public class Student {
private String name;
private int age;
public Student(String name,int age) {
this.name = name;
this.age = age;
}
}
import java.util.Collection;
import java.util.ArrayList;
import java.util.Iterator;
public class StudentDemo {
public static void main(String[] args) {
//创建集合对象
Collection c = new ArrayList();
//创建学生对象
Student s1 = new Student("二蛋",27);
Student s2 = new Student("三蛋",30);
Student s3 = new Student("四蛋",30);
Student s4 = new Student("五蛋",25);
Student s5 = new Student("六蛋",16);
//添加元素
c.add(s1);
c.add(s2);
c.add(s3);
c.add(s4);
c.add(s5);
//迭代器遍历集合
Iterator it = c.iterator();
while(it.hasNext()) {
Student s = (Student)it.next();
System.out.println(s.getName()+"------------"+s.getAge());
}
}
}
import java.util.Collection;
import java.util.ArrayList;
import java.util.Iterator;
public class CollectionDemo {
public static void main(String[] args) {
//创建集合对象
Collection c = new ArrayList();
//创建并添加元素
c.add("hello");
c.add("world");
c.add("java");
//迭代器遍历集合
Iterator it = c.iterator();
while(it.hasNext()) {
String s =(String) it.next();
System.out.println(s);
}
}
}
B:存储自定义对象并遍历
public class Student {
private String name;
private int age;
public Student(){
}
public Student(String name,int age) {
this.name = name;
this.age = age;
}
public void getName(){
this.name=name;
}
public void getAge(){
this.age=age;
}
public String setName(){
return name;
}
public int setAge(){
return age;
}
}
import java.util.Collection;
import java.util.ArrayList;
import java.util.Iterator;
public class StudentDemo {
public static void main(String[] args) {
//创建集合对象
Collection c = new ArrayList();
//创建学生对象
Student s1 = new Student("二蛋",27);
Student s2 = new Student("三蛋",30);
Student s3 = new Student("四蛋",30);
Student s4 = new Student("五蛋",25);
Student s5 = new Student("六蛋",16);
//添加元素
c.add(s1);
c.add(s2);
c.add(s3);
c.add(s4);
c.add(s5);
//迭代器遍历集合
Iterator it = c.iterator();
while(it.hasNext()) {
Student s = (Student)it.next();
System.out.println(s.getName()+"------------"+s.getAge());
}
}
}
0 0
- 关于集合的遍历
- 关于Map集合的遍历
- 关于JAVA中Map集合的遍历
- 关于map集合的循环遍历
- 关于Map集合的遍历方式
- JDK8 中关于集合的遍历
- 关于集合的遍历的同时增删问题
- 安卓开发----关于集合遍历的问题
- 关于java集合的遍历以及ConcurrentModificationException(并发操作异常)
- 集合中关于iterator遍历顺序的理解
- 关于几种方法对java集合的遍历
- 关于Map集合的三种遍历方法
- 2017-5-6关于Map集合的遍历一些记录
- JAVA集合-集合的遍历
- 关于java list集合的for遍历和Iterator遍历的随想
- 遍历集合的方法
- JavaMap集合的遍历
- Map集合的遍历
- px4原生源码学习-(3)--Nuttx实时操作系统的使用
- UVa 11134 Fabled Rooks (排序+贪心)
- 纯CSS画的基本图形(矩形、圆形、三角形、多边形、爱心、八卦等)
- 股市骗子
- 谷歌插件Image downloader开发之popup
- 关于集合的遍历
- C 递归与迭代的使用切换
- Freestanding C与交叉编译器的生成原理分析
- 当互联网金融遇到区块链
- Android基础知识
- Android简单实例(生成“Hello World!”)
- 知道这20个正则表达式,能让你少写1,000行代码
- Rancher v1.2:网络架构解读
- Jquery实现从一个页面跳转到另一页面