1

Anyone give me any insight as why the following snippet of code gives a cannot find symbol error, this swap() should be used as when given an array list name it will swap the contents of index location 1 with location 2

Cheers!

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;
}
Seelenvirtuose
  • 20,273
  • 6
  • 37
  • 66
Dmcdaid
  • 11
  • 2
  • `swapArray` is not an `ArrayList`, it is a `String`. In Java, `String`s are immutable (cannot be modified). – robbie_c Oct 15 '13 at 12:07

5 Answers5

1

Cause for error:

swapArray.set(location1)

swapArray.get(location1)

Since swapArray is a String type, there in no set method and even get method in String class.

Possible resolution:

If I'm not wrong, swapArray should be List type. Please check and as a side note use an IDE like Eclipse which saves you lot of time.

Might useful further :

Update:

    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);
    }

Assuming you are passing a list to swap method.

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • The swapArray is used to enter the name of arraylist which i would like to swap, as there is multiple, how would use the users data with out using a string to achieve this, Thanks! – Dmcdaid Oct 15 '13 at 12:12
  • @DavidMcDaid Your method signature is wrong, updated the post. – Suresh Atta Oct 15 '13 at 12:17
1

Assuming the swapArray is of type list as mentioned in your question title, you need to swap the values like this

swapArray.set(location1, tempswap2); // Set the location1 with value2
swapArray.set(location2, tempswap1); // Set the location2 with value1

The error is because of swapArray.set(location1)=tempswap2;. The left hand side is a method call(set()) which returns a value and you are trying to assign another value to a value, which is illegal. You need to have a variable on the LHS of the assignment operator.


Also, this should be/have been the actual method signature

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.      

Side Note: Always remember to copy/paste the code from the IDE, instead of manually typing it here, as you tend to make typos and syntax errors.

Rahul
  • 44,383
  • 11
  • 84
  • 103
0

There is a trick you can use here

public static <T> void swap(List<T> list, int pos1, int pos2) {
    list.set(pos1, list.set(pos2, list.get(pos1)));
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

String are immutable in Java. You can't change them. You need to create a new string with the character replaced. Check this thread: Replace a character at a specific index in a string?

Community
  • 1
  • 1
drop.in.ocean
  • 298
  • 2
  • 9
0

I like Peter's answer better. But if you were just putting your Strings in a collection for purposes of calling setters (that don't exist anyway) you can do it all with native code.

Keep in mind that if you're doing a lot of String manipulation you should use a StringBuffer if you need thread safety or a StringBuilder if your program is not multi-threaded. Because as mentioned Strings are actually immutable - meaning every time you change one you are actually destroying the old object and creating a new one.

This code would need to be a little smarter if the starting point (e.g. loc1) could be offset 0, but the general idea for native String manipulation is:

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);
Terry
  • 911
  • 10
  • 26