gpt4 book ai didi

java - 创建一个在数组列表中交换两个整数位置的方法

转载 作者:行者123 更新时间:2023-11-30 04:12:00 25 4
gpt4 key购买 nike

任何人都可以告诉我为什么以下代码片段给出找不到符号错误, 这个 swap() 应该被使用,因为当给定一个数组列表名称时,它会将索引位置 1 的内容与位置 2 交换

干杯!

public static void swap(String swapArray, int location1, int location2) {
int tempswap1 = swapArray.get(location1);
int tempswap2 = swapArray.get(location2);
swapArray.set(location1)=tempswap2;
swapArray.set(location2)=tempswap1;
}

最佳答案

错误原因:

swapArray.set(location1)

swapArray.get(location1)

由于swapArrayString类型,因此中没有set方法,甚至没有get方法>String 类。

可能的解决方案:

如果我没记错的话,swapArray应该是List类型。请检查并作为旁注使用像 Eclipse 这样的 IDE,它可以节省您大量时间。

可能进一步有用:

更新:

    public static void swap(List swapArray, int location1, int location2) {
-------------------------------^
int tempswap1 = swapArray.get(location1);
int tempswap2 = swapArray.get(location2);
swapArray.set(location1,tempswap2);
swapArray.set(location2,tempswap1);
}

假设您将一个列表传递给交换方法。

关于java - 创建一个在数组列表中交换两个整数位置的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19380750/

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