五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

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

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

2020-09-09 20:08 作者:詩(shī)書畫唱  | 我要投稿

注意事項(xiàng):

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

類的首字母大小。

快速代碼版(從寫類時(shí)開始計(jì)時(shí))

可以自己取其中的大寫字母的類名的首字母為變量名,有時(shí)可以用上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);

}

}


}





————————


有時(shí)要注意的,錄音記憶的自己不熟的部分:

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類中能通過(guò)get

//等方法返回出:

@Override

public String toString() {

// TODO Auto-generated method stub

return "商品名稱是:"?

+ this.name?

+ "商品價(jià)格是:¥"?

+ 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("詩(shī)書畫唱辣條",6.6);

? ? ? ? Product p2 = new Product("詩(shī)書畫唱瓜子",8.8);

? ? ? ? Product p3 = new Product("詩(shī)書畫唱巧克力",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)容【詩(shī)書畫唱】的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
锡林郭勒盟| 班玛县| 阳山县| 赫章县| 本溪市| 紫云| 临高县| 炉霍县| 乌兰浩特市| 南平市| 光山县| 偃师市| 商城县| 如东县| 灵寿县| 漳州市| 兴仁县| 南宁市| 邵东县| 额尔古纳市| 策勒县| 肥城市| 萨嘎县| 石门县| 高密市| 万全县| 沛县| 菏泽市| 怀安县| 新乐市| 江阴市| 巴马| 伊金霍洛旗| 玛曲县| 新化县| 沭阳县| 长乐市| 苍山县| 呼和浩特市| 洪江市| 旌德县|