第四周作业
2014-04-03 21:12
281 查看
import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; public class GraphRepresentation { int v; int e; int[][] matrix; static String srcpath="F:\\tinyG.txt"; static String dispath="F:\\tinyG_matrix.txt"; public GraphRepresentation(){ } public GraphRepresentation(int var,int edg){ this.v=var; this.e=edg; this.matrix=new int[v][v]; } void readFileByChars(String fileName) { try{ Scanner scn=new Scanner(new File(fileName)); v=scn.nextInt();//读取文件第一个数字 e=scn.nextInt();//读取文件第二个数 GraphRepresentation ect=new GraphRepresentation(v,e); for(int i=0;i<e;i++){ int a1=scn.nextInt(); int a2=scn.nextInt(); matrix=ect.getMatrix(a1, a2); } }catch(IOException e){ e.printStackTrace(); } } void writeFileByChars(String fileName){ try{ PrintWriter pw=new PrintWriter(new File(fileName)); for(int i=0;i<v;i++){ for(int j=0;j<v;j++){ pw.print(matrix[i][j]+"\t"); } pw.println(); pw.flush(); //printwriter (file(fileName)) 用这种方式 需要刷新。 } System.out.println("矩阵已存入文件。"); }catch(IOException e){ e.printStackTrace(); } } int [][] getMatrix(int v1,int v2){ matrix[v1][v2]=1; matrix[v2][v1]=1; return matrix; } public static void main(String[] args){ GraphRepresentation ec=new GraphRepresentation(); ec.readFileByChars(srcpath); ec.writeFileByChars(dispath); } }
运行结果下载:http://download.csdn.net/download/u010917502/7143409
相关文章推荐
- c#操作word表格
- 树状数组
- 最近开始找实习,连续被鄙视了三次
- Ubuntu下配置SVN服务器
- Find them, Catch them 并查集
- html5
- 在Linux平台下部署Java web环境和发布Java web程序
- Java内存溢出的详细解决方案
- 第5章: jQuery对表单、表格的操作及更多应用---目录
- 对于IP地址检测问题的考虑!
- js判断input输入框为空时遇到的问题 弹窗后,光标没有定位到输入框,而是直接执行我的处理页面程序
- 【DFS&二叉树】Binary Tree Maximum Path Sum
- 2014开源技术大会(读书汇)
- 冒烟测试、α测试、Beta测试、性能测试
- 冒烟测试、α测试、Beta测试、性能测试
- 日积月累:ListView的属性详解和探究
- ( ubuntu )linux快捷键
- jvm的参数含义及设置
- NSOpenPanel 设置允许打开的文件类型
- android的事件处理1(监听 回调 handle)