适合Java稍微入门的读者观看,此篇主要关于Arralist三种遍历语法以及他们的快捷键~
首先创建一个测试类,用来测试:
class test0
{
private String name;
private double price;
public test0(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
不要忘记重写tostring方法要不然会显示一堆地址
实例化对象 (这里价格是细节 是考研408 4门所占比重 哈哈哈哈哈~)
public class list三种遍历方式 {
public static void main(String[] args) {
ArrayList arrayList = new ArrayList();
arrayList.add(new test0("数据结构",45));
arrayList.add(new test0("计算机组成原理",45));
arrayList.add(new test0("计算机网络",30));
arrayList.add(new test0("操作系统",30));
}
}
三种依次是iterator(迭代器) 增强for循环 普通for循环 (这三种方式也同样适用于我们日常编程中 尤其是增强for循环 在面向对象编程使用非常广)
Iterator iterator = arrayList.iterator();
while (iterator.hasNext()) {
Object next = iterator.next();
System.out.println(next);
}//快捷键ieie
for (Object o :arrayList) {
System.out.println(o);
}//快捷键 大写的I
for (int i = 0; i <1; i++) {
System.out.println(arrayList);
}
运行结果: