国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

Java操作考試:List,for,foreach,迭代器循環(huán)3種循環(huán)打印商品類內(nèi)容【詩書畫唱】

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分鐘內(nèi)自己敲代碼寫完:


package one;


public class Product {

? ? private String name;

? ? private Double price;

? ??

? ?// 下面聲明的傳參的構(gòu)造方法是為了在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循環(huán):

? ? ? ? System.out.println("\n下面是for循環(huán):");

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

? ? ? ? Product p = list.get(i);

? ? ? ? System.out.println(p);

? ? ? ? }

? ? ? ? //foreach循環(huán):

? ? ? ? System.out.println("\n下面是foreach循環(huán):");

? ? ? ? for(Product p : list) {

? ? ? ? System.out.println(p);

? ? ? ? }

? ? ? ? //迭代器循環(huán):

? ? ? ? System.out.println("\n下面是迭代器循環(huán):");

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

? ? ? ? while(it.hasNext()) {

? ? ? ? Product p = it.next();

? ? ? ? System.out.println(p);

? ? ? ? }

}


}



Java操作考試:List,for,foreach,迭代器循環(huán)3種循環(huán)打印商品類內(nèi)容【詩書畫唱】的評論 (共 條)

分享到微博請遵守國家法律
盐池县| 三门县| 二手房| 靖安县| 卢湾区| 桓台县| 东光县| 黄石市| 扶风县| 临高县| 大城县| 湟源县| 教育| 宜州市| 巩留县| 图们市| 祁连县| 玉龙| 巨鹿县| 巨野县| 五华县| 罗江县| 昌黎县| 湘西| 灵武市| 濮阳市| 平乐县| 紫金县| 佛山市| 宁远县| 武冈市| 岗巴县| 湘西| 偃师市| 沈丘县| 富民县| 广德县| 富川| 南华县| 府谷县| 瑞昌市|