三种常用的对应list集合的双重遍历(一)

2014-11-24 02:36:24 · 作者: · 浏览: 0

三种常用的对应list集合的双重遍历,直接上代码

package com.collections.lists;


import java.util.ArrayList;
import java.util.Iterator;

import com.collections.lists.vo.Product;
import com.collections.lists.vo.ProductClassify;

public class ListTestDemo implements Runnable {

public static void main(String[] args) {

/***
* 以下采用的是 数据库的数据模拟
*/

Product p = new Product();
p.setId(1);
p.setName("耐克");
p.setPcId(1);
p.setPrice(10);
p.setNum(200);
// 第一种产品
ProductClassify pc = new ProductClassify();
pc.setId(1);
pc.setPcId(p.getPcId());
pc.setpName("鞋子");

Product p1 = new Product();
p1.setId(2);
p1.setName("馒头");
p1.setPcId(2);
p1.setPrice(20);
p1.setNum(300);
// 第二种产品
ProductClassify pc1 = new ProductClassify();
pc1.setId(2);
pc1.setPcId(p.getPcId());
pc1.setpName("食品");

Product p2 = new Product();
p2.setId(2);
p2.setName("水密码");
p2.setPcId(2);
p2.setPrice(20);
p2.setNum(300);
// 第三种产品
ProductClassify pc2 = new ProductClassify();
pc2.setId(3);
pc2.setPcId(p.getPcId());
pc2.setpName("化妆品");

Product p3 = new Product();
p3.setId(4);
p3.setName("杜冷丁");
p3.setPcId(3);
p3.setPrice(20);
p3.setNum(300);
// 第四种产品
ProductClassify pc3 = new ProductClassify();
pc3.setId(4);
pc3.setPcId(p.getPcId());
pc3.setpName("药品");

// 向集合中保存
ArrayList plist = new ArrayList ();
plist.add(p);
plist.add(p1);
plist.add(p2);
plist.add(p3);
ArrayList > pclist = new ArrayList >();
pclist.add(plist);
// pclist.add(pc);
// pclist.add(pc1);
// pclist.add(pc2);
// pclist.add(pc3);

// 遍历读出
for (ArrayList arrayList : pclist) {

for (Product product : arrayList) {
// System.out.println("第一种打印方式 " + product.getName());
}
}
Iterator > pclists = pclist.iterator();

while (pclists.hasNext()) {
ArrayList arrayList = (ArrayList ) pclists
.next();
Iterator pli = arrayList.iterator();

while (pli.hasNext()) {
Product product = (Product) pli.next();
// System.out.println("第二种打印方式 " + product.getName());
}
}

for (int i = 0; i < pclist.size(); i++) {

plist = pclist.get(i);

for (int j = 0; j < plist.size(); j++) {
System.out
.println("第三中打印方式 " + plist.get(j).getName());
}
}
// System.out.println("单重集合" + plist.size());
// System.out.println("双重集合" + pclist.size());
Thread t1 = new Thread();
t1.start();

}

@Override
public void run() {
// TODO Auto-generated method stub

System.out.println("haha");

}

}

package com.collections.lists.vo;

public class Product {

@SuppressWarnings("product")
private int id;

private String name;

private int pcId;//商品类型编号

private int price;

private int num;

public Product() {
// TODO Auto-generated constructor stub
}

public Product(int id, String name, int pcId, int price, int num) {
super();
this.id = id;
this.name = name;
this.pcId = pcId;
this.price = price;
this.num = num;
}


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 int getPcId() {
return pcId;
}

public void setPcId(int pcId) {
this.pcId = pcId;
}

public int getPrice() {
return price;
}

public void setPrice(int price) {
this.price = price;
}

public int getNum() {
return num;
}

public void setNum(int num) {
this.num = num;
}



}
package com.collections.lists.vo;

public class ProductClassify {

@SuppressWarnings("productclassify")
private int id;//编号

private int pcId;//商品类型编号

private String pName;//商品分类名字

public ProductCla