小白打比賽-隨記(一)

隨着華爲2019年軟挑的開啓,我正式開啓了菜鳥打比賽模式。java

2019年華爲的賽題我的感受出的特別好,好到我一點頭緒賭沒有。不過這個賽題特別符合如今智能駕駛這個主題。不過華爲這個確實站的角度相對高點,之前看到的大部分相關信息主要是討論智能駕駛車輛採用圖像傳感等模式去規避其它物體,而華爲軟挑確實去調度整個城市的汽車。可想而知,這和阿里近些年推行的智慧城市遙相輝映。我想將來的智慧城市可能不單單是體如今衣、食,也應該體如今人們的住(智能家居)、行(智能駕駛)方面。正則表達式

說實話,我第一次看見這個賽題介紹的開頭時,竟然以爲不是很難。不就是先讀取三個TXT文件,而後把TXT寫入到數組(數組鏈表、或者MAP)中,而後在建立根據roadList(路數組鏈表)建立圖,最後把carList(車數組鏈表)中的節點car利用最短路徑算法去搜索圖不久能夠了嗎,看來大獎非我莫屬——手動滑稽,勿噴。算法

直到我繼續看到下面的介紹時,才發我真符合那就經典名句——「Too young too simple to naive」。若是不是實驗室成員執意要參賽,我都要放棄了。儘管到如今也沒有什麼好的想法,不過也算是鞏固了下本身所學習的知識。數組

好吧,廢話不說了,直接先上代碼(嘿嘿-其實只是讀取文件的代碼)。學習

 

  1 package huawei;
  2 
  3 import java.io.BufferedReader;
  4 
  5 import java.io.FileReader;
  6 import java.io.IOException;
  7 
  8 import java.util.ArrayList;
  9 import java.util.List;
 10 
 11 import java.util.regex.Matcher;
 12 import java.util.regex.Pattern;
 13 
 14 
 15 /**
 16  * 逐行讀取txt文件並寫入到list實體類中
 17  * 
 18  * @author Bob
 19  *
 20  */
 21 public class car_input{
 22     
 23 
 24     public static List<car_Node> read_Car() throws IOException {
 25 
 26         String pathname = "C:\\Users\\11940\\Desktop\\1-map-training-2\\car.txt";
 27         FileReader reader = new FileReader(pathname);
 28         BufferedReader br = new BufferedReader(reader);
 29         String carLine = null;
 30         int carNum = 0;
 31         List<car_Node> carList = new ArrayList<car_Node>();
 32         while ((carLine = br.readLine()) != null) {
 33             if (!carLine.contains("#")) {
 34                 //System.out.println(carLine);
 35                 // System.out.println("進來了");
 36                 
 37                 
 38                 Pattern car_p = Pattern.compile("[0-9A-Z]+");
 39                 Matcher car_m = car_p.matcher(carLine);
 40                 String[] strCarNode = new String[5];
 41                 int[] intCarNode=new int[5];
 42                 // int index = 0;
 43 //                while (m.find()) {
 44 //                    //System.out.println(m.group());
 45 //                    strCarNode[index++] = m.group();
 46 //                    intCarNode [index++] = Integer.parseInt(strCarNode[index++]);
 47 //                }
 48                 for (int i = 0; car_m.find()&&i < strCarNode.length; i++) {
 49                     strCarNode[i] = car_m.group();
 50                     intCarNode [i] = Integer.parseInt(strCarNode[i]);
 51                     //System.out.println(intCarNode[i]);
 52                 }
 53                 car_Node car = new car_Node(intCarNode[0],intCarNode[1],intCarNode[2],intCarNode[3],intCarNode[4]);
 54                 System.out.println(car);
 55                 carList.add(car);
 56                 carNum++;
 57             }
 58 
 59         }
 60         
 61         //System.out.println("讀取總條數:" + count + "||讀取的list的長度" + list.size());
 62         
 63     //    System.out.println(carList);
 64 
 65         
 66 //        for (int i = 0; i < 1000; i++) {
 67 //            System.out.print(carList.get(i).id);
 68 //            System.out.print(carList.get(i).from);
 69 //            System.out.print(carList.get(i).to);
 70 //            System.out.print(carList.get(i).speed);
 71 //            System.out.println(carList.get(i).planTime);
 72 //            
 73 //            
 74 //        }
 75 
 76         return carList;
 77 
 78     }
 79 //    public static void main(String [] args) {
 80 //        try {
 81 //            read_Car();
 82 //        } catch (IOException e) {
 83 //            // TODO Auto-generated catch block
 84 //            e.printStackTrace();
 85 //        }
 86 //    }
 87 
 88     public static class car_Node {
 89         private int id;
 90         private int from;
 91         private int to;
 92         private int speed;
 93         private int planTime;
 94         
 95         
 96         public car_Node(int id, int from, int to, int speed, int planTime) {
 97             this.id = id;
 98             this.from = from;
 99             this.to = to;
100             this.speed = speed;
101             this.planTime = planTime;
102             
103         }
104 
105         public int getId() {
106             return id;
107         }
108 
109         public void setId(int id) {
110             this.id = id;
111         }
112 
113         public int getFrom() {
114             return from;
115         }
116 
117         public void setFrom(int from) {
118             this.from = from;
119         }
120 
121         public int getTo() {
122             return to;
123         }
124 
125         public void setTo(int to) {
126             this.to = to;
127         }
128 
129         public int getSpeed() {
130             return speed;
131         }
132 
133         public void setSpeed(int speed) {
134             this.speed = speed;
135         }
136 
137         public int getPlanTime() {
138             return planTime;
139         }
140 
141         public void setPlanTime(int planTime) {
142             this.planTime = planTime;
143         }
144 
145         public String toString() {
146 
147             return "car_Node [id=" + id + ", from=" + from+", to=" + to + ", speed=" + speed+ ",planTime=" + planTime
148                     +  "]";
149         }
150 
151     }}

 

