⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 公交换乘算法.txt

📁 java开发中经常用到的代码
💻 TXT
📖 第 1 页 / 共 2 页
字号:
                                        // 筛选站点规则:如果该站点距离初始站点距离比出发站点的距离初始站点的距离大,那么就把该站点存储到 
                                        // firLineStaList,反之就做反车了,所以那些站点不必加入firLineStaList中 
                                        if (i > transferStaNo) { 
                                                // 
                                                firLineStaList.add(map); 
                                        } 
                                } 
                        } 
                } 

                if (secTrainLine != null) { 
                        Iterator secIt = secTrainLine.iterator(); 
                        while (secIt.hasNext()) { 
                                line = (HashMap) secIt.next(); 
                                trainNo = (String) line.get("busLine"); 
                                transferStaNo = (Integer) line.get("stationNo"); 

                                station = stationsOfLine.get(trainNo); 
                                Iterator it = station.iterator(); 
                                while (it.hasNext()) { 

                                        Map map = (Map) it.next(); 
                                        int i = (Integer) map.get("stationNo"); 

                                        if (i < transferStaNo) { 

                                                secLineStaList.add(map); 
                                        } 
                                } 
                        } 
                } 
        } 

        /** 
        * 
        * create date:2008-5-19 author:Administrator 
        * 
        * @return 
        */ 
        private ArrayList <Map> checkCrossLine() { 

                ArrayList <Map> crossLineList = new ArrayList <Map>();// 相交线路的集合,即是所有的换乘方法的集合 
                ArrayList <Map> lsStart = firLineStaList;// 经过起点站的所有车次的经停站站信息。 
                ArrayList <Map> lsEnd = secLineStaList;// 经过目的站的所有车次的经停站站信息。 

                if (lsStart != null && !lsStart.isEmpty() && lsEnd != null 
                                && !lsEnd.isEmpty()) { 

                        for (Map <String, String> mapStart : lsStart) { 
                                for (Map <String, String> mapEnd : lsEnd) { 
                                        if (IsInTheSameCity(mapStart.get("up"), mapEnd.get("up"))) { 

                                                // 将相交线路信息存入crossLine,存储某一个具体的换乘方法 
                                                Map <String, String> crossLine = new HashMap <String, String>( 
                                                                4, 0.8f); 

                                                // 把第一次要做到车次放如crossLine 
                                                crossLine.put("firstLine", mapStart.get("busLine")); 
                                                // 把要换乘的车次放入到crossLine 
                                                crossLine.put("secondLine", mapEnd.get("busLine")); 
                                                // 把中转站点放入到crossLine 
                                                crossLine.put("transferSta", mapEnd.get("up")); 

                                                // crossLine.put("transferSta",(String)startInf.get("up")); 
                                                // 将包含相交线路信息的HashMap存入List 
                                                // 也即是把具体某个换乘方法放入crossLineList 
                                                crossLineList.add(crossLine); 
                                        } else { 
                                                continue; 
                                        } 
                                } 
                        } 

                } else { 
                        crossLineList = null; 
                } 

                return crossLineList; 
        } 

        private boolean IsInTheSameCity(String station1, String station2) { 

                if (station1.contains(station2) | | station2.contains(station1)) { 
                        // System.out.println(station1+"#########"+station2); 
                        return true; 
                } else { 
                        return false; 
                } 

        } 

        public ArrayList <Map> getSchemaOfTransfer() { 
                this.getStationsInLine(this.start, this.whither); 
                return this.checkCrossLine(); 
        } 

        public static void main(String[] args) { 
                BusTransfer tb = new BusTransfer("前门", "天安门西"); 
                // tb.getSchemaOfTransfer(); 
                for (Map map : tb.getSchemaOfTransfer()) { 
                        System.out.println(map); 
                        System.out.println("您好,您可以先乘坐 " + map.get("firstLine") + " 到 " 
                                        + map.get("transferSta") + " 然后换乘 " + map.get("secondLine") 
                                        + " 便可到达,不要错过站吆 !"); 
                } 

                // System.out.println(tb.secLineStaList.size()); 
        } 

 private Connection getConnection() { 
                Connection con = null; 
                String url = "jdbc:mysql://127.0.0.1:3306/souwhat?autoReconnect=true&useUnicode=true&characterEncoding=GBK&amp;mysqlEncoding=GBK"; 
                String user = "root"; 
                String psWord = ""; 
                try { 
                        Class.forName("com.mysql.jdbc.Driver"); 
                } catch (ClassNotFoundException e) { 
                        // TODO Auto-generated catch block 
                        e.printStackTrace(); 
                        System.out.println("The Exception at load the Driver"); 
                } 
                try { 
                        con = DriverManager.getConnection(url, user, psWord); 
                } catch (SQLException e) { 
                        // TODO Auto-generated catch block 
                        e.printStackTrace(); 
                        System.out.println("The Exception at creat the connection"); 
                } 
                return con; 
        } 

        private void closeConnection(Connection conn) throws Exception { 
                if (conn != null) { 
                        conn.close(); 
                } 
        } 

        private List executeQuery(String sql) throws Exception { 
                // System.out.println("executeQuery(sql): " + sql); 
                List list = new ArrayList(); 
                Connection conn = null; 
                Statement stmt = null; 
                ResultSet rs = null; 
                try { 
                        conn = getConnection(); 
                        stmt = conn.createStatement(); 

                        System.out.println(sql); 

                        rs = stmt.executeQuery(sql); 

                        ResultSetMetaData rsmd = rs.getMetaData(); 
                        while (rs.next()) { 
                                Map map = new HashMap(); 
                                for (int i = 1; i <= rsmd.getColumnCount(); i++) { 
                                        // 每一行所有列存入HashMap中 
                                        map.put(rsmd.getColumnName(i), rs.getObject(i)); 
                                } 
                                // 所有行存入List中 
                                list.add(map); 
                        } 
                } catch (Exception e) { 
                        System.out.println("数据库查询出错!"); 
                        e.printStackTrace(); 
                } finally { 
                        if (rs != null) 
                                rs.close(); 
                        closeConnection(conn); 
                } 
                return list; 
        } 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -