操作数类型不兼容String[]转int。我在我的项目中加入了dividide and conquer算法,但我不知道这是否是一个好的集成?我的项目是关于寻找城市x-y坐标的最短路径,这个项目可以涉及到3种算法,例如:分而治之策略 贪婪算法 最近邻算法
package formalProject;
import java.io.*;
import java.io.FileNotFoundException;
import java.util.*;
public class Main {
public static void main (String[] args) throws FileNotFoundException{
String[][] cities = readArray("att48_xy.txt");
//printing cities 2d array
for(int i = 0; i < cities.length ; i++){
System.out.println(cities[i][0] + " " + cities[i][1]);
}
}
public static String[][] readArray(String file) throws FileNotFoundException{
//we'll count how many elements are there?
int counter = 0; //counter for calculating text's row length
Scanner sc1 = new Scanner(new File(file)); //scanner for calculating text's row length
while(sc1.hasNextLine()){ //checks for if there is any line
counter++;
sc1.nextLine();//jumps to next line
}
String[][] cities = new String[counter][2]; //creating our cities array with
//rows as "counter" and columns as 2(X, Y)
Scanner sc2 = new Scanner(new File(file)); //scanner for getting values from text
int i = 0;
while(sc2.hasNext()) {
String tempX = sc2.next();//first next will be X coordinate
String tempY = sc2.next();//second next will be Y coordinate
cities[i][0] = tempX;
cities[i][1] = tempY;
i++;
}
return cities; //returns our 2d array
}
public static int cities1 (String[][] cities){
if(cities.length==0 || cities.length==1){
return 0;
}
else{
return cities(cities,1,cities.length);
}
}
public static int cities(String[][] cities,int i,int f){
int m,result,sx,dx;
if(i>=f){
return 0;
}
else{
m=(i+f)/2;
sx=cities(cities,i,m);
dx=cities(cities,m+1,f);
result=sx+dx;
if((cities[m]==cities[m+1])&&(cities[m]==0)) //problem is here
result++;
return result;
}
}
}
cities[m]是一个数组。 我想你在这里可能需要的是 cities[m].length。