zoukankan      html  css  js  c++  java
  • 【java】ArrayList、Iterator用法

     1 package com.tn.collect;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Iterator;
     5 
     6 class Product{
     7     public Product() {
     8         super();
     9         // TODO Auto-generated constructor stub
    10     }
    11     public Product(int id, String name, int price) {
    12         // TODO Auto-generated constructor stub
    13         setId(id);
    14         setName(name);
    15         setPrice(price);
    16     }
    17     private int id;
    18     private String name;
    19     private double price;
    20     public int getId() {
    21         return id;
    22     }
    23     public void setId(int id) {
    24         this.id = id;
    25     }
    26     public String getName() {
    27         return name;
    28     }
    29     public void setName(String name) {
    30         this.name = name;
    31     }
    32     public double getPrice() {
    33         return price;
    34     }
    35     public void setPrice(double price) {
    36         this.price = price;
    37     }
    38     @Override
    39     public String toString() {
    40         return "Product [id=" + id + ", name=" + name + ", price=" + price
    41                 + "]";
    42     }
    43 }
    44 public class ArrayListLearning {
    45     public static void main(String[] args){
    46         Product product1=new Product(1001,"奇瑞",99999);
    47         Product product2=new Product(1002,"江淮",88888);
    48         Product product3=new Product(1003,"比亚迪",77777);
    49         ArrayList arrayList=new ArrayList();
    50         arrayList.add(product1);
    51         arrayList.add(product2);
    52         arrayList.add(product3);
    53         System.out.println(arrayList);//会打印容器中每个对象的toString
    54         System.out.println(arrayList.size());
    55         Object object=arrayList.get(0);
    56         System.out.println(object);
    57         Product product=(Product)arrayList.get(0);
    58         System.out.println(arrayList.get(0));
    59         Product product4=new Product(1004,"力帆",66666);
    60         Product product5=new Product(1005,"吉利",55555);
    61         Product productx=new Product(1005,"吉利",55555);
    62         ArrayList<Product> list=new ArrayList<Product>();
    63         list.add(product4);
    64         list.add(product5);
    65         Product product6=list.get(0);
    66         System.out.println(product6);
    67         
    68         //遍历方法一:
    69         for(int i=0;i<list.size();i++){
    70             Product pro=list.get(i);
    71             System.out.println(pro);
    72         }
    73         
    74         //遍历方法二:
    75         Iterator<Product> iterator=list.iterator();
    76         while(iterator.hasNext()){
    77             Product pro=iterator.next();
    78         }
    79         
    80         //遍历方法三:
    81         for(Object obj:list){
    82             Product pro=(Product)obj;
    83         }
    84         
    85         System.out.println(list.contains(product5));
    86         System.out.println(list.contains(productx));//对象创建是两个,但实际应用中这两个对象应该是一个
    87     }
    88 }
    ArrayList

    运行结果:

    [Product [id=1001, name=奇瑞, price=99999.0], Product [id=1002, name=江淮, price=88888.0], Product [id=1003, name=比亚迪, price=77777.0]]
    3
    Product [id=1001, name=奇瑞, price=99999.0]
    Product [id=1001, name=奇瑞, price=99999.0]
    Product [id=1004, name=力帆, price=66666.0]
    Product [id=1004, name=力帆, price=66666.0]
    Product [id=1005, name=吉利, price=55555.0]
    true
    false


      1 package com.tn.collect;
      2 
      3 import java.util.ArrayList;
      4 import java.util.Iterator;
      5 
      6 class Product{
      7     @Override
      8     public boolean equals(Object obj) {
      9         if (this == obj)
     10             return true;
     11         if (obj == null)
     12             return false;
     13         if (getClass() != obj.getClass())
     14             return false;
     15         Product other = (Product) obj;
     16         if (id != other.id)
     17             return false;
     18         return true;
     19     }
     20     public Product() {
     21         super();
     22         // TODO Auto-generated constructor stub
     23     }
     24     public Product(int id, String name, int price) {
     25         // TODO Auto-generated constructor stub
     26         setId(id);
     27         setName(name);
     28         setPrice(price);
     29     }
     30     private int id;
     31     private String name;
     32     private double price;
     33     public int getId() {
     34         return id;
     35     }
     36     public void setId(int id) {
     37         this.id = id;
     38     }
     39     public String getName() {
     40         return name;
     41     }
     42     public void setName(String name) {
     43         this.name = name;
     44     }
     45     public double getPrice() {
     46         return price;
     47     }
     48     public void setPrice(double price) {
     49         this.price = price;
     50     }
     51     @Override
     52     public String toString() {
     53         return "Product [id=" + id + ", name=" + name + ", price=" + price
     54                 + "]";
     55     }
     56 }
     57 public class ArrayListLearning {
     58     public static void main(String[] args){
     59         Product product1=new Product(1001,"奇瑞",99999);
     60         Product product2=new Product(1002,"江淮",88888);
     61         Product product3=new Product(1003,"比亚迪",77777);
     62         ArrayList arrayList=new ArrayList();
     63         arrayList.add(product1);
     64         arrayList.add(product2);
     65         arrayList.add(product3);
     66         System.out.println(arrayList);//会打印容器中每个对象的toString
     67         System.out.println(arrayList.size());
     68         Object object=arrayList.get(0);
     69         System.out.println(object);
     70         Product product=(Product)arrayList.get(0);
     71         System.out.println(arrayList.get(0));
     72         Product product4=new Product(1004,"力帆",66666);
     73         Product product5=new Product(1005,"吉利",55555);
     74         Product productx=new Product(1005,"吉利",55555);
     75         ArrayList<Product> list=new ArrayList<Product>();
     76         list.add(product4);
     77         list.add(product5);
     78         Product product6=list.get(0);
     79         System.out.println(product6);
     80         
     81         //遍历方法一:
     82         for(int i=0;i<list.size();i++){
     83             Product pro=list.get(i);
     84             System.out.println(pro);
     85         }
     86         
     87         //遍历方法二:
     88         Iterator<Product> iterator=list.iterator();
     89         while(iterator.hasNext()){
     90             Product pro=iterator.next();
     91         }
     92         
     93         //遍历方法三:
     94         for(Object obj:list){
     95             Product pro=(Product)obj;
     96         }
     97         
     98         System.out.println(list.contains(product5));
     99         System.out.println(list.contains(productx));//对象创建是两个,但实际应用中这两个对象应该是一个
    100     }
    101 }
    重写equals方法后

    运行结果:

    [Product [id=1001, name=奇瑞, price=99999.0], Product [id=1002, name=江淮, price=88888.0], Product [id=1003, name=比亚迪, price=77777.0]]
    3
    Product [id=1001, name=奇瑞, price=99999.0]
    Product [id=1001, name=奇瑞, price=99999.0]
    Product [id=1004, name=力帆, price=66666.0]
    Product [id=1004, name=力帆, price=66666.0]
    Product [id=1005, name=吉利, price=55555.0]
    true
    true

  • 相关阅读:
    fzuoj Problem 2177 ytaaa
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest Capture the Flag
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest Team Formation
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest Beauty of Array
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest Lunch Time
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest Convert QWERTY to Dvorak
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest May Day Holiday
    zoj The 12th Zhejiang Provincial Collegiate Programming Contest Demacia of the Ancients
    zjuoj The 12th Zhejiang Provincial Collegiate Programming Contest Ace of Aces
    csuoj 1335: 高桥和低桥
  • 原文地址:https://www.cnblogs.com/xiongjiawei/p/6601837.html
Copyright © 2011-2022 走看看