-4

If I have an integer array, I can use the following efficient code to switch elements 1 and 5, and print the new array: (assume that in the main method, the integer array is {1,2,3,4,5}. So when the method below is called upon, {5,2,3,4,1} is returned)

        public static int[] SwitchEnds(int[] nums){
        nums[0]=(nums[0]+nums[4])-(nums[4]=nums[0]);
        return nums;
        }

Can I do something similar for ArrayList? I tried the following code (an exact copy of the previous code but using commands relevant to ArrayList):

       ArrayList<Integer> nums=new ArrayList<Integer>();
       for (int i=1;i<=5;i++){nums.add(i);}
       nums.get(0)=(nums.get(0)+nums.get(4))-(nums.get(4)=nums.get(0));

The error is that the left-hand side on line 3 has to be a variable. I understand this error, but I cannot figure out how to correct this.

Finally, can I also do something similar for String arrays? The code below illustrates this question:

       String[]a={"java","is","cool"};
       a[0]=(a[0]+a[2])-(a[2]=a[0]);
       return a;

(I want to obtain the result {"cool","is","java"}) Thanks!

  • https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#set(int,%20E) – luk2302 Apr 05 '17 at 19:05
  • 2
    And please tell me *why* on earth you would use that code to swap entries, why do you think it is more efficient? **Nobody** will understand the array swapping code. – luk2302 Apr 05 '17 at 19:06
  • Why not just take the difference between index 0 and index size and subtract the difference from index 0 but add it to index size? – CraigR8806 Apr 05 '17 at 19:09
  • Well let's say I have {1,2,3,4,5......,1000} and I want to get {1000,999...,5,4,3,2,1}, I can create a for-loop to run 500 times. Alternatively, I could create 500 temporary variables and replace; needless to say, this is inefficient. I am a beginner programmer, so this is all I know; please suggest another efficient method if you know one – JohnVanDenHof Apr 05 '17 at 19:10
  • If you want to swap elements, use the `Collections.swap` method. Code readability is far more important than so-called optimizations – fps Apr 05 '17 at 19:10
  • 2
    @BharadwajViswanathan: You don't need 500 variable, you need only one. – Frank Puffer Apr 05 '17 at 19:16
  • I understand that the code is difficult to read, but here it is mentioned that it will not be much slower: http://stackoverflow.com/questions/13766209/effective-swapping-of-elements-of-an-array-in-java – JohnVanDenHof Apr 05 '17 at 19:21

3 Answers3

1

you can use Collections.swap method for that

stinepike
  • 54,068
  • 14
  • 92
  • 112
0

Because they are Integers, you could mathematically swap them like so:

int diff = nums[0] - nums[nums.length-1];
nums[0] = nums[0] + diff;
nums[nums.length-1] = nums[nums.length-1] - diff;
CraigR8806
  • 1,584
  • 13
  • 21
0

Use set to modify a value of an ArrayList:

nums.set(0, (nums.get(0)+nums.get(4))-(nums.set(4,nums.get(0));

However this code is hard to understand and I doubt that this (and the additional arithmetics) is worth saving a single swap variable.

For Strings, the - operator is not defined. But you could use the substring method. But this will make it even more complicated.

Frank Puffer
  • 8,135
  • 2
  • 20
  • 45