首页 > 解决方案 > 不兼容的操作数类型 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;
        }
    }
}

标签: incompatibletypeerror

解决方案


city[m] 是一个数组。我想你可能想要 city[m].length 这里。


推荐阅读