第四周作业 1(给定图数据文件(tinyG.txt),计算得到图的邻接矩阵,并把邻接矩阵保存到文件)

import java.util.ArrayList;
import java.util.List;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Reader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.FileWriter;
public class GraphRepresentation {
	public static void main(String[] orgs){
		Test3 test3=new Test3();
		test3.test("D:\\tinyG.txt");
	}
}
class Test3{
	Test3(){}
	void test(String PathName){
		
		FileReader file1 = null;
		try {
			file1 = new FileReader(PathName);
		} catch (FileNotFoundException e) {
			
			e.printStackTrace();
		}
		BufferedReader br=new BufferedReader(file1);
	
		String s1=null;
		int line=0;
		String [][]strs=new String[13][2];
		int count=0;
		try {
			while((s1=br.readLine())!=null){
				
				line++;
				if(line==1||line==2||line==3)continue;
				
				String str=s1.trim();
				if(!str.isEmpty()){
					strs[count]=str.split(" ");
					count++;	
				}
		}
		   } catch (IOException e) {
			e.printStackTrace();
		  }
		int[][] s5=new int [13][13];
		for(int i=0;i<12;i++){
			for(int j=0;j<12;j++){
				s5[i][j]=0;
			}
		}
		for(int i=0;i<13;i++){
			for(int j=0;j<2;j++){
				s5[Integer.parseInt(strs[i][0])][Integer.parseInt(strs[i][1])]=1;
				s5[Integer.parseInt(strs[i][1])][Integer.parseInt(strs[i][0])]=1;
				
				
			}
		}
		
		  File file = new File("D:\\array.txt");  //存放数组数据的文件
		  
		  FileWriter out = null;
		try {
			out = new FileWriter(file);
		} catch (IOException e) {
			
			e.printStackTrace();
		}  //文件写入流
		 
		  //将数组中的数据写入到文件中。每行各数据之间TAB间隔
		  for(int i=0;i<12;i++){
		   for(int j=0;j<12;j++){
		    try {
				out.write(s5[i][j]+" ");
			} catch (IOException e) {
				
				e.printStackTrace();
			}
		   }
		   try {
			out.write("rn");
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		  }
		  try {
			out.close();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		
	}
		
	}
	

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值