gpt4 book ai didi

java - 数组列表 "get"问题

转载 作者:行者123 更新时间:2023-12-01 11:34:53 24 4
gpt4 key购买 nike

在本类(class)中,我获取存储在数据库中的公交车站列表的名称,我想将它们显示在组合框中。但我收到以下错误:

ArrayList 类型中的 get(int) 方法不适用于参数 (String)

有什么建议吗?

// gets the stop names from the database
int[] routeArray = BusStopInfo.getRoutes();
int[] stopIdArray;
ArrayList<String>stopName = new ArrayList<String>();
for(int i = 0; i < routeArray.length; i++) {
stopIdArray = BusStopInfo.getBusStops(i);
for (int j = 0; j < stopIdArray.length; j++) {
stopName.add(BusStopInfo.getFullName(stopIdArray[j]));
}//for
} // for
String[] nameArray = new String[stopName.size()];
nameArray = stopName.toArray(nameArray);
for (int k = 0; k < stopIdArray.length; k++){
stopListDeparture = stopName.get(nameArray[k]);
}

stopListDeparture.setSelectedIndex(0);
setLayout(new BorderLayout());
add(stopListDeparture, BorderLayout.NORTH);

最佳答案

我将提出与其他答案不同的建议。考虑这些行:

nameArray = stopName.toArray(nameArray);
for (int k = 0; k < stopIdArray.length; k++){
stopListDeparture = stopName.get(nameArray[k]);
}

您正在将列表 stopName 转换为 String 数组,然后迭代该数组并获取该数组中...对应于其自身的项?你确定这是你的意思吗?

而不是 stopName.get(nameArray[k]) 我想也许你的意思是 stopName.get(stopIdArray[k])。与 nameArray 不同,stopIdArray 实际上是一个 int 数组,因此可以很好地编译和运行。

这里的赠品是,您正在从 0 迭代到 stopIdArray 的长度,而是从不同的数组 (nameArray) 获取元素。有时这就是您想要做的,但通常这是一种代码味道。

关于java - 数组列表 "get"问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30105888/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com