免费高清特黄a大片,九一h片在线免费看,a免费国产一级特黄aa大,国产精品国产主播在线观看,成人精品一区久久久久,一级特黄aa大片,俄罗斯无遮挡一级毛片

分享

第十七天 集合-Collection&增強for&迭代器【悟空教程】

 Java幫幫 2020-01-02


第十七天 集合-Collection&增強for&迭代器【悟空教程】

17天 集合

第1章 集合

1.1 集合介紹

集合,集合是java中提供的一種容器,可以用來存儲多個數(shù)據(jù)。

出現(xiàn)意義:面向對象語言對事物的體現(xiàn)都是以對象的形式,所以為了方便對多個對象的操作,就對對象進行存儲,集合就是存儲對象最常用的一種方式。

Java中的集合: JDK為我們提供了一套完整的容器類庫,這些容器可以用于存儲各種類型的對象,并且長度都是可變的,我們把這些類統(tǒng)稱為集合類,它們都位于java.util包中。

在前面的學習中,我們知道數(shù)據(jù)多了,可以使用數(shù)組存放或者使用ArrayList集合進行存放數(shù)據(jù)。那么,集合和數(shù)組既然都是容器,它們有啥區(qū)別呢?

  • 數(shù)組的長度是固定的。集合的長度是可變的。

  • 集合中存儲的元素必須是引用類型數(shù)據(jù)

1.2 ArrayList集合存儲元素

我們來回顧下,使用ArrayList集合存儲元素并遍歷的過程。

  • 練習一:ArrayList集合存儲5個int類型元素

public static void main(String[] args) {

     ArrayList<Integer> list = new ArrayList<Integer>();

list.add(111); //自動裝箱  把基本類型 包裝成 對象

list.add(222);

list.add(333);

list.add(444);

list.add(555);

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

         System.out.println(list.get(i));

}

}

  • 練習二:ArrayList集合存儲5個Person類型元素

public static void main(String[] args) {

     ArrayList<Person> list = new ArrayList<Person>();

list.add(new Person(“小強));

list.add(new Person(“老王”));

list.add(new Person(“小虎”));

list.add(new Person(“小澤”));

list.add(new Person(“小紅”));

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

Person p = list.get(i);

         System.out.println(p);

}

}

第2章 Collection

2.1 Collection接口

2.1.1 Collection概念

Collection是所有單列集合的直接或間接接口,其指定了所有集合應該具備的功能。

AbstractCollection是實現(xiàn)了Collection接口的抽象父類。

Collection與AbstractCollection均無法直接使用,需要使用其具體實現(xiàn)類。下圖是強調(diào)功能的集合體系圖,實線黑框為具體實現(xiàn)類。我們可以從中任意挑選一個以多態(tài)形式演示Collection的方法。

2.1.2 Collection通用方法

主要方法

boolean add(E e)    //添加元素

boolean remove(Object o)  //刪除元素

void clear()     //清空集合

boolean contains(Object o) //判斷是否包含某元素

boolean isEmpty()   //判斷是否為空

int size()     //獲取集合長度

 

import java.util.ArrayList;

import java.util.Collection;

/*

 *  Collection   容器.  

 *  

 *  方法:

 *  

 boolean add(E e)   確保此 collection 包含指定的元素(可選操作)。

 void clear()       移除此 collection 中的所有元素(可選操作)。

 boolean contains(Object o)  如果此 collection 包含指定的元素,則返回 true

 boolean isEmpty()           如果此 collection 不包含元素,則返回 true

 boolean remove(Object o)    從此 collection 中移除指定元素的單個實例,如果存在的話(可選操作)。

 int size()                  返回此 collection 中的元素數(shù)。

 Object[] toArray()          返回包含此 collection 中所有元素的數(shù)組。

 *  

 */

