Java每日笔记之Arraylist三种遍历方式(同样适用于LinkedList,Vector)

适合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);
      }

 运行结果:

 

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值