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

三种常用的对应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<Product> plist = new ArrayList<Product>();
        plist.add(p);
        plist.add(p1);
        plist.add(p2);
        plist.add(p3);
        ArrayList<ArrayList<Product>> pclist = new ArrayList<ArrayList<Product>>();
        pclist.add(plist);
        // pclist.add(pc);
        // pclist.add(pc1);
        // pclist.add(pc2);
        // pclist.add(pc3);

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

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

        while (pclists.hasNext()) {
            ArrayList<com.collections.lists.vo.Product> arrayList = (ArrayList<com.collections.lists.vo.Product>) pclists
                    .next();
            Iterator<Product> 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 ProductClassify() {
        // TODO Auto-generated constructor stub
    }
    
    public ProductClassify(int id, int pcId, String pName) {
        super();
        this.id = id;
        this.pcId = pcId;
        this.pName = pName;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getPcId() {
        return pcId;
    }

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

    public String getpName() {
        return pName;
    }

    public void setpName(String pName) {
        this.pName = pName;
    }

    @Override
    public String toString() {
        return "ProductClassify [id=" + id + ", pcId=" + pcId + ", pName="
                + pName + "]";
    }
    
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值