incompatibletypeerror - 不兼容的操作数类型 String[] 到 int
问题描述
不兼容的操作数类型 String[] 到 int。我在我的项目中添加了分而治之算法,但我不知道它是否很好集成?我的项目与寻找城市 xy 坐标的最短路径有关,例如,该项目可能与 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;
}
}
}
解决方案
city[m] 是一个数组。我想你可能想要 city[m].length 这里。
推荐阅读
- spring - 为 Spring Boot 2 客户端和服务器存根使用 Open API 3 Codegen
- symfony - empty_data ChoiceType 返回 null
- c - 使用指针操作打印输入的字符串
- c# - 如何在 Dotnetzip 中删除 zip 文件
- flutter - 颤振:等待导航过渡结束
- java - 在 Spring Data JPA 的休眠中使用 xml 配置而不是 @Type 注释
- csv - 使用 filebeat 传输 csv 文件
- ansible - Installation of an agent when port is opened using ansible
- sql - change the column value as per other column and Serialize value after it in SQL
- php - How to process data from query in PHP