Java 制作一种交换两个int';s在arraylist中的位置

Java 制作一种交换两个int';s在arraylist中的位置,java,arraylist,Java,Arraylist,有人告诉我为什么下面的代码片段会出现找不到符号的错误, 此swap()应被用作在给定数组列表名称时将索引位置1的内容与位置2交换 干杯 public static void swap(String swapArray, int location1, int location2) { int tempswap1 = swapArray.get(location1); int tempswap2 = swapArray.get(location2); swapArray.se

有人告诉我为什么下面的代码片段会出现找不到符号的错误, 此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)
    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);
    }
由于
swapArray
是一种
String
类型,因此
set
方法甚至
get
方法在
String
类中都不存在

可能的解决方案:

swapArray.set(location1)

swapArray.get(location1)
    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);
    }
如果我没有错,
swapArray
应该是
List
类型。请检查并作为旁注使用像Eclipse这样的
IDE
,这样可以节省大量时间

还可能有用的是:

更新:

swapArray.set(location1)

swapArray.get(location1)
    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);
    }

假设您正在将列表传递给swap方法。

假设
swapArray
是问题标题中提到的列表类型,您需要像这样交换值

swapArray.set(location1, tempswap2); // Set the location1 with value2
swapArray.set(location2, tempswap1); // Set the location2 with value1
错误是因为
swapArray.set(location1)=tempswap2。左侧是一个方法调用(
set()
),它返回一个值,并且您试图为一个值分配另一个值,这是非法的。赋值运算符的LHS上需要有一个变量


此外,这应该是/已经是实际的方法签名

public static void swap(List<Integer> swapArray, int location1, int location2)
                        ^^^^^^^^^^^^^ - This is the type of the object you're passing. You needn't give the name of that object as such.      
公共静态无效交换(列表交换阵列、内部位置1、内部位置2)
^^^^^^^^^^^^^-这是您要传递的对象的类型。您不必给出该对象的名称。


旁注:请始终记住从IDE复制/粘贴代码,而不是在此处手动键入,因为您可能会出现打字错误和语法错误。

这里有一个技巧可以使用

public static <T> void swap(List<T> list, int pos1, int pos2) {
    list.set(pos1, list.set(pos2, list.get(pos1)));
}
publicstaticvoidswap(列表、intpos1、intpos2){
list.set(pos1,list.set(pos2,list.get(pos1));
}

字符串在Java中是不可变的。你不能改变它们。 您需要创建一个替换字符的新字符串。 检查此线程:
我更喜欢彼得的答案。但是,如果您只是为了调用setter(无论如何都不存在)而将字符串放入集合中,那么您可以使用本机代码来完成这一切

请记住,如果要执行大量字符串操作,如果需要线程安全,则应使用StringBuffer;如果程序不是多线程的,则应使用StringBuilder。因为前面提到的字符串实际上是不可变的——这意味着每次更改一个字符串时,实际上就是在销毁旧对象并创建一个新对象

如果起始点(例如loc1)的偏移量为0,则此代码需要更智能一些,但本机字符串操作的一般思路是:

String x = a.substring(loc1,loc1+1);
String y = b.substring(loc2,loc2+1);
a = a.substring(0, loc1) + y + a.substring(loc1);
b = b.substring(0,loc2) + x + b.substring(loc2);

swapArray
不是一个
ArrayList
,它是一个
字符串
。在Java中,
String
s是不可变的(不能修改)。swapArray用于输入我要交换的arraylist的名称,因为有多个,如何在不使用字符串的情况下使用用户数据来实现这一点,谢谢@DavidMcDaid您的方法签名错误,更新了帖子。