One: Description
(1) Use Floyd to realize the calculation records and paths of each website
(2) Site acquisition and initial distance are based on external documents
(3) results are stored as external files
(4) Transfer between sites, feel the initial value is also 1
(5) Code gaze is more specific, in case of doubt or code. Please contact me, thank you
(6) Definition of two-dimensional data in Java:
A:float[][] Numthree; Defines a 2-dimensional array of type float
Numthree=new Float[5][5]; Allocate 5 rows and 5 columns to the space size
numthree[0][0]=1.1f; Visit 1 rows and 1 columns via subscript index =1.1
numthree[1][0]=1.2f; 2 rows 1 Columns =1.2
B:int[][] Numseven=new int[][]{{10,20,30},{40,50},{60}}; There's nothing to say if you don't understand, then don't learn!
C:list two-dimensional array: list<object>[][]lists=new arraylist[4][4];
List two-dimensional array that holds two-dimensional object types: list<object[][]>[][] list=new arraylist[4][4];
List:list<object[][]> list=new arraylist<object[][]> () for storing two-dimensional arrays
(7) Array traversal
A:
int arr[][] = new int[][] {{1}, {1, 2}, {1, 2, 3}}; for (int i = 0; i < arr.length; i++) { int[] arr2 = arr[i]; for (int c = 0; c < arr2.length; C + +) { System.out.print (arr2[c]); }
B:ARR[I][J] the way
C:
Java traversal ArrayList four ways to package Com.test;import java.util.arraylist;import Java.util.iterator;import java.util.List; public class Arraylistdemo {public static void Main (String args[]) { list<string> List = new Arraylist<s Tring> (); List.add ("Luojiahui"); List.add ("Luojiafeng"); Method 1 Iterator it1 = List.iterator (); while (It1.hasnext ()) { System.out.println (It1.next ()); } Method 2 for (Iterator it2 = List.iterator (); It2.hasnext ();) { System.out.println (It2.next ()); } Method 3 for (String tmp:list) { System.out.println (TMP); } Method 4 for (int i = 0;i < List.size (); i + +) { System.out.println (List.get (i)) ; }}}
Two: Complete code such as the following
Package Edu.tju.cs;import Java.io.bufferedreader;import Java.io.bufferedwriter;import java.io.File;import Java.io.fileinputstream;import Java.io.filewriter;import Java.io.ioexception;import Java.io.InputStreamReader; Import Java.util.arraylist;import java.util.hashmap;import Java.util.list;import Java.util.map;public class Metrofloyd {private final int max_size = 92;//86+1 + 5 Open array space, can be larger some private int k = 0;private int Vertex = 0;//number of sites , by reading each record to get private int[] line = new int[max_size];//and path to record the path of private int[][] path = new int[max_size][max_size];//record Path private int[][] Dist = new int[max_size][max_size];//records the private list<integer> of the distance between each site StationID = new ARRAYLIST&L T;integer> ();//Get the website number private map<integer,string> mapidname = new hashmap<integer,string> ();// The corresponding relationship of the site number and name */function: reads external files. Put the site number in the Stationid array, the corresponding relationship between the site number and name Mapidname, and vertex statistics * @param: OriginalPath read the path name of the file * @ call other functions: null * @return: null *// ////2305, Liu Can, 117.123174,39.214493public void ReadText (STring OriginalPath) {try {Vertex = 0; String regex = ",";//cut dependent symbol, \ string encoding= "GBK"; File File=new file (OriginalPath); if (File.isfile () && file.exists ()) {//infer whether the file exists inputstreamreader read = new InputStreamReader ( New FileInputStream (file), encoding);//considering the encoding format BufferedReader BufferedReader = new BufferedReader (read); Symbol String Originalline = NULL if the original row of data and data need to be changed; Originalline = Bufferedreader.readline ();//First row identifier filter out if (Originalline = = null) {Read.close (); Bufferedreader.close (); Return } stationid.add (0); Invalid value, since the site number is starting from 1 while ((Originalline = Bufferedreader.readline ()) = null) {//character String delimited string tmp[] = Originalline.split (regex); Assume that another synthetic stationid.add is met (Integer.parseint (Tmp[0])); Mapidname.put (Integer.parseint (tmp[0]), tmp[1]); vertex++; System.out.println ("Jilu:" + Vertex); }//Close Write file Read.close (); Bufferedreader.close (); } else {System.out.println ("The specified file cannot be found"); }} catch (Exception e) {System.out.println ("Readtowrite ... Error reading file contents "); E.printstacktrace (); }system.out.println ("Readtowrite ... Devide is OVER!!! ");} /* Function: Count the number of paths and paths of each website. The format is as follows. But for the sake of the effect: site name * @param: DestinationPath2 Write file path name * @ Call other functions: null * @return: NULL * Results Demo sample: * ========================== Source: Two weft path target: Tianjin Bay Plaza Distance:6path: Two---Hai-Guang temple-------------------------------- ========== *///public void writeText2 (String destinationPath2) {try {bufferedwriter bufferwriter = new BufferedWriter (NE W FileWriter (DestinationPath2)); String newLine = ""; int p,q,m; for (p=1;p<=vertex;p++) { for (q=p+1;q<=vertex;q++) {newLine = "\n==========================\nsource:" + mapidname.get (statio Nid.get (p)) + "\ntarget:" + mapidname.get (Stationid.get (q)) + "\ndistance:" + dist[p][q] + "\npath:" + mapidname.get (Stati Onid.get (p)); I=p; Root (P,Q); for (m=2;m<=k-1;m++) {newLine = NewLine +--+ mapidname.get (Stationid.get (line[m])); } NewLine + = "\n==========================\n"; Bufferwriter.write (NewLine); }} bufferwriter.close ();} catch (Exception e) {System.out.println ("Mergemindistortbyday_zheng ... Error reading file contents "); E.printstacktrace (); }}/* function: Count the number of paths and paths of each website. The format is the following: But the number of the site. This is the file format to be used later.
* @param: DestinationPath2 The path name of the write file * @ Call other functions: private void Root (int p,int q) Returns the Web site where the path was passed * @return: null *///Output Shortest path, numbered only The corresponding relationship between 2314,533 and 2314,533,6,2314,2313,529,530,531,532,533public void WriteText (String destinationpath) {try { BufferedWriter bufferwriter = new BufferedWriter (new FileWriter (DestinationPath)); String newLine = ""; int p,q,m; for (p=1;p<=vertex;p++) {for (q=1;q<=vertex;q++) {newLine = "" + stationid.get (P) + "," + St Ationid.get (q) + "," + dist[p][q] + "," + Stationid.get (p); k=2; Root (P,Q); for (m=2;m<=k-1;m++) {newLine = NewLine + "," + Stationid.get (Line[m]); } newLine + = "\ n"; Bufferwriter.write (NewLine); }} bufferwriter.close ();} catch (Exception e) {System.out.println ("Mergemindistortbyday_zheng ... Error reading file contents "); E.printstacktrace (); }}/* function: Initialize the initial distance between the various sites, that is, adjacent to 1, not adjacent to 0, interchange station to sit special treatment, and interchange between the station feel that the path is 1 * @param: null * @ Call other functions: Null * @return: null *///initprivate void init () {/*| | (Stationid.get (p) ==280 && 533-stationid.get (q) = = 1) | | (Stationid.get (p) ==280 && 791-stationid.get (q) = = 1) | | (Stationid.get (p) ==533 && 280-stationid.get (q) = = 1) | | (Stationid.get (p) ==533 && 791-stationid.get (q) = = 1) | | (Stationid.get (p) ==791 && 280-stationid.get (q) = = 1) | | (Stationid.get (p) ==791 && 533-stationid.get (q) = = 1) | | (Stationid.get (q) ==280 && 533-stationid.get (p) = = 1) | | (Stationid.get (q) ==280 && 791-stationid.get (p) = =-1) | | (Stationid.get (q) ==533 && 280-stationid.get (p) = =-1) | | (Stationid.get (q) ==533 && 791-stationid.get (p) = =-1) | | (Stationid.get (q) ==791 && 280-stationid.get (p) = =-1) | | (Stationid.get (q) ==791 && 533-stationid.get (p) = =-1) *///Initialize for (int p=1;p<=vertex;p++) {for (Int. q=1;q<= vertex;q++) {//280 533 791 Tjif (Stationid.get (P)-stationid.get (q) ==-1 | | stationid.get (P)-stationid.get (q) ==1) {Dist[ P][Q] = 1;D ist[q][p] = 1;} ELSE{DIST[P][Q] = 0;dist[q][p] = 0;} PATH[P][Q] = 0; PATH[P][Q] = 0;}} Yingkou Road interchange feel is a station, 13 78 must strictly according to the order of the documents written, their own number to dist[13][78]=1;dist[78][13]=1;//southwest corner to think is a station to dist[9][53]=1;dist[53][9]=1;// Tianjin station to think is a station dist[46][57]=1;dist[57][46]=1;dist[46][81]=1;dist[81][46]=1;dist[57][81]=1;dist[81][57]=1;} /* Function: The invocation of a recursive function that calculates the path through which the website is passed. The * @param: null * @ Call OTHER functions: null * @return: null *///recursively seek points on each path private void Root (int p,int q) {if (path[p][q]>0) { Root (P,path[p][q]); Root (PATH[P][Q],Q); }else{line[k]=q; k++; }}/* function: Core algorithm, Floyd calculate path between each website * @param: null * @ Call OTHER functions: null * @return: null *///Floyd algorithm shortest path private void Floyd () {i NT K,p,q; for (k=1;k<=vertex;k++) {for (p=1;p<=vertex;p++) {if (dist[p][k]>0) {for (q=1;q<= vertex;q++) {if (dist[k][q]>0) {if (((dist[p][q]>dist[p][k]+dist[k][q)) | | (dist[p][q]==0)) && (p!=q)) {Dist[p][q]=diST[P][K]+DIST[K][Q]; Path[p][q]=k; }}}}}}}public static void Main (string[] args) {Metrofloyd Metro Floyd = new Metrofloyd ();//2305, Liu Can, 117.123174,39.214493string originalPath = "D:\\tjdata_metro\\stationid_name_ New.csv "; String destinationpath= "D:\\tjdata_metro\\metrofloydid.csv"; String destinationpath2= "D:\\tjdata_metro\\metrofloydname.csv"; Metrofloyd.readtext (OriginalPath); Metrofloyd.init (); Metrofloyd.floyd (); Metrofloyd.writetext (DestinationPath); Metrofloyd.writetext2 ( DESTINATIONPATH2);}
Java implementation Floyd Statistic website distance of Tianjin Metro