- Java 内置有数百个类.
核心java API 是一堆等着被你当做组件使用的类的集合而成的.
- 前一章程序修改, 使用 ArrayList.
ArrayList 操作 ( 个人感觉有点类似链表 ) 总之, 在知道固定大小的时候, 尽量还是用普通数组.


- 掌握 java API 十分重要
- 比较 ArrayList 与一般数组




- 完整源码 ( 在原来基础上 )

GameTest.java/*
* File: GameTest.java
* -----------------------------
* This class is for testing, only has main method.
*/
public class GameTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Game game = new Game();
game.init();
// game.gameShow();
game.start();
game.gameOver();
}
}

Game.java*
* File: Game.java
* ----------------------------
* This class is for the main game.
* Game initialize, start and so on.
*
*/
import java.util.*;
public class Game {
/* initialize the game */
public void init() {
gameMap = new char[7][7];
dao1 = new Dao();
dao2 = new Dao();
dao3 = new Dao();
dao1.setDaoName("1-company");
dao2.setDaoName("2-company");
dao3.setDaoName("3-company");
dao1.setLocation(2, 3, 4);
dao2.setLocation(7, 14, 21);
dao3.setLocation(46,47, 48);
}
/* check the two dao's element equal or not */
private boolean isDaoEqual(ArrayList<Integer> a, ArrayList<Integer> b) {
for (int i: a) {
for (int j: b) {
if (i == j) {
return true;
}
}
}
return false;
}
/* check the two dao's element equal or not */
private boolean isDaoEqual(ArrayList<Integer> a, ArrayList<Integer> b, ArrayList<Integer> c) {
for (int i: a) {
for (int j: b) {
for (int k: c) {
if (i == j || j ==k || i == k) {
return true;
}
}
}
}
return false;
}
/* start the game */
public void start() {
Player player = new Player();
while (numHit < 9) {
int num = player.getAttackNum();
result = "Miss";
numAttack++;
if (!(isHit(num, dao1)) &&
!(isHit(num, dao2)) &&
!(isHit(num, dao3))) {
System.out.println(result);
}
}
}
/* check to attack miss or hitted */
private boolean isHit(int num, Dao dmo) {
for (int i: dmo.getLocation()) {
if (num == i) {
dmo.getLocation().remove(dmo.getLocation().indexOf(i));
numHit++;
if (dmo.getLocation().isEmpty()) {
result = "Killed";
System.out.println(dmo.getDaoName() + ": " + result);
return true;
} else {
result = "Hitted";
System.out.println(dmo.getDaoName() + ": " + result);
return true;
}
}
}
return false;
}
public void gameOver() {
System.out.println("Congatulation, you finish your game, the detail as below:");
System.out.println("Total attack times: "+numAttack);
}
public void gameShow() {
System.out.println(dao1.getDaoName());
for (int i: dao1.getLocation()) {
System.out.println(i + ",");
}
System.out.println(dao2.getDaoName());
for (int i: dao2.getLocation()) {
System.out.println(i + ",");
}
System.out.println(dao3.getDaoName());
for (int i: dao3.getLocation()) {
System.out.println(i + ",");
}
}
/* private instance values */
private String result = "Miss"; // every time, you attack and the result.
private ArrayList<Integer> daoLocation; // The location of the company.
private char[][] gameMap ; // The graph map for the game.
private int numAttack = 0; // count the attack number.
private int numHit = 0; // count the hit number.
private Dao dao1; // Dao company instance, dao1.
private Dao dao2; // Dao company instance, dao2.
private Dao dao3; // Dao company instance, dao3.
}

Player.java/*
* File: Player.java
* -----------------------------------
* This class is player, player can guess the game.
*/
import java.io.*;
public class Player {
public int getAttackNum() {
String inputLine = null;
try {
BufferedReader is = new BufferedReader(
new InputStreamReader(System.in));
inputLine = is.readLine();
if (inputLine.length() == 0) return -1;
} catch (IOException e) {
System.out.println("IoException:" + e);
}
int num = Integer.parseInt(inputLine);
return num;
}
}


Dao.java/*
* File: Dao.java
* ------------------------
* This class is for the company Dao.
* it will show on the Game map.
*/
import java.util.*;
public class Dao {
/* set the location of the company */
public void setLocation(int a, int b, int c) {
location.add(a);
location.add(b);
location.add(c);
}
/* create the location for the company */
public ArrayList<Integer> getLocation() {
return location;
}
/* set the name of the company */
public void setDaoName(String name) {
daoName = name;
}
/* getter method */
public String getDaoName() {
return daoName;
}
/* private instance values */
private ArrayList<Integer> location = new ArrayList<Integer>(); // save the location of the company.
private String daoName; // The name of company.
}
- 使用java函数库( Java API )
在Java的API中, 类是被包装在包中. 要使用API中的类, 你必须知道它被放在哪个包中.
包重要的原因:
- 首先, 它们可以帮助组织项目或函数库相对于一大堆零散的类, 以功能来组织会比较好
- 其次, 包可以制造出名称空间, 以便错开相同名称的类.
- 最后, 包可以通过限制同一包之间的类才能相互存取以维护安全性.
必须要告诉java, 你使用的是哪一个ArrayList(类), 有两种方法:
- IMPORT: 放在源文件最前面
- 在程序中打出全名, 不管在哪里, 只要有使用到就打出全名, java.util.ArrayList<Dog> list = new java.util.ArrayList<Dog>();
来自 java.lang 这个包中的内容, 可以直接使用.
javax 这个包: 首先它是相对的, 标准的包都是 java.util java.io 之类的, 只有扩展的或非标准的才使用 javax, 后来也是标准的.
类的完整名称: 包名+类名, 例如 java.util.ArrayList
- import 与 c语言中的 include
使用 import 不会使程序变大, 这一点与 c语言的 include 是不同的.
- 如何查询 API
1. 库中有哪些类?
2. 找到类之后, 你怎么知道它是做什么的?
查阅参考书 或 HTML API 文档