?? 公交換乘算法.txt
字號:
偶5月份寫的公交換乘算法:
公交換乘一站算法busTransfer
/**
* 公交換乘一站的算法思想:
* (注意:車次信息、站點信息、公交信息是等價的都是以HashMap的形式存儲信息)
* 1.從數據庫中獲得所有公交信息存儲到ArrayList,每個具體信息的元數據有三個:
* 公交車次、公交站點、該公交站點距離該公交車次的始發站點的站數,具體信息用HashMap保存
* 2.然后把公交信息數據進行結構化,把所有公交站點抽出,再把每一個站點對應的所有車次抽出
* 與其一一對應,單一的車次信息用HashMap存儲,站點對應的所有車次信息用ArrayList存儲,
* 所有的站點有經過站點的車次信息用HashMap存儲
* 3.根據查詢要求,分別從結構化以后的公交信息數據中找到,經過出發地的所有車次,經過目的地
* 的所有車次,然后在分別遍歷每個車次,篩選出符合要求的中轉站點,篩選規則是:每查詢出一個
* 站點時,得到該站點距離該站點對應車次的始發站的站數,如果這個站數小于查詢站點距離該車次的始
* 發站的站數,那么就符合規則,便把該站點信息保存到符合站點的ArrayList中,反之亦然
* 4.分別得到查詢條件中出發地和目的地的中轉站點信息(中轉站點信息存儲在ArrayList中),然
* 后遍歷兩個中轉站點信息的集合,得到最終的具體中轉信息(最終中轉信息也是用ArrayList存儲)
*/
package bustransfer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
public class BusTransfer {
private String start = null;// 出發地
private String whither = null;// 目標地
private List schedule = null;// 用于緩存列車時刻表。
private HashMap <String, ArrayList> stationsOfLine = null; // 所有公交線路,每個list存儲該線路經過的所有車站。
private HashMap <String, ArrayList> linesOfStation = null;// 所有車站,每個list中存儲通過該車站的所有車次。
// private ArrayList <String> startLine = new ArrayList <String>();//
// 途經出發地的所有車次。
// private ArrayList <String> whitherLine = new ArrayList <String>();//
// 途經目的地的所有車次。
private ArrayList <Map> firLineStaList = new ArrayList <Map>();
private ArrayList <Map> secLineStaList = new ArrayList <Map>();
public BusTransfer(String start, String whither) {
this.start = start;
this.whither = whither;
try {
this.schedule = this
.executeQuery("select busLine,up,stationNo from bus_stations");
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("讀取數據庫出錯");
e.printStackTrace();
}
stationsOfLine = this.getStationsOfLine();
linesOfStation = this.getLinesOfStation();
}
private HashMap <String, ArrayList> getStationsOfLine() {
HashMap map = new HashMap();// 用于 臨時存儲從schedule中取出的HashMap.
ArrayList <Map> line = null;// 每個list存儲一個車次的相關車站信息。
String buffer = "a";// 緩存站名。
String temp = null;// 臨時存儲每次迭代取出的站名,用于與buffer站名比較。
HashMap <String, ArrayList> stationsGroupByLine = new HashMap <String, ArrayList>();// 存儲以車次分組後的車站的list
Iterator it = schedule.iterator(); // 迭代器
while (it.hasNext()) {
map = (HashMap) it.next();
temp = (String) map.get("busLine");
if (stationsGroupByLine.containsKey(temp)) {
line = stationsGroupByLine.get(temp);
buffer = (String) ((Map) line.get(0)).get("busLine");
}
if (buffer.equals(temp)) {
line.add(map);// 將同一車次的車站放入一個list中
} else {
if (line != null && !line.isEmpty()) {
stationsGroupByLine.put(buffer, line);// 將由車次分組后的車站構成的list存入一個map
}
line = new ArrayList <Map>(); // line重新引用一個新構造的list,以供存儲同一車站的車次。
line.add(map);// 將同一車次的車站放入剛剛構造的空list中
}
buffer = temp;// 緩存當前操作的車次。
}
return stationsGroupByLine;
}
private HashMap getLinesOfStation() {
HashMap map = new HashMap();// 用于 臨時存儲從schedule中取出的HashMap.
ArrayList <Map> station = null;// 每個list存儲一個經過該車站的相關車次信息。
String buffer = "a";// 緩存車次。
String temp = null;// 臨時存儲每次迭代取出的車次,用于與buffer車次比較。
HashMap <String, ArrayList> linesGroupBystation = new HashMap <String, ArrayList>();// 存儲以車站分組後的車次的list
Iterator it = schedule.iterator(); // 迭代器
while (it.hasNext()) {
map = (HashMap) it.next();
temp = (String) map.get("up");
if (linesGroupBystation.containsKey(temp)) {
// station存儲temp車次對應的站點信息
station = linesGroupBystation.get(temp);
// 從station中取出已經放入linesGroupBystation車站信息,緩存改車站的名字
// 與剛取出的Map中存儲到車站信息進行比較
buffer = (String) ((Map) station.get(0)).get("up");
}
if (buffer.equals(temp)) {
// 如果station中幾經存在該站點信息,那么,本站和station中存儲到是同一站,所以
// 將同一車次的車站放入一個list中
station.add(map);
} else {
if (station != null && !station.isEmpty()) {
// 將由車次分組后的車站構成的list存入一個map
linesGroupBystation.put(buffer, station);
}
// line重新引用一個新構造的list,以供存儲經過另一車站的所有車次
station = new ArrayList <Map>();
station.add(map);// 將同一車次的車站放入剛剛構造的空list中
}
buffer = temp;// 緩存當前操作的車次。
}
return linesGroupBystation;
}
/**
* 站點篩選規則:把符合規則的站點添分別放入
*
* @param startSta
* @param whitherSta
*/
private void getStationsInLine(String startSta, String whitherSta) {
// 獲得經過初始站點的所有公交車次
ArrayList firTrainLine = linesOfStation.get(startSta);
// 獲得經過所有目的站點的公交車次
ArrayList secTrainLine = linesOfStation.get(whitherSta);
ArrayList station;
HashMap line = null;
int transferStaNo = 0;
// String stationName = null;
String trainNo = "";
if (firTrainLine != null) {
Iterator firIt = firTrainLine.iterator();
while (firIt.hasNext()) {
// 取出一個存儲車站信息HashMap
line = (HashMap) firIt.next();
// 取出車次信息
trainNo = (String) line.get("busLine");
transferStaNo = (Integer) line.get("stationNo");
// 取出車次trainNo經過的所有站點信息
station = stationsOfLine.get(trainNo);
Iterator it = station.iterator();
while (it.hasNext()) {
Map map = (Map) it.next();// trainNo's map.
int i = (Integer) map.get("stationNo");
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -