欢迎光临散文网 会员登陆 & 注册

Java操作考试:List,for,foreach,迭代器循环3种循环打印商品类内容【诗书画唱】

2020-09-09 20:08 作者:诗书画唱  | 我要投稿

注意事项:

Ctrl+shift+F:整理代码格式。

类的首字母大小。

快速代码版(从写类时开始计时)

可以自己取其中的大写字母的类名的首字母为变量名,有时可以用上Alt+/的快速生成代码,用P1,P2的等命名。

Alt+/:







package A;


public class P {

private String name;


private Double 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;

}


public P(String name, Double price) {

super();

this.name = name;

this.price = price;

}


@Override

public String toString() {

return "P [name=" + name + ", price=" + price + "]";

}



}





package A;


import java.util.*;




public class D {


public static void main(String[] args) {

// TODO Auto-generated method stub


List<P> L=new  ArrayList<P>();

P P1= new  P("1", 1.1);

P P2= new  P("2", 1.2);

L.add(P1);

L.add(P2);

for (P P : L) {

System.out.println(P);

}

 

Iterator<P>  I= L.iterator() ;

while(I.hasNext()){

P P=I.next();


System.out.println(P);


}



for (int j = 0; j < L.size(); j++) {

P P=L.get(j);



System.out.println(P);

}

}


}





————————


有时要注意的,录音记忆的自己不熟的部分:

Iterator<Product> it = list.iterator();


ArrayList<Product>();



Product p

list.iterator();

it.hasNext()


it.next();


list.size();



list.get(i);



————————————

要求8分钟内自己敲代码写完:


package one;


public class Product {

    private String name;

    private Double price;

    

   // 下面声明的传参的构造方法是为了在Text类中给商品类赋值:

    public Product(String n,Double p) {

    this.name = n;

    this.price = p;

    }

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方法是为了在Text类中能通过get

//等方法返回出:

@Override

public String toString() {

// TODO Auto-generated method stub

return "商品名称是:" 

+ this.name 

+ "商品价格是:¥" 

+ this.price;

}

}





package one;


import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;


public class Test {


public static void main(String[] args) {

// TODO Auto-generated method stub

        List<Product>list = new ArrayList<Product>();

        Product p1 = new Product("诗书画唱辣条",6.6);

        Product p2 = new Product("诗书画唱瓜子",8.8);

        Product p3 = new Product("诗书画唱巧克力",2.3);

        list.add(p1);

        list.add(p2);

        list.add(p3);

        //for循环:

        System.out.println("\n下面是for循环:");

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

        Product p = list.get(i);

        System.out.println(p);

        }

        //foreach循环:

        System.out.println("\n下面是foreach循环:");

        for(Product p : list) {

        System.out.println(p);

        }

        //迭代器循环:

        System.out.println("\n下面是迭代器循环:");

        Iterator<Product> it = list.iterator();

        while(it.hasNext()) {

        Product p = it.next();

        System.out.println(p);

        }

}


}



Java操作考试:List,for,foreach,迭代器循环3种循环打印商品类内容【诗书画唱】的评论 (共 条)

分享到微博请遵守国家法律