猿问

如何将字符串数组调用到方法中

我定义了 3 个方法,其中有 String[] 作为参数。我想从另一个函数设置这些,使用一些字符串,并打印每个字符串。


当我尝试从另一个函数(start() 函数)调用这些方法时,设置 String[] 参数的值 Eclipse 说:


类型不匹配:无法从 String 转换为 String[]


public class logistics2 {


private static String plane;

private static String truck;

private static String airport[];

private static String loc[];

private static String city[];

private static String pack[];



public static void at_obj_place(String object, String[] place) {


    object = truck;

    object = plane;

    place = loc;

    place = city;

    place = airport;


    return;

}


public static void in_pk_vehicle(String[] Pack, String vehicle) {


    Pack = pack;

    vehicle = truck;

    vehicle = plane;


    return;


}


public static void in_city(String[] place, String[] City ) {


    place = loc;

    City = city;


    return;

}






public static void start() {


    // HERE I RECEIVE THE ERROR MESSAGE

    in_city(airport = "cdg", city = "paris");

    in_city(airport = "lhr", city = "london");

    in_city(loc = "north", city = "paris");

    in_city(loc = "south",city = "paris");


    at_obj_place(plane = "plane", airport = "lhr");

    at_obj_place(truck = "truck", airport =  "cdg");

    at_obj_place(pack1 = "p1", airport = "lhr");

    at_obj_place(pack2 = "p2", airport = "lhr");



    for(int i = 0; i < airport[].length(); ) {


        System.out.println(airport + " " + city);

    }




    return;

我想根据我在 String[] 中插入的值的计数来打印使用 for 条件设置的每个值


喵喵时光机
浏览 93回答 1
1回答

一只甜甜圈

在所有方法中,您必须传递字符串,您必须传递字符串数组。对于所有数组都是这样:airport[0] = "cdg";airport[1] = "lhr";city[0] = "paris";city[1] = "london";然后将其传递给以下方法:in_city(airport,city);在 for 循环中,您必须仅提及数组名称,例如 airport而不是 airport[]并为 i 添加增量。
随时随地看视频慕课网APP

相关分类

Java
我要回答