【java】ArrayList、Iterator用法

时间:2021-07-11 07:20:51
 package com.tn.collect;

 import java.util.ArrayList;
import java.util.Iterator; class Product{
public Product() {
super();
// TODO Auto-generated constructor stub
}
public Product(int id, String name, int price) {
// TODO Auto-generated constructor stub
setId(id);
setName(name);
setPrice(price);
}
private int id;
private String name;
private double price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
@Override
public String toString() {
return "Product [id=" + id + ", name=" + name + ", price=" + price
+ "]";
}
}
public class ArrayListLearning {
public static void main(String[] args){
Product product1=new Product(1001,"奇瑞",99999);
Product product2=new Product(1002,"江淮",88888);
Product product3=new Product(1003,"比亚迪",77777);
ArrayList arrayList=new ArrayList();
arrayList.add(product1);
arrayList.add(product2);
arrayList.add(product3);
System.out.println(arrayList);//会打印容器中每个对象的toString
System.out.println(arrayList.size());
Object object=arrayList.get(0);
System.out.println(object);
Product product=(Product)arrayList.get(0);
System.out.println(arrayList.get(0));
Product product4=new Product(1004,"力帆",66666);
Product product5=new Product(1005,"吉利",55555);
Product productx=new Product(1005,"吉利",55555);
ArrayList<Product> list=new ArrayList<Product>();
list.add(product4);
list.add(product5);
Product product6=list.get(0);
System.out.println(product6); //遍历方法一:
for(int i=0;i<list.size();i++){
Product pro=list.get(i);
System.out.println(pro);
} //遍历方法二:
Iterator<Product> iterator=list.iterator();
while(iterator.hasNext()){
Product pro=iterator.next();
} //遍历方法三:
for(Object obj:list){
Product pro=(Product)obj;
} System.out.println(list.contains(product5));
System.out.println(list.contains(productx));//对象创建是两个,但实际应用中这两个对象应该是一个
}
}

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


 package com.tn.collect;

 import java.util.ArrayList;
import java.util.Iterator; class Product{
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Product other = (Product) obj;
if (id != other.id)
return false;
return true;
}
public Product() {
super();
// TODO Auto-generated constructor stub
}
public Product(int id, String name, int price) {
// TODO Auto-generated constructor stub
setId(id);
setName(name);
setPrice(price);
}
private int id;
private String name;
private double price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
@Override
public String toString() {
return "Product [id=" + id + ", name=" + name + ", price=" + price
+ "]";
}
}
public class ArrayListLearning {
public static void main(String[] args){
Product product1=new Product(1001,"奇瑞",99999);
Product product2=new Product(1002,"江淮",88888);
Product product3=new Product(1003,"比亚迪",77777);
ArrayList arrayList=new ArrayList();
arrayList.add(product1);
arrayList.add(product2);
arrayList.add(product3);
System.out.println(arrayList);//会打印容器中每个对象的toString
System.out.println(arrayList.size());
Object object=arrayList.get(0);
System.out.println(object);
Product product=(Product)arrayList.get(0);
System.out.println(arrayList.get(0));
Product product4=new Product(1004,"力帆",66666);
Product product5=new Product(1005,"吉利",55555);
Product productx=new Product(1005,"吉利",55555);
ArrayList<Product> list=new ArrayList<Product>();
list.add(product4);
list.add(product5);
Product product6=list.get(0);
System.out.println(product6); //遍历方法一:
for(int i=0;i<list.size();i++){
Product pro=list.get(i);
System.out.println(pro);
} //遍历方法二:
Iterator<Product> iterator=list.iterator();
while(iterator.hasNext()){
Product pro=iterator.next();
} //遍历方法三:
for(Object obj:list){
Product pro=(Product)obj;
} System.out.println(list.contains(product5));
System.out.println(list.contains(productx));//对象创建是两个,但实际应用中这两个对象应该是一个
}
}

重写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