aretai
aretai

Reputation: 1641

Swapping elements in ArrayList

I want to write a method, which takes a String and swaps each pair of characters in it and then concatenates them into a new String. Please let me know how to fix this code (or write a new better one):

    static String s;

    public static void proc(String w) {     
        ArrayList k = new ArrayList();
        ArrayList m = new ArrayList();
        System.out.println(w.length()); 
        int j = 0;
        //test arraylist to check if string is written into arraylist
        for (int i = 0; i < w.length(); i++){
            k.add(w.charAt(i));         
        }
        String p = k.get(2).toString();
        System.out.println(p);  

//here starts the logic of my app
        for (int n = 0; n < w.length(); n++){
            String v = k.get(n).toString();
            if (n == 0){
                m.add(1, v);
            }
            else if (n == 1){
                m.add(0, v);
            }
            else if ((n % 2) == 0){
                m.add(n+1, v);
            }
            else {
                m.add(n, v);
            }           
        }
    }

    public static void main(String[] args){
        s = "tests";
        proc(s);        
    }

Hi this is not a homework, but am doing exercises from a book. Anyway using code provided by Jon managed to work on my own - it may be not as much elegant but is doing the job using dynamic sizing as well:

public static void proc(String w) {     

        ArrayList k = new ArrayList();
        ArrayList g = new ArrayList();
        String h = "";

        for (int i = 0; i < w.length(); i++){   
            char temp = w.charAt(i);
            k.add(i, temp);
        }
        for (int i = 0; i < w.length(); i++){
            if (i == 0){
                h = k.get(1).toString();
                g.add(h);
            }
            else if (i == 1){
                h = k.get(0).toString();
                g.add(h);
            }
            else if ((i % 2) == 0){

                h = k.get(i+1).toString();

                g.add(h);
            }
            else if ((i % 2) == 1){
                h = k.get(i-1).toString();
                g.add(h);
            }
        }
        System.out.println(g.toString());
    }
    public static void main(String[] args){
        s = "test";
        proc(s);
    }

Upvotes: 0

Views: 2551

Answers (2)

NPE
NPE

Reputation: 500933

This looks like homework, so I'll limit my answer to a couple of hints.

  1. I would accumulate the result in a StringBuilder (called sb in what follows).
  2. I would have a loop (for i = 0; i < w.length(); i += 2). In this loop I would do two things:
    • if i + 1 is within the bounds of the string, I'd append the i + 1-th character to sb;
    • append the i-th character to sb.
  3. At the end, call sb.toString().

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1504092

I haven't tried to go through exactly how your code is trying to work, but it looks unnecessarily complicated to me. Given that you don't need dynamic sizing, you can do this more easily with an array:

public static String swapPairs(String input) {
    char[] chars = input.toCharArray();
    for (int i = 0; i < chars.length - 1; i += 2) {
        char tmp = chars[i];
        chars[i] = chars[i + 1];
        chars[i + 1] = tmp;
    }
    return new String(chars);
}

Note that while this will work for "simple" characters (where each element of the array is independent of the rest), it doesn't try to take any form of "composite" characters into consideration, such as characters formed from two UTF-16 code units (surrogate pairs) or combined characters such as "e + acute accent". Doing this sort of contextually-aware swapping would take a lot more effort.

Upvotes: 4

Related Questions