Java-从文件中读取值?
所以我试图建立一个图表,找出两个对象之间的最短路径。要构建图形,我从输入文件读取名称和值:Java-从文件中读取值?,java,Java,所以我试图建立一个图表,找出两个对象之间的最短路径。要构建图形,我从输入文件读取名称和值: Location1 0 0 0 Location2 5 0 0 Location3 5 5 0 Location4 0 5 0 我的问题是如何解读这些值?以下是我为我的主要方法所做的: public static void main(String[] args) { // You can test your program with something like this.
Location1 0 0 0
Location2 5 0 0
Location3 5 5 0
Location4 0 5 0
我的问题是如何解读这些值?以下是我为我的主要方法所做的:
public static void main(String[] args) {
// You can test your program with something like this.
In in = new In( args[0] );
int T = in.readInt();
for (int t=1; t<=T; t++) {
System.out.println("Case " + t + ":") ;
Edge w = new Edge( in );
}
}
我不明白的是,我如何将这些值添加到has表中,然后始终将位置与这些坐标关联。我想用Floyd-Warshall算法找到最短路径,我可以这样做:
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
dist[i][j] = Math.min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
for(int k=0;k
我希望这些值进入一个名为dist的2d数组,但我不知道如何将这些值分配给数组 您可以使用bufferedReader和文件读取器来完成
File file = new File(replace with file location);
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
while ((line = br.readLine()) != null) {
//process line will be in the format of a String
}
br.close();
行将等于文件中的当前行。它将一直读取文件直到文件结束我建议您创建一个自定义对象,例如
位置
,然后使用哈希映射
保留链接信息
阅读完整的输入并准备HashMap
后,您可以迭代它并进行计算
请参阅以下代码段:
位置类别:
public class Location
{
private String location;
private int point1;
private int point2;
private int point3;
public Location(String location, int point1, int point2, int point3) {
this.location = location;
this.point1 = point1;
this.point2 = point2;
this.point3 = point3;
}
public void setLocation(String location) {
this.location= location;
}
public String getLocation() {
return this.location;
}
public void setPoint1(int point1) {
this.point1 = point1;
}
public int getPoint1() {
return this.point1;
}
public void setPoint2(int point2) {
this.point2 = point2;
}
public int getPoint2() {
return this.point2;
}
public void setPoint3(int point3) {
this.point3 = point3;
}
public int getPoint3() {
return this.point3;
}
}
public class Foo
{
public static void main (String[] args)
{
Foo f = new Foo();
f.run();
}
private void run() {
Map<String,Location> locationMap = new HashMap<>();
Scanner in = new Scanner(System.in);
String line = null;
while(in.hasNext()) {
line = in.nextLine();
String[] split = line.split(" ");
locationMap.put(split[0],new Location(split[0],Integer.parseInt(split[1]),
Integer.parseInt(split[2]),Integer.parseInt(split[3])));
}
/* Iterate locationMap and do your calculations */
}
}
主类:
public class Location
{
private String location;
private int point1;
private int point2;
private int point3;
public Location(String location, int point1, int point2, int point3) {
this.location = location;
this.point1 = point1;
this.point2 = point2;
this.point3 = point3;
}
public void setLocation(String location) {
this.location= location;
}
public String getLocation() {
return this.location;
}
public void setPoint1(int point1) {
this.point1 = point1;
}
public int getPoint1() {
return this.point1;
}
public void setPoint2(int point2) {
this.point2 = point2;
}
public int getPoint2() {
return this.point2;
}
public void setPoint3(int point3) {
this.point3 = point3;
}
public int getPoint3() {
return this.point3;
}
}
public class Foo
{
public static void main (String[] args)
{
Foo f = new Foo();
f.run();
}
private void run() {
Map<String,Location> locationMap = new HashMap<>();
Scanner in = new Scanner(System.in);
String line = null;
while(in.hasNext()) {
line = in.nextLine();
String[] split = line.split(" ");
locationMap.put(split[0],new Location(split[0],Integer.parseInt(split[1]),
Integer.parseInt(split[2]),Integer.parseInt(split[3])));
}
/* Iterate locationMap and do your calculations */
}
}
公共类Foo
{
公共静态void main(字符串[]args)
{
Foo f=新的Foo();
f、 run();
}
私家车{
Map locationMap=newhashmap();
扫描仪输入=新扫描仪(系统输入);
字符串行=null;
while(在.hasNext()中){
line=in.nextLine();
String[]split=line.split(“”);
locationMap.put(拆分[0],新位置(拆分[0],Integer.parseInt(拆分[1]),
Integer.parseInt(split[2])、Integer.parseInt(split[3]);
}
/*迭代locationMap并进行计算*/
}
}
在这里,我使用了System.in
从控制台读取输入。您可以替换它以从任何所需文件获取输入