1) 輸入輸出流this

String pathname = "C:\\Users\\11940\\Desktop\\1-map-training-2\\car.txt";編碼

FileReader reader = new FileReader(pathname);spa

BufferedReader br = new BufferedReader(reader);code

這裏我主要是建立一個FileReader對象將文件讀取出來。對象

其實讀取字符有兩種方式,除了FileReader類之外,還有FileInputStream類。那麼它們之間有什麼聯繫(區別)呢?

FileReader類的構造方法假定默認字符編碼和默認字節緩衝區大小都是適當的。FileReader 用於讀取字符流。要讀取原始字節流,請考慮使用 FileInputStream 

FileInputStream 從文件系統中的某個文件中得到輸入字節。哪些文件可用取決於主機環境。FileInputStream 用於讀取諸如圖像數據之類的原始字節流。要讀取字符流,請考慮使用 FileReader

對比文檔咱們能夠得出結論:FileReader用於字符流, FileInputStream用於字節流。這兩種方法其實差很少,都是從文件中寫入數據或讀出數據,區別就是字節流是以字節爲單位進行操做的,而字符流是以字符爲單位進行操做的一個英文字母佔一個字節,一箇中文漢字佔兩個字節,而一個英文字母與一箇中文漢字咱們都稱之爲一個字符)。

2)正則表達式

我把每一行讀取出來後,發現字符流是(10005, 98, 1, 6, 44)的,須要把車賦予各個屬性的話就必須把這些數字分別讀取出來。

因而我採用正則表達式,如下是摘自API

經過調用模式的 matcher 方法從模式建立匹配器。建立匹配器後,可使用它執行三種不一樣的匹配操做:

matches 方法嘗試將整個輸入序列與該模式匹配。

lookingAt 嘗試將輸入序列從頭開始與該模式匹配。

find 方法掃描輸入序列以查找與該模式匹配的下一個子序列。

每一個方法都返回一個表示成功或失敗的布爾值。經過查詢匹配器的狀態能夠獲取關於成功匹配的更多信息。

https://baike.baidu.com/item/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/1700215?fr=aladdin

相關文章
相關標籤/搜索