public class Demo {

public static void main(String[] args) {

Collection c = new ArrayList<>();

System.out.println(c); // []

c.add("abc"); // 添加元素

c.add("eeee");

c.add(123);

c.add(true);

c.add(null);

c.add("abc"); // 添加元素

System.out.println(c); // [abc,eee..... ]

//c.clear(); // 清空所有元素.

//System.out.println(c); // [abc,eee..... ]

//  System.out.println("是否包含: " +  c.contains("abc")); //true

//  System.out.println("是否包含: " +  c.contains("a")); // false

//  System.out.println("是否包含: " +  c.contains("ee")); // false

//  System.out.println("是否包含: " +  c.contains(23));

//  System.out.println("是否包含: " +  c.contains(null));

//  System.out.println("============");

//  System.out.println("是否元素為空: " +  c.isEmpty());

//  c.clear();

//  System.out.println("是否元素為空: " +  c.isEmpty());

boolean remove = c.remove("abc");

System.out.println(remove);

System.out.println(c);

boolean remove2 = c.remove(222);

System.out.println(remove2);

System.out.println(c);

System.out.println("============");

Object[] array = c.toArray();

for (int i = 0; i < array.length; i++) {

System.out.println(array[i]);

}

}

}

2.2 增強for循環(huán)

增強for循環(huán)是JDK1.5以后出來的一個高級for循環(huán),專門用來遍歷數(shù)組和集合的。它的內(nèi)部原理其實是個Iterator迭代器,所以在遍歷的過程中,不能對集合中的元素進行增刪操作。增強for循環(huán)用來迭代集合或數(shù)組,:

格式:

for(元素的數(shù)據(jù)類型  變量 : Collection集合or數(shù)組){

}

它用于遍歷Collection和數(shù)組。通常只進行遍歷元素,不要在遍歷的過程中對集合元素進行增刪操作。

練習一:遍歷數(shù)組

int[] arr = new int[]{11,22,33};

for (int n : arr) {//變量n代表被遍歷到的數(shù)組元素

System.out.println(n);

}

練習二:遍歷集合

Collection<String> coll = new ArrayList<String>();

coll.add("javahelp1");

coll.add("javahelp2");

coll.add("javahelp3");

coll.add("javahelp4");

for(String str : coll){//變量Str代表被遍歷到的集合元素

System.out.println(str);

}

增強for循環(huán)和老式的for循環(huán)有什么區(qū)別?

注意:新for循環(huán)必須有被遍歷的目標。目標只能是Collection或者是數(shù)組。

建議:遍歷數(shù)組時,如果僅為遍歷,可以使用增強for如果要對數(shù)組的元素進行 操作,使用老式for循環(huán)可以通過角標操作

/*

 * 增強for 循環(huán) .   簡化的遍歷集合操作.

 *

 *  格式:  

 * for (元素類型 變量名 : 容器名字){

 *  直接使用變量名 ,就是使用元素.

 *  }

 * for (Student student : c) {

System.out.println(student);

}

fore alt + /

 */

public class Demo {

public static void main(String[] args) {

//fun1();

//1. 創(chuàng)建學生對象

Student student1 = new Student("老王" , 66);

Student student2 = new Student("金蓮" , 28);

Student student3 = new Student("西門" , 99);

Student student4 = new Student("大郎" , 100);

Collection<Student> c = new ArrayList<>();

// 3.添加元素

c.add(student1);

c.add(student2);

c.add(student3);

c.add(student4);

for (Student student : c) {

//c.remove(student);//ConcurrentModificationException 并發(fā)修改異常 .

student.setScore(200);

}

//  Student student ;

//  for (Iterator iterator = c.iterator(); iterator.hasNext(); c.remove(student))

//    student = (Student)iterator.next();

System.out.println(c);

}

private static void fun1() {

Collection<String> c = new ArrayList<>();

c.add("aaa");

c.add("bbb");

c.add("ccc");

c.add("ddd");

for (String string : c) {

System.out.println(string);

}

}

}


2.3 迭代器

集合用來持有數(shù)據(jù),一定會設計出對數(shù)據(jù)的增、刪、改、查四個常用方法,而查是集合中最常用的功能。Collection接口繼承了Iterable接口,具備了可迭代功能iterator方法,該方法用于迭代集合。所以,所有單列集合由于是Collection的直接或間接實現(xiàn)類,均具有該方法。

