第四周作业
来源:互联网 发布:网络安全产品资质 编辑:程序博客网 时间:2024/05/29 01:55
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
0 0
- 第四周作业
- 第四周作业 1
- 第四周作业 4
- 第四周作业 2
- 第四周作业
- 第四周作业二
- 第四周作业三
- 第四周作业1
- 第四周作业2
- 第四周作业
- Java第四周作业
- 第四周作业:多线程
- 第四周作业
- 第四周作业
- 第四周C++作业
- 第四周作业
- 第四周作业
- 第四周作业
- 最近开始找实习,连续被鄙视了三次
- DWR3.0框架入门(3) —— ScriptSession的维护及优化
- SQL临时表
- C++ string类的隐式共享写时拷贝的实现及设计要点
- 解决apache服务器本地可以访问,同局域网内他人不能访问的问题(转)
- 第四周作业
- java中的堆栈
- 《基于ORACLE的SQL优化读书》笔记 绑定变量窥探
- -----------android QQ截图 开发--------------growBy-----------
- eclipse编辑器,怎么创建PHP和JAVA的工程项目?
- Nyoj 21 三个水杯
- mac 上安装 gdb
- Android设计模式系列(5)--备忘录模式
- 面向对象的单例模式