集合html
一:.數據結構介紹java
1.通常將數據結構分爲兩大類:線性數據結構和非線性數據結構。算法
(1)線性數據結構:線性表、棧、隊列、串、數組和文件。編程
(2)非線性數據結構:樹和圖。數組
2.線性表:數據結構
3.鏈表:(1)單向鏈表框架
(2)循環鏈表dom
(3)雙向循環鏈表ide
4.棧:棧(Stack)也是一種特殊的線性表,是一種後進先出(LIFO)的結構。棧是限定僅在表尾進行插入和刪除運算的線性表,表尾稱爲棧頂(top),函數
表頭稱爲棧底(bottom)。
5.隊列:隊列(Queue)是限定全部的插入只能在表的一端進行,而全部的刪除都在表的另外一端進行的線性表。
表中容許插入的一端稱爲隊尾(Rear),容許刪除的一端稱爲隊頭(Front)。
隊列的操做是按先進先出(FIFO)的原則進行的。
6.散列表:散列表又稱爲哈希表。散列表算法的基本思想是:以結點的關鍵字爲自變量,經過必定的函數關係(散列函數)計算出對應的函數值,
以這個值做爲該結點存儲在散列表中
的地址。當散列表中的元素存放太滿,就必須進行再散列,將產生一個新的散列表,全部元素存放到新的散列表中,原先的散列表將被刪除。
7.JAVA的集合框架:JAVA的集合框架實現對各類數據結構的封裝,以下降對數據管理與處理的難度所謂框架就是一個類庫的集合,
框架中包含不少超類,編程者建立這些超類的子類可較方便的設計設計程序所需的類。例如:Swing類包
集合(Collection或稱爲容器)是一種包含多個元素並提供對所包含元素操做方法的類,其包含的元素能夠由同一類型的對象組成,也能夠由不一樣類
型的對象組成。
8.集合類的做用:
– Java的集合類提供了一些基本數據結構的支持。
– 例如Vector、Hashtable、Stack等。
9.集合類的使用:
– Java的集合類包含在java.util包中。
– import java.util.*;
10.集合類的特色:
(1)只容納對象。數組能夠容納基本數據類型數據和對象。
若是集合類中想使用基本數據類型,又想利用集合類的靈活性,能夠把基本數據類型數據封裝成該數據類型的包裝器對象,而後放入集合中處理。
(2) 集合類容納的對象都是Object類的實例,一旦把一個對象置入集合類中,它的類信息將丟失,這樣設計的目的是爲了集合類的通用性。
由於Object類是全部類的祖先,因此能夠在這些集合中存聽任何類的對象而不受限制,但切記在使用集合成員以前必須對它從新造型。
11.新舊集合類:在JDK1.0和JDK 1.1中提供了Vector(矢量),Hashtable ( 哈希表) , Stack ( 堆棧) ,Properties(屬性集)等集合類,
儘管這些類很是有用,但卻彼此獨立,缺乏一個統一集中的機制。
(1)Vector類:Vector類相似長度可變的數組。 Vector中只能存放對象。 Vector的元素經過下標進行訪問。
(2)Stack類:Stack類是Vector的子類。 Stack類描述堆棧數據結構,即LIFO。
(3)Hashtable類:Hashtable經過鍵來查找元素。Hashtable用散列碼(hashcode)來肯定鍵。
全部對象都有一個散列碼,能夠經過Object類的hashCode()方法得到。
12.在JDK1.2中,JAVA設計了一個統一的類集,並對上述類進行了改寫,使其統一歸入JAVA的集合框架。
集合框架中的基本接口:
(1)Collection:集合層次中的根接口,JDK未提供這個接口的直接實現類。
(2)Set:不能包含重複的元素。對象可能不是按存放的次序存放,也就是說不能像數組同樣按索引的方式進行訪問,SortedSet是一個按照升序排列元素的Set。
(3)List:是一個有序的集合,能夠包含重複的元素。提供了按索引訪問的方式。
(4)Map:包含了key-value對。Map不能包含重複的key
(5)SortedMap是一個按照升序排列key的Map。
實驗十一 集合
實驗時間 2018-11-8
1、實驗目的與要求
(1) 掌握Vetor、Stack、Hashtable三個類的用途及經常使用API;
(2) 瞭解java集合框架體系組成;
(3) 掌握ArrayList、LinkList兩個類的用途及經常使用API。
(4) 瞭解HashSet類、TreeSet類的用途及經常使用API。
(5)瞭解HashMap、TreeMap兩個類的用途及經常使用API;
(6) 結對編程(Pair programming)練習,體驗程序開發中的兩人合做。
2、實驗內容和步驟
實驗1: 導入第9章示例程序,測試程序並進行代碼註釋。
測試程序1:
l 使用JDK命令運行編輯、運行如下三個示例程序,結合運行結果理解程序;
l 掌握Vetor、Stack、Hashtable三個類的用途及經常使用API。
//示例程序1 import java.util.Vector;
class Cat { private int catNumber;
Cat(int i) { catNumber = i; }
void print() { System.out.println("Cat #" + catNumber); } }
class Dog { private int dogNumber;
Dog(int i) { dogNumber = i; }
void print() { System.out.println("Dog #" + dogNumber); } }
public class CatsAndDogs { public static void main(String[] args) { Vector cats = new Vector(); for (int i = 0; i < 7; i++) cats.addElement(new Cat(i)); cats.addElement(new Dog(7)); for (int i = 0; i < cats.size(); i++) ((Cat) cats.elementAt(i)).print(); } } |
//示例程序2 import java.util.*;
public class Stacks { static String[] months = { "1", "2", "3", "4" };
public static void main(String[] args) { Stack stk = new Stack(); for (int i = 0; i < months.length; i++) stk.push(months[i]); System.out.println(stk); System.out.println("element 2=" + stk.elementAt(2)); while (!stk.empty()) System.out.println(stk.pop()); } } |
//示例程序3 import java.util.*;
class Counter { int i = 1;
public String toString() { return Integer.toString(i); } }
public class Statistics { public static void main(String[] args) { Hashtable ht = new Hashtable(); for (int i = 0; i < 10000; i++) { Integer r = new Integer((int) (Math.random() * 20)); if (ht.containsKey(r)) ((Counter) ht.get(r)).i++; else ht.put(r, new Counter()); } System.out.println(ht); } } |
示例程序1運行結果以下:
import java.util.Vector; class Cat { private int catNumber; Cat(int i) { catNumber = i; } void print() { System.out.println("Cat #" + catNumber); } } class Dog { private int dogNumber; Dog(int i) { dogNumber = i; } void print() { System.out.println("Dog #" + dogNumber); } } public class CatsAndDogs { public static void main(String[] args) { Vector cats = new Vector(); for (int i = 0; i < 7; i++) cats.addElement(new Cat(i)); cats.addElement(new Dog(7)); for (int i = 0; i < cats.size(); i++) { //if(cats.elementAt(i) instanceof Cat) { //((Cat) cats.elementAt(i)).print(); //} ((Cat) cats.elementAt(i)).print();//程序有未檢查異常 // else { // ((Dog) cats.elementAt(i)).print(); // } } } }
程序進行修改後:
import java.util.Vector; class Cat { private int catNumber; Cat(int i) { catNumber = i; } void print() { System.out.println("Cat #" + catNumber); } } class Dog { private int dogNumber; Dog(int i) { dogNumber = i; } void print() { System.out.println("Dog #" + dogNumber); } } public class CatsAndDogs { public static void main(String[] args) { Vector cats = new Vector(); for (int i = 0; i < 7; i++) cats.addElement(new Cat(i)); cats.addElement(new Dog(7)); for (int i = 0; i < cats.size(); i++) { if(cats.elementAt(i) instanceof Cat) {//instanceof指出對象是不是cat類 ((Cat) cats.elementAt(i)).print(); } //((Cat) cats.elementAt(i)).print();//程序有未檢查異常 else { ((Dog) cats.elementAt(i)).print();//判斷後若不是則返回dog類 } } } }
示例程序2測試以下:
import java.util.*; public class Stacks { static String[] months = { "1", "2", "3", "4" }; public static void main(String[] args) { Stack stk = new Stack(); for (int i = 0; i < months.length; i++) stk.push(months[i]);//入棧操做 System.out.println(stk); System.out.println("element 2=" + stk.elementAt(2)); //elementAt(2)該方法在沒有list之前跟get方法的功能徹底相同 while (!stk.empty()) System.out.println(stk.pop());//出棧操做 } }
示例程序3運行以下:
import java.util.*; class Counter { int i = 1;//權限 public String toString() { return Integer.toString(i); } } public class Statistics { public static void main(String[] args) { Hashtable ht = new Hashtable(); for (int i = 0; i < 10000; i++) { Integer r = new Integer((int) (Math.random() * 20));//random半開半閉區間0-19的數 //Integer是int的封裝類,將生成的數據轉化成整型,讓r去調用它 if (ht.containsKey(r)) ((Counter) ht.get(r)).i++;//直接在類外部引用屬性是由於i的訪問權限是friendly //經過get方法經過鍵得到對應的value值 else ht.put(r, new Counter());//調用put方法使得哈希表建立一個新的鍵值對 } System.out.println(ht); } }
測試程序2:
l 使用JDK命令編輯運行ArrayListDemo和LinkedListDemo兩個程序,結合程序運行結果理解程序;
import java.util.*;
public class ArrayListDemo { public static void main(String[] argv) { ArrayList al = new ArrayList(); // Add lots of elements to the ArrayList... al.add(new Integer(11)); al.add(new Integer(12)); al.add(new Integer(13)); al.add(new String("hello")); // First print them out using a for loop. System.out.println("Retrieving by index:"); for (int i = 0; i < al.size(); i++) { System.out.println("Element " + i + " = " + al.get(i)); } } } |
import java.util.*; public class LinkedListDemo { public static void main(String[] argv) { LinkedList l = new LinkedList(); l.add(new Object()); l.add("Hello"); l.add("zhangsan"); ListIterator li = l.listIterator(0); while (li.hasNext()) System.out.println(li.next()); if (l.indexOf("Hello") < 0) System.err.println("Lookup does not work"); else System.err.println("Lookup works"); } } |
import java.util.*; public class ArrayListDemo { public static void main(String[] argv) { ArrayList al = new ArrayList();//ArrayList類生成一個數組由a1調用使用 // Add lots of elements to the ArrayList... al.add(new Integer(11)); //integer是與int不一樣的數據類型,往ArrayList中放東西時,像int這種內建類型是放不進去的。 al.add(new Integer(12)); al.add(new Integer(13)); al.add(new String("hello")); // First print them out using a for loop. System.out.println("Retrieving by index:"); for (int i = 0; i < al.size(); i++) { System.out.println("Element " + i + " = " + al.get(i));//已有list,可直接用get方法 } } }
import java.util.*; public class LinkedListDemo {//以雙向鏈表的形式完成操做 public static void main(String[] argv) { LinkedList l = new LinkedList(); l.add(new Object()); l.add("Hello"); l.add("zhangsan"); ListIterator li = l.listIterator(0);//listIterator(迭代器)用來遍歷集合 while (li.hasNext()) System.out.println(li.next()); if (l.indexOf("Hello") < 0) System.err.println("Lookup does not work"); else System.err.println("Lookup works"); } }
l 在Elipse環境下編輯運行調試教材360頁程序9-1,結合程序運行結果理解程序;
l 掌握ArrayList、LinkList兩個類的用途及經常使用API。
package linkedList; import java.util.*; /** * This program demonstrates operations on linked lists. * @version 1.11 2012-01-26 * @author Cay Horstmann */ public class LinkedListTest { public static void main(String[] args) { List<String> a = new LinkedList<>();//建立一個泛型類型,使得a能引用它 a.add("Amy"); a.add("Carl"); a.add("Erica"); List<String> b = new LinkedList<>(); b.add("Bob"); b.add("Doug"); b.add("Frances"); b.add("Gloria"); // merge the words from b into a ListIterator<String> aIter = a.listIterator();//遍歷a中的全部元素,且a中元素爲string類 Iterator<String> bIter = b.iterator(); while (bIter.hasNext()) { if (aIter.hasNext()) aIter.next(); aIter.add(bIter.next()); } System.out.println(a); // remove every second word from b bIter = b.iterator(); while (bIter.hasNext()) { bIter.next(); // skip one element if (bIter.hasNext()) { bIter.next(); // skip next element bIter.remove(); // remove that element } } System.out.println(b); // bulk operation: remove all words in b from a a.removeAll(b); System.out.println(a); } }
測試程序3:
l 運行SetDemo程序,結合運行結果理解程序;
import java.util.*; public class SetDemo { public static void main(String[] argv) { HashSet h = new HashSet(); //也能夠 Set h=new HashSet() h.add("One"); h.add("Two"); h.add("One"); // DUPLICATE h.add("Three"); Iterator it = h.iterator(); while (it.hasNext()) { System.out.println(it.next()); } } } |
import java.util.*; public class SetDemo { public static void main(String[] argv) { HashSet h = new HashSet(); //也能夠 Set h=new HashSet() h.add("One"); h.add("Two"); h.add("One"); // DUPLICATE h.add("Three"); Iterator it = h.iterator(); while (it.hasNext()) { System.out.println(it.next()); } } }
l 在Elipse環境下調試教材365頁程序9-2,結合運行結果理解程序;瞭解HashSet類的用途及經常使用API。
package set; import java.util.*; /** * This program uses a set to print all unique words in System.in. * @version 1.12 2015-06-21 * @author Cay Horstmann */ public class SetTest { public static void main(String[] args) { Set<String> words = new HashSet<>(); // HashSet implements Set long totalTime = 0; try (Scanner in = new Scanner(System.in)) { while (in.hasNext()) { String word = in.next(); long callTime = System.currentTimeMillis(); words.add(word); callTime = System.currentTimeMillis() - callTime; totalTime += callTime; } } Iterator<String> iter = words.iterator(); for (int i = 1; i <= 20 && iter.hasNext(); i++) System.out.println(iter.next()); System.out.println(". . ."); System.out.println(words.size() + " distinct words. " + totalTime + " milliseconds."); } }
l 在Elipse環境下調試教材367頁-368程序9-三、9-4,結合程序運行結果理解程序;瞭解TreeSet類的用途及經常使用API。
package treeSet; import java.util.*; /** * An item with a description and a part number. */ public class Item implements Comparable<Item> { private String description; private int partNumber; /** * Constructs an item. * * @param aDescription * the item's description * @param aPartNumber * the item's part number */ public Item(String aDescription, int aPartNumber) { description = aDescription; partNumber = aPartNumber; } /** * Gets the description of this item. * * @return the description */ public String getDescription() { return description; } public String toString() { return "[description=" + description + ", partNumber=" + partNumber + "]"; } public boolean equals(Object otherObject) { if (this == otherObject) return true; if (otherObject == null) return false; if (getClass() != otherObject.getClass()) return false; Item other = (Item) otherObject; return Objects.equals(description, other.description) && partNumber == other.partNumber; } public int hashCode() { return Objects.hash(description, partNumber); } public int compareTo(Item other) { int diff = Integer.compare(partNumber, other.partNumber); return diff != 0 ? diff : description.compareTo(other.description); } }
package treeSet; import java.util.*; /** * This program sorts a set of item by comparing their descriptions. * @version 1.12 2015-06-21 * @author Cay Horstmann */ public class TreeSetTest { public static void main(String[] args) { SortedSet<Item> parts = new TreeSet<>(); parts.add(new Item("Toaster", 1234)); parts.add(new Item("Widget", 4562)); parts.add(new Item("Modem", 9912)); System.out.println(parts); NavigableSet<Item> sortByDescription = new TreeSet<>( Comparator.comparing(Item::getDescription)); sortByDescription.addAll(parts); System.out.println(sortByDescription); } }
測試程序4:
l 使用JDK命令運行HashMapDemo程序,結合程序運行結果理解程序;
import java.util.*; public class HashMapDemo { public static void main(String[] argv) { HashMap h = new HashMap(); // The hash maps from company name to address. h.put("Adobe", "Mountain View, CA"); h.put("IBM", "White Plains, NY"); h.put("Sun", "Mountain View, CA"); String queryString = "Adobe"; String resultString = (String)h.get(queryString); System.out.println("They are located in: " + resultString); } } |
測試程序:
import java.util.*; public class HashMapDemo { public static void main(String[] argv) { HashMap h = new HashMap(); // The hash maps from company name to address. h.put("Adobe", "Mountain View, CA"); h.put("IBM", "White Plains, NY"); h.put("Sun", "Mountain View, CA"); String queryString = "Adobe";//訪問指定的關鍵字Adobe String resultString = (String)h.get(queryString); System.out.println("They are located in: " + resultString); } }
l 在Elipse環境下調試教材373頁程序9-6,結合程序運行結果理解程序;
l 瞭解HashMap、TreeMap兩個類的用途及經常使用API。
package map; /** * A minimalist employee class for testing purposes. */ public class Employee { private String name; private double salary; /** * Constructs an employee with $0 salary. * @param n the employee name */ public Employee(String name) { this.name = name; salary = 0; } public String toString() { return "[name=" + name + ", salary=" + salary + "]"; } }
package map; import java.util.*; /** * This program demonstrates the use of a map with key type String and value type Employee. * @version 1.12 2015-06-21 * @author Cay Horstmann */ public class MapTest { public static void main(String[] args) { Map<String, Employee> staff = new HashMap<>(); staff.put("144-25-5464", new Employee("Amy Lee")); staff.put("567-24-2546", new Employee("Harry Hacker")); staff.put("157-62-7935", new Employee("Gary Cooper")); staff.put("456-62-5527", new Employee("Francesca Cruz")); // print all entries System.out.println(staff); // remove an entry staff.remove("567-24-2546"); // replace an entry staff.put("456-62-5527", new Employee("Francesca Miller")); // look up a value System.out.println(staff.get("157-62-7935")); // iterate through all entries staff.forEach((k, v) -> System.out.println("key=" + k + ", value=" + v)); } }
實驗2:結對編程練習:
l 關於結對編程:如下圖片是一個結對編程場景:兩位學習夥伴坐在一塊兒,面對着同一臺顯示器,使用着同一鍵盤,同一個鼠標,他們一塊兒思考問題,一塊兒分析問題,一塊兒編寫程序。
l 關於結對編程的闡述可參見如下連接:
http://www.cnblogs.com/xinz/archive/2011/08/07/2130332.html
http://en.wikipedia.org/wiki/Pair_programming
l 對於結對編程中代碼設計規範的要求參考:
http://www.cnblogs.com/xinz/archive/2011/11/20/2255971.html
如下實驗,就讓咱們來體驗一下結對編程的魅力。
l 肯定本次實驗結對編程合做夥伴;
合做夥伴:李婷華
l 各自運行合做夥伴實驗九編程練習1,結合使用體驗對所運行程序提出完善建議;
夥伴的代碼及運行結果:
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; import java.util.Collections;//對集合進行排序、查找、修改等; public class Test { private static ArrayList<Citizen> citizenlist; public static void main(String[] args) { citizenlist = new ArrayList<>(); Scanner scanner = new Scanner(System.in); File file = new File("E:/java/身份證號.txt"); //異常捕獲 try { FileInputStream fis = new FileInputStream(file); BufferedReader in = new BufferedReader(new InputStreamReader(fis)); String temp = null; while ((temp = in.readLine()) != null) { Scanner linescanner = new Scanner(temp); linescanner.useDelimiter(" "); String name = linescanner.next(); String id = linescanner.next(); String sex = linescanner.next(); String age = linescanner.next(); String birthplace = linescanner.nextLine(); Citizen citizen = new Citizen(); citizen.setName(name); citizen.setId(id); citizen.setSex(sex); // 將字符串轉換成10進制數 int ag = Integer.parseInt(age); citizen.setage(ag); citizen.setBirthplace(birthplace); citizenlist.add(citizen); } } catch (FileNotFoundException e) { System.out.println("信息文件找不到"); e.printStackTrace(); } catch (IOException e) { System.out.println("信息文件讀取錯誤"); e.printStackTrace(); } boolean isTrue = true; while (isTrue) { System.out.println("1.按姓名字典序輸出人員信息"); System.out.println("2.查詢最大年齡的人員信息、查詢最小年齡人員信息"); System.out.println("3.查詢人員中是否查詢人員中是否有你的同鄉"); System.out.println("4.輸入你的年齡,查詢文件中年齡與你最近人的姓名、身份證號、年齡、性別和出生地"); System.out.println("5.退出"); int nextInt = scanner.nextInt(); switch (nextInt) { case 1: Collections.sort(citizenlist); System.out.println(citizenlist.toString()); break; case 2: int max = 0, min = 100; int m, k1 = 0, k2 = 0; for (int i = 1; i < citizenlist.size(); i++) { m = citizenlist.get(i).getage(); if (m > max) { max = m; k1 = i; } if (m < min) { min = m; k2 = i; } } System.out.println("年齡最大:" + citizenlist.get(k1)); System.out.println("年齡最小:" + citizenlist.get(k2)); break; case 3: System.out.println("出生地:"); String find = scanner.next(); String place = find.substring(0, 3); for (int i = 0; i < citizenlist.size(); i++) { if (citizenlist.get(i).getBirthplace().substring(1, 4).equals(place)) System.out.println("出生地" + citizenlist.get(i)); } break; case 4: System.out.println("年齡:"); int yourage = scanner.nextInt(); int near = peer(yourage); int j = yourage - citizenlist.get(near).getage(); System.out.println("" + citizenlist.get(near)); break; case 5: isTrue = false; System.out.println("程序已退出!"); break; default: System.out.println("輸入有誤"); } } } public static int peer(int age) { int flag = 0; int min = 53, j = 0; for (int i = 0; i < citizenlist.size(); i++) { j = citizenlist.get(i).getage() - age; if (j < 0) j = -j; if (j < min) { min = j; flag = i; } } return flag; } }
public class Citizen implements Comparable<Citizen> { private String name; private String id; private String sex; private int age; private String birthplace; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getage() { return age; } public void setage(int age) { this.age = age; } public String getBirthplace() { return birthplace; } public void setBirthplace(String birthplace) { this.birthplace = birthplace; } public int compareTo(Citizen other) { return this.name.compareTo(other.getName()); } public String toString() { return name + "\t" + sex + "\t" + age + "\t" + id + "\t" + birthplace + "\n"; } }
l 各自運行合做夥伴實驗十編程練習2,結合使用體驗對所運行程序提出完善建議;
import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; public class calculator { public static void main(String[] args) { Scanner in = new Scanner(System.in); Count count=new Count(); PrintWriter out = null; try { out = new PrintWriter("test.txt"); int sum = 0; for (int i = 1; i <=10; i++) { int a = (int) Math.round(Math.random() * 100); int b = (int) Math.round(Math.random() * 100); int menu = (int) Math.round(Math.random() * 3); switch (menu) { case 0: System.out.println(i+":"+a + "+" + b + "="); int c1 = in.nextInt(); out.println(a + "+" + b + "=" + c1); if (c1 == (a + b)) { sum += 10; System.out.println("恭喜答案正確"); } else { System.out.println("抱歉,答案錯誤"); } break; case 1: while (a < b) { b = (int) Math.round(Math.random() * 100); } System.out.println(i+":"+a + "-" + b + "="); int c2 = in.nextInt(); out.println(a + "-" + b + "=" + c2); if (c2 == (a - b)) { sum += 10; System.out.println("恭喜答案正確"); } else { System.out.println("抱歉,答案錯誤"); } break; case 2: System.out.println(i+":"+a + "*" + b + "="); int c3 = in.nextInt(); out.println(a + "*" + b + "=" + c3); if (c3 == a * b) { sum += 10; System.out.println("恭喜答案正確"); } else { System.out.println("抱歉,答案錯誤"); } break; case 3: while(b == 0){ b = (int) Math.round(Math.random() * 100); } while(a % b != 0){ a = (int) Math.round(Math.random() * 100); } System.out.println(i+":"+a + "/" + b + "="); int c4 = in.nextInt(); if (c4 == a / b) { sum += 10; System.out.println("恭喜答案正確"); } else { System.out.println("抱歉,答案錯誤"); } break; } } System.out.println("你的得分爲" + sum); out.println("你的得分爲" + sum); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
public class Count<T> { private T a; private T b; public Count() { a=null; b=null; } public Count(T a,T b) { this.a=a; this.b=b; } public int count1(int a,int b) { return a+b; } public int count2(int a,int b) { return a-b; } public int count3(int a,int b) { return a*b; } public int count4(int a,int b) { return a/b; } }
l 採用結對編程方式,與學習夥伴合做完成實驗九編程練習1;
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Scanner; public class Xinxi { private static ArrayList<Student> studentlist; public static void main(String[] args) { studentlist = new ArrayList<>(); Scanner scanner = new Scanner(System.in); File file = new File("D:\\身份證號\\身份證號.txt"); try { FileInputStream fis = new FileInputStream(file); BufferedReader in = new BufferedReader(new InputStreamReader(fis)); String temp = null; while ((temp = in.readLine()) != null) { Scanner linescanner = new Scanner(temp); linescanner.useDelimiter(" "); String name = linescanner.next(); String number = linescanner.next(); String sex = linescanner.next(); String age = linescanner.next(); String province = linescanner.nextLine(); Student student = new Student(); student.setName(name); student.setnumber(number); student.setsex(sex); int a = Integer.parseInt(age); student.setage(a); student.setprovince(province); studentlist.add(student); } } catch (FileNotFoundException e) {//添加的異常處理語句try{ }catch{ }語句 System.out.println("所找信息文件找不到"); e.printStackTrace(); } catch (IOException e) { System.out.println("所找信息文件讀取錯誤");//採起積極方法捕獲異常,並將異常返回本身所設定的打印文字 e.printStackTrace(); } boolean isTrue = true; while (isTrue) { System.out.println("選擇你的操做,輸入正確格式的選項"); System.out.println("1按姓名字典序輸出人員信息"); System.out.println("2.查詢最大和最小年齡的人員信息"); System.out.println("3.尋找年齡相近的人的信息"); System.out.println("4.尋找老鄉"); System.out.println("5.退出"); String n = scanner.next(); switch (n) { case "1": Collections.sort(studentlist); System.out.println(studentlist.toString()); break; case "2": int max = 0, min = 100; int j, k1 = 0, k2 = 0; for (int i = 1; i < studentlist.size(); i++) { j = studentlist.get(i).getage(); if (j > max) { max = j; k1 = i; } if (j < min) { min = j; k2 = i; } } System.out.println("年齡最大:" + studentlist.get(k1)); System.out.println("年齡最小:" + studentlist.get(k2)); break; case "3": System.out.println("家鄉在哪裏?"); String find = scanner.next(); String place = find.substring(0, 3); for (int i = 0; i < studentlist.size(); i++) { if (studentlist.get(i).getprovince().substring(1, 4).equals(place)) System.out.println("同鄉" + studentlist.get(i)); } break; case "4": System.out.println("年齡:"); int yourage = scanner.nextInt(); int near = agenear(yourage); int value = yourage - studentlist.get(near).getage(); System.out.println("" + studentlist.get(near)); break; case "5": isTrue = false; System.out.println("退出程序!"); break; default: System.out.println("輸入有誤"); } } } public static int agenear(int age) { int j = 0, min = 53, value = 0, flag = 0; for (int i = 0; i < studentlist.size(); i++) { value = studentlist.get(i).getage() - age; if (value < 0) value = -value; if (value < min) { min = value; flag = i; } } return flag; } } xinxi
public class Student implements Comparable<Student> { private String name; private String number; private String sex; private String province; private int age; public void setName(String name) { // TODO 自動生成的方法存根 this.name = name; } public String getName() { // TODO 自動生成的方法存根 return name; } public void setnumber(String number) { // TODO 自動生成的方法存根 this.number = number; } public String getNumber() { // TODO 自動生成的方法存根 return number; } public void setsex(String sex) { // TODO 自動生成的方法存根 this.sex = sex; } public String getsex() { // TODO 自動生成的方法存根 return sex; } public void setprovince(String province) { // TODO 自動生成的方法存根 this.province = province; } public String getprovince() { // TODO 自動生成的方法存根 return province; } public void setage(int a) { // TODO 自動生成的方法存根 this.age = age; } public int getage() { // TODO 自動生成的方法存根 return age; } public int compareTo(Student o) { return this.name.compareTo(o.getName()); } public String toString() { return name + "\t" + sex + "\t" + age + "\t" + number + "\t" + province + "\n"; } } student類
l 採用結對編程方式,與學習夥伴合做完成實驗十編程練習2。
import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; public class calculator { public static void main(String[] args) { Scanner in = new Scanner(System.in); Count count=new Count(); PrintWriter out = null; try { out = new PrintWriter("test.txt"); int sum = 0; for (int i = 1; i <=10; i++) { int a = (int) Math.round(Math.random() * 100); int b = (int) Math.round(Math.random() * 100); int menu = (int) Math.round(Math.random() * 3); switch (menu) { case 0: System.out.println(i+":"+a + "+" + b + "="); int c1 = in.nextInt(); out.println(a + "+" + b + "=" + c1); if (c1 == (a + b)) { sum += 10; System.out.println("恭喜答案正確"); } else { System.out.println("抱歉,答案錯誤"); } break; case 1: while (a < b) { b = (int) Math.round(Math.random() * 100); } System.out.println(i+":"+a + "-" + b + "="); int c2 = in.nextInt(); out.println(a + "-" + b + "=" + c2); if (c2 == (a - b)) { sum += 10; System.out.println("恭喜答案正確"); } else { System.out.println("抱歉,答案錯誤"); } break; case 2: System.out.println(i+":"+a + "*" + b + "="); int c3 = in.nextInt(); out.println(a + "*" + b + "=" + c3); if (c3 == a * b) { sum += 10; System.out.println("恭喜答案正確"); } else { System.out.println("抱歉,答案錯誤"); } break; case 3: while(b == 0){ b = (int) Math.round(Math.random() * 100); } while(a % b != 0){ a = (int) Math.round(Math.random() * 100); } System.out.println(i+":"+a + "/" + b + "="); int c4 = in.nextInt(); if (c4 == a / b) { sum += 10; System.out.println("恭喜答案正確"); } else { System.out.println("抱歉,答案錯誤"); } break; } } System.out.println("你的得分爲" + sum); out.println("你的得分爲" + sum); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
public class Count<T> { private T a; private T b; public Count() { a=null; b=null; } public Count(T a,T b) { this.a=a; this.b=b; } public int count1(int a,int b) { return a+b; } public int count2(int a,int b) { return a-b; } public int count3(int a,int b) { return a*b; } public int count4(int a,int b) { return a/b; } }
綜上所述:結對編程有很大的益處,對於有些我的能力限制及思考不到的問題,同伴合做總能產生一些新穎的想法,同時在本身不能修改本身的程序錯誤或沒有思路的時候
同伴總能給予必定的思路引導。
實驗總結:經過本週的學習我瞭解了新舊集合類的不一樣, 掌握了Vetor、Stack、Hashtable三個類的用途,瞭解了java集合框架體系組成;
掌握ArrayList、LinkList兩個類的用途及經常使用API。 對於HashSet類、TreeSet類的用途以及HashMap、TreeMap兩個類的用途尚且理解不是很到位。
初步對結對練習編程有了瞭解,並與個人同伴互相對實驗九的編程進行了閱讀並對咱們的寫程序的思想互相分享,並相互借鑑對方比較新穎的思路。