這里涉及到以下內(nèi)容共同完成集合的迭代:

  • Collection接口的iterator方法,所以單列集合實現(xiàn)類均有該方法

  • iterator方法的返回值類型Iterator接口類型

  • Iterator接口的兩個方法:hasNext與next方法


2.3.1 迭代常規(guī)步驟

1:通過集合獲取這個集合的迭代器

2:結合使用迭代器的hashNext與next完成集合迭代

如:

ArryList<String> list = new ArryList <String>();

hs.add("i love java");

hs.add("i like java");

//返回迭代器

Iterator<String> iterator = list.iterator();

//調(diào)用hasNextnext完成集合迭代

while (iterator.hasNext()) {

String string = iterator.next();

System.out.println(string);

}

2.3.2 Collection接口的Iterator

方法聲明為:

Iterator<集合中數(shù)據(jù)類型>  iterator()

用來返回專屬于該集合對象的迭代器對象(Iterator的子類對象)。


2.3.3 Iterator接口

該接口規(guī)定了迭代集合所需要的方法


2.3.4 Iterator接口的兩個方法:hasNextnext方法

Iterator規(guī)定了兩個方法,集合對象產(chǎn)生的迭代器對象正是通過這兩個方法幫助集合進行迭代工作的。

調(diào)用迭代器的hasNext方法判斷是否有下一個元素

調(diào)用迭代器的next獲取下一個元素


2.3.5 迭代集合元素圖解:

 

2.3.6 代碼演示

import java.util.ArrayList;

import java.util.Collection;

import java.util.Iterator;

/*

 * Iterator<E> iterator() 返回在此 collection 的元素上進行迭代的迭代器。

 *

 * Iterator 就是幫你去做遍歷集合操作 .  

 *

 * iterator() 返回的就是 Iterator接口的子類對象.

 */

public class Demo2 {

public static void main(String[] args) {

Collection  c  = new ArrayList();

c.add("111");

c.add("222");

c.add("333");

c.add("444");

// 遍歷集合.   獲取其中每一個元素.

// 1.獲取迭代器對象  .

Iterator iterator = c.iterator();  // Iterator 接口  指向 子類對象.

// 2.調(diào)用迭代器的方法,獲取集合中的元素.

//  Object next = iterator.next();

//  System.out.println(next);

//  

//  Object next2 = iterator.next();

//  System.out.println(next2);

//  

//  Object next3 = iterator.next();

//  System.out.println(next3);

//  

//  Object next4 = iterator.next();

//  System.out.println(next4);

//  

//  Object next5 = iterator.next(); //NoSuchElementException

//  System.out.println(next5);

while(iterator.hasNext()){

Object next = iterator.next();

System.out.println(next);

}

}

}


2.3.7 迭代器遍歷集合總結

import java.util.Collection;

import java.util.Iterator;

import java.util.ArrayList;

/*

 * 1.hasNext 方法和  next方法 說明 . 遍歷之前,有一個指針指向初始位置,第一個元素的前面,

 *  hasNext,判斷是否在指針后面有元素,有返回true ,沒有返回false .

 *   如果有 , 就可以通過 next() 獲取下一個元素了.

 * 2.syso(c); 我也能看所有的元素, 那我為什么還要遍歷呢?   

 *   

 * 3.toString() 在哪重寫的呢? 你管的著么!!   要找找他爹,他爺爺

 *

 */

public class Demo3 {

public static void main(String[] args) {

Collection c = new ArrayList<>();

c.add("aaa");

c.add("abbbbb");

c.add("cccc");

c.add("xxxx ");

// 遍歷集合

// 1.獲取迭代器

Iterator iterator = c.iterator();

// 2.通過迭代器獲取元素 .

while (iterator.hasNext()) { // 是否有下一個.

Object next = iterator.next(); // 獲取下一個元素

System.out.println(next);

}

}

}

2.3.8 保存自定義對象練習(迭代器移除元素)

public class Student {

String name;  // 姓名  

int score;  // 分數(shù)

public Student() {

super();

// TODO Auto-generated constructor stub

}

public Student(String name, int score) {

super();

this.name = name;

this.score = score;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getScore() {

return score;

}

public void setScore(int score) {

this.score = score;

}

@Override

public String toString() {

return "學生 [姓名=" + name + ", 分數(shù)=" + score + "]";

}

}

import java.util.ArrayList;

import java.util.Collection;

import java.util.Iterator;

/*

 * 給你集合 ,保存 很多 學生對象. 學生類中, 包含了姓名和分數(shù).   

 *

 * 學生1 ("老王" , 66) ;

 * 學生2 ("金蓮" , 28) ;

 * 學生3 ("西門" , 99) ;

 *

 * 1.把所有對象保存集合.

 * 2.遍歷集合,60分以下的同學,移除掉.

 * 3.再遍歷的時候,只有 60分以上的同學了.

 */

public class Test {

public static void main(String[] args) {

//1. 創(chuàng)建學生對象

Student student1 = new Student("老王" , 66);

Student student2 = new Student("金蓮" , 28);

Student student3 = new Student("西門" , 99);

Student student4 = new Student("大郎" , 100);

// 2.創(chuàng)建集合

Collection<Student> c = new ArrayList<>();

// 3.添加元素

c.add(student1);

c.add(student2);

c.add(student3);

c.add(student4);

System.out.println(c);

// 遍歷集合,判斷分數(shù) .

//a.獲取迭代器

Iterator<Student> it  = c.iterator();

//b. 使用迭代器遍歷

while (it.hasNext()) {

 Student stu  = it.next();

 System.out.println(stu);

 //獲取分數(shù)

 int score = stu.getScore();

 //判斷分數(shù)

 if(score <60){

 // 移除學生  

 //c.remove(stu);

 it.remove(); // 移除當前迭代的元素. (迭代器移除方法可以避免并發(fā)修改異常)

 }

}

// 再遍歷集合

Iterator<Student> iterator = c.iterator();

while (iterator.hasNext()) {

Student student = iterator.next();

System.out.println(student.getName() +" --- "+ student.getScore());

}

}

}


2.3.9 并發(fā)修改異常

迭代的常規(guī)用法中我們要盡量避免在迭代過程中為集合添加/刪除數(shù)據(jù)。否則會報錯,原因是Java拋出了并發(fā)修改異常。

迭代過程中并發(fā)修改異常的原因為迭代器中”記憶”的集合長度與集合中實際長度不同,而導致出現(xiàn)索引與實際元素不符甚至無限循環(huán)的情況發(fā)生。

所以在使用Iterator時,避免類似操作,for循環(huán)底層為迭代器實現(xiàn),所以也需要避免類似操作。

有些迭代器避免了這樣的問題,如ListIterator,但該類并不通用也不常用,實際開發(fā)中很少使用,只需要簡單了解。

java中提供了很多個集合,它們在存儲元素時,采用的存儲方式不同。我們要取出這些集合中的元素,可通過一種通用的獲取方式來完成。

import java.util.ArrayList;

import java.util.Collection;

import java.util.Iterator;

import java.util.List;

import java.util.ListIterator;

/*

 *  并發(fā)修改異常:當你 遍歷集合并操作集合,添加或者移除元素的使用,可能會報出并發(fā)修改異常.

 *  處理方式:添加,移除 ,使用迭代器的方法. 迭代器中的remove.   

 *  查看異常信息: 第一行是 異常的說明 _空指針 ,角標越界, 并發(fā).....   

 *       詳細異常信息, 從下往上看, 能看懂哪行就是你發(fā)生異常的地方,基本上就是你寫錯代碼.

 *       Iterator.next()繼續(xù)迭代的時候報錯, 那么我們提供一種解決方案, 可以修改,但是不再遍歷集合.

 *   

 *  解決方案:  

 *  1.采用 Iterator中的方法 去操作集合  -- 推薦 !

 *  2.使用非Iterator中的方法操作, 那么再操作之后 ,break;  

 *   

 */

public class Demo {

public static void main(String[] args) {

 Collection<String> c = new ArrayList<>();

c.add("aaa");

c.add("bbb");

c.add("ccc");

c.add("ddd");

System.out.println(c);

Iterator<String> iterator = c.iterator();

while (iterator.hasNext()) {

String string =  iterator.next();

// 如果存在bbb ,那么添加一個EEE

if (string.equals("bbb")) {

c.add("bbb");

break;

}

}

System.out.println(c); //java.util.ConcurrentModificationException  并發(fā)修改異常

List l  = new ArrayList<>();

ListIterator listIterator = l.listIterator();

}

}


2.3.10 三種遍歷集合方式對比:

import java.util.ArrayList;

import java.util.Collection;

import java.util.Iterator;

/*

 *  1.集合變成數(shù)組  .遍歷數(shù)組  

 *  2.迭代器   -- 必須掌握

 *  3.增強for  -- 必須掌握

 */

public class Demo2 {

public static void main(String[] args) {

Collection<Object> c = new ArrayList<>();

c.add(111);

c.add(222);

c.add(333);

//1.變成數(shù)組

Object[] array = c.toArray();

for (int i = 0; i < array.length; i++) {

System.out.println(array[i]);

}

System.out.println("============");

//2.迭代器

Iterator<Object> iterator = c.iterator();

while (iterator.hasNext()) {

Object next = iterator.next();

System.out.println(next);

}

System.out.println("============");

//3.增強for

for (Object object : array) {

System.out.println(object);

}

}

}


第3章 集合綜合案例---斗地主洗牌發(fā)牌

3.1 案例介紹與演示

按照斗地主的規(guī)則,完成洗牌發(fā)牌的動作。

具體規(guī)則:

使用54張牌打亂順序

三個玩家參與游戲,三人交替摸牌,每人17張牌,最后三張留作底牌。

3.2 案例分析

準備牌:

牌可以設計為一個ArrayList<String>,每個字符串為一張牌。

每張牌由花色數(shù)字兩部分組成,我們可以使用花色集合與數(shù)字集合嵌套迭代完成每張牌的組裝。

牌由Collections類的shuffle方法進行隨機排序。

發(fā)牌:

將每個人以及底牌設計為ArrayList<String>,將最后3張牌直接存放于底牌,剩余牌通過對3取模依次發(fā)牌。

看牌:

直接打印每個集合

//牌類

public class Card {

String huaSe;// 花色 黑桃 ,紅桃 ,梅花, 方塊 大 小

String dianShu; // 2 A K Q J 10 .... 3

// 有參構造 .

public Card(String huaSe, String dianShu) {

super();

this.huaSe = huaSe;

this.dianShu = dianShu;

}

// 黑桃A

@Override

public String toString() {

return huaSe + dianShu;

}

}

//發(fā)牌機

import java.util.ArrayList;

/*

 * 發(fā)牌機類  . cardList 發(fā)牌機中的保存牌的集合.

 *

 * 創(chuàng)建發(fā)牌機 ,就同時初始化牌了.

 *

 */

public class FaPaiJi {

ArrayList<Card > cardList = new ArrayList<>();//   顯性初始化

// 無參構造

public FaPaiJi() {

super();

//初始化牌的操作

init();

}

/*

 * 初始化牌的方法 , 創(chuàng)建牌對象 ,保存到cardList

 */

private void init() {

//添加 王牌

cardList.add(new Card("",""));

cardList.add(new Card("",""));

// 添加 52張牌 .

//循環(huán)創(chuàng)建牌的對象.  

String[] dianShuArr  = {"2","A","K","Q","J" ,"10","9","8","7","6","5","4","3"};  

String[] huaSeArr  = {"黑桃","紅桃","梅花","方塊"};  

for (int i = 0; i < huaSeArr.length; i++) {

String huaSe = huaSeArr[i];  //獲取花色

for (int j = 0; j < dianShuArr.length; j++) {

String dianShu  = dianShuArr[j];  // 點數(shù)

cardList.add(new Card(huaSe, dianShu)); // 添加到集中  

}

}

}

public ArrayList<Card> getCardList() {

return cardList;

}

public void setCardList(ArrayList<Card> cardList) {

this.cardList = cardList;

}

}

//斗地主

import java.util.ArrayList;

import java.util.Collections;

import java.util.Iterator;

/*有牌, 洗牌 ,發(fā)牌.   

 *  *  定義牌 類  Card

 *  屬性:String  花色

 *     String  點數(shù)

 *  方法:  toString  

 *      

 *  定義發(fā)牌機類: 保存54.  

 *  屬性:ArrayList<Card> , 創(chuàng)建發(fā)牌機對象, 集合就裝滿了54張牌.

 *  方法 getCards ,獲取的就是 54張牌的集合.

 *  

 *  

 *   洗牌  Collections.shuffle  

 *   

 *   發(fā)牌: 大集合中牌,存到小集合(玩家)中去.  

 *   創(chuàng)建玩家的集合.

 *   發(fā)牌方法: 大集合中移除元素, 添加到新的小集合中.

 *   參數(shù): 大集合, 小集合

 *   返回值類型 : void

 *   

 */

public class DDZTest {

public static void main(String[] args) {

FaPaiJi faPaiJi = new FaPaiJi();

ArrayList<Card> cardList = faPaiJi.getCardList();

System.out.println(cardList);

// 洗牌  

Collections.shuffle(cardList);

System.out.println(cardList);

//創(chuàng)建 玩家集合對象

ArrayList<Card> player1 = new ArrayList<>();

ArrayList<Card> player2 = new ArrayList<>();

ArrayList<Card> player3 = new ArrayList<>();

//System.out.println("玩家1 " + player1);

// 發(fā)牌方法

faPai(cardList , player1);

faPai(cardList , player2);

faPai(cardList , player3);

System.out.println("玩家1 " + player1);

System.out.println("玩家2 " + player2);

System.out.println("玩家3 " + player3);

System.out.println("底牌 " + cardList);

System.out.println(cardList.size());

}

/*

 * 發(fā)牌方法: 大集合中移除元素, 添加到新的小集合中.

 *   參數(shù): 大集合, 小集合

 *   返回值類型 : void

 */

private static void faPai(ArrayList<Card> cardList, ArrayList<Card> player1) {

//從大集合中獲取元素,并移除 .

// 1.獲取大集合迭代器

Iterator<Card> iterator = cardList.iterator();

// 2. 獲取17

for (int i = 0; i < 17 ; i++) {

Card card = iterator.next();

//3.添加到小集合

player1.add(card);

iterator.remove();

}

}

}

utf-8改進

public class Card {

String huaSe;// 花色 黑桃 ,紅桃 ,梅花, 方塊 大 小

String dianShu; // 2 A K Q J 10 .... 3

// 有參構造 .

public Card(String huaSe, String dianShu) {

super();

this.huaSe = huaSe;

this.dianShu = dianShu;

}

// 黑桃A

@Override

public String toString() {

return huaSe + dianShu;

}

}

import java.util.ArrayList;

import java.util.Collections;

import java.util.Iterator;

import cn.javahelp_DDZ.Card;

/*????, ??? ,????.   

 *  *  ?????? ??  Card

 *  ????:String  ???

 *     String  ????

 *  ????:  toString  

 *    

 *    

 *  ????????: ????54??.  

 *  ????:ArrayList<Card> , ????????????, ??????????54????.

 *  ???? getCards ,???????? 54????????.

 *  

 *  

 *   ???  Collections.shuffle  

 *   

 *   ????: ???????,?浽С????(???)???.  

 *   ???????????.

 *   ???????: ???????????, ?????μ?С??????.

 *   ????: ???, С????

 *   ????????? : void

 *   

 *   修改編碼之后,原文件中 漢字會亂碼

 */

public class DDZTest {

public static void main(String[] args) {

FaPaiJi faPaiJi = new FaPaiJi();

ArrayList<Card> cardList = faPaiJi.getCardList();

System.out.println(cardList);

// ???  

Collections.shuffle(cardList);

System.out.println(cardList);

//???? ?????????

ArrayList<Card> player1 = new ArrayList<>();

ArrayList<Card> player2 = new ArrayList<>();

ArrayList<Card> player3 = new ArrayList<>();

//System.out.println("???1 " + player1);

// ???????

faPai(cardList , player1);

faPai(cardList , player2);

faPai(cardList , player3);

System.out.println("玩家1 " + player1);

System.out.println("玩家2 " + player2);

System.out.println("玩家3 " + player3);

System.out.println("底牌:" + cardList);

System.out.println(cardList.size());

}

/*

 * ???????: ???????????, ?????μ?С??????.

 *   ????: ???, С????

 *   ????????? : void

 */

private static void faPai(ArrayList<Card> cardList, ArrayList<Card> player1) {

//?????л?????,????? .

// 1.??????????

Iterator<Card> iterator = cardList.iterator();

// 2. ???17??

for (int i = 0; i < 17 ; i++) {

Card card = iterator.next();

//3.????С????

player1.add(card);

iterator.remove();

}

}

}

import java.util.ArrayList;

import cn.javahelp_DDZ.Card;

public class FaPaiJi {

ArrayList<Card > cardList = new ArrayList<>();//   顯性初始化

// 無參構造

public FaPaiJi() {

super();

//初始化牌的操作

init();

}

/*

 * 初始化牌的方法 , 創(chuàng)建牌對象 ,保存到cardList

 */

private void init() {

//添加 王牌

cardList.add(new Card("","?"));

cardList.add(new Card("",""));

// 添加 52張牌 .

//循環(huán)創(chuàng)建牌的對象.  

String[] dianShuArr  = {"2","A","K","Q","J" ,"10","9","8","7","6","5","4","3"};  

String[] huaSeArr  = {"?","?","?","?"};  

for (int i = 0; i < huaSeArr.length; i++) {

String huaSe = huaSeArr[i];  //獲取花色

for (int j = 0; j < dianShuArr.length; j++) {

String dianShu  = dianShuArr[j];  // 點數(shù)

cardList.add(new Card(huaSe, dianShu)); // 添加到集中

}

}

}

public ArrayList<Card> getCardList() {

return cardList;

}

public void setCardList(ArrayList<Card> cardList) {

this.cardList = cardList;

}

}


第4章 本日自習作業(yè):

4.1 知識點相關題

1:使用集合存儲String或者int,并分別使用增強for循環(huán)與迭代器方式訪問元素

2:完成斗地主課上案例

3:是否可以在發(fā)牌時,不使用帶索引的遍歷方式,轉而使用增強for循環(huán)或迭代器

4:完成以下需求:

公司有多名員工,具有普漲工資的方法:查找到多名員工的工資,如果沒有120000以上的月薪,則所有員工的工資普漲2000。

要求具有:公司類、員工類,員工該類集合與公司類為組合關系(員工是公司的成員變量)

5:Collection是接口還是類,ArrayList與Collection、AbstractCollection、Iterable、iterator()、List都是什么關系。

6:分別描述Iterable、Collection、List、Iterator四個接口的功能方法都有哪些

7:簡單描述目前掌握的集合體系

8:預習Set集合(明日難點:Set集合如何判斷元素唯一性,明日重點:認識更多的集合工具)

9:辨析集合與數(shù)組的異同點

4.2 代碼題

4.2.1 模擬 飯店結賬程序. 定義菜品,屬性為名稱,價格,數(shù)量. 集合中添加若干菜品對象. 遍歷集合,打印出集合中所有菜品名稱消費總金額.

答案:

public class Test1 {

public static void main(String[] args) {

ArrayList<Cai> al = new ArrayList<>();

Cai cai = new Cai("烤羊腿", 58, 4);

Cai cai2 = new Cai("皮蛋豆腐", 8, 1);

Cai cai3 = new Cai("烤板筋", 2, 10);

Cai cai4 = new Cai("可樂", 8, 1);

al.add(cai);

al.add(cai2);

al.add(cai3);

al.add(cai4);

System.out.println(al);

double total   =  0 ;

// 遍歷 集合

for (Cai c  : al) {

// 打印

String name = c.getName();

int num = c.getNum();

System.out.println("菜名 :" + name+" -- " + num +"");

// 計算 總價

//獲取 每一個 菜的單價 .   單價 * 份數(shù)  

total += c.getPrice()*num;

}

System.out.println("總價:" +  total);

}

}

class Cai {

private String name;

private double price;

private int num = 1; // 數(shù)量,默認一份.

public Cai(String name, double price, int num) {

super();

this.name = name;

this.price = price;

this.num = num;

}

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 int getNum() {

return num;

}

public void setNum(int num) {

this.num = num;

}

@Override

public String toString() {

return "菜品 [名稱=" + name + ", 單價=" + price + ", 數(shù)量=" + num + "]";

}

}


4.2.2 編寫一個程序的模擬班級學生的成績庫, 包含多項成績,英語,數(shù)學,Java.

實現(xiàn)如下功能:

1.可以修改某個學生的某項分數(shù).

2.可以打印全班同學成績

要求使用集合來完成.

public class Test2 {

public static void main(String[] args) {

ArrayList<Student> student_list = new ArrayList<>();

student_list.add(new Student("張三", 99, 90, 100));

student_list.add(new Student("李四", 89, 80, 100));

for (Student student : student_list) {

System.out.println(student);

}

Scanner scanner = new Scanner(System.in);

// 請輸入學生姓名

System.out.println("請輸入學生姓名 ");

String name = scanner.nextLine();

for (Student student : student_list) {

String n = student.getName();

if (n.equals(name)) {

// 可以繼續(xù)錄入, 修改

System.out.println("請輸入科目 ");

String kemu = scanner.nextLine();

System.out.println("請輸入分數(shù) ");

double score = scanner.nextDouble();

// 根據(jù)不同的科目,修改不同的分數(shù).

switch (kemu) {

case "數(shù)學":

student.setMath(score);

break;

case "英語":

student.setEnglish(score);

break;

case "java":

student.setJava(score);

break;

default:

System.out.println("查無此課, 對不起");

return ;

}

System.out.println("恭喜您, 修改成功");

System.out.println(student);

return;

}

}

System.out.println("對不起 ,查無此人");

}

}

class Student {

private double english;

private double math;

private double java;

private String name;

public Student() {

super();

}

public Student(String name, double english, double math, double java) {

super();

this.english = english;

this.math = math;

this.java = java;

this.name = name;

}

public double getEnglish() {

return english;

}

public void setEnglish(double english) {

this.english = english;

}

public double getMath() {

return math;

}

public void setMath(double math) {

this.math = math;

}

public double getJava() {

return java;

}

public void setJava(double java) {

this.java = java;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

@Override

public String toString() {

return "Student [english=" + english + ", math=" + math + ", java=" + java + ", name=" + name + "]";

}

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

long temp;

temp = Double.doubleToLongBits(english);

result = prime * result + (int) (temp ^ (temp >>> 32));

temp = Double.doubleToLongBits(java);

result = prime * result + (int) (temp ^ (temp >>> 32));

temp = Double.doubleToLongBits(math);

result = prime * result + (int) (temp ^ (temp >>> 32));

result = prime * result + ((name == null) ? 0 : name.hashCode());

return result;

}

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

Student other = (Student) obj;

if (Double.doubleToLongBits(english) != Double.doubleToLongBits(other.english))

return false;

if (Double.doubleToLongBits(java) != Double.doubleToLongBits(other.java))

return false;

if (Double.doubleToLongBits(math) != Double.doubleToLongBits(other.math))

return false;

if (name == null) {

if (other.name != null)

return false;

} else if (!name.equals(other.name))

return false;

return true;

}

}


    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多