为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

喵喵时光机 2023-10-19 21:32:58
我定义了 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 条件设置的每个值
查看完整描述

1 回答

?
一只甜甜圈

TA贡献1836条经验 获得超5个赞

在所有方法中,您必须传递字符串,您必须传递字符串数组。对于所有数组都是这样:


airport[0] = "cdg";

airport[1] = "lhr";

city[0] = "paris";

city[1] = "london";

然后将其传递给以下方法:


in_city(airport,city);

在 for 循环中,您必须仅提及数组名称,例如 airport而不是 airport[]并为 i 添加增量。


查看完整回答
反对 回复 2023-10-19
  • 1 回答
  • 0 关注
  • 92 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信