交换 ArrayList 中的元素

发布于 2024-12-28 06:09:39 字数 2111 浏览 0 评论 0原文

我想编写一个方法,它接受一个字符串并交换其中的每对字符,然后将它们连接成一个新的字符串。请让我知道如何修复此代码(或编写一个更好的新代码):

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

嗨,这不是作业,而是我正在做书上的练习。无论如何,使用乔恩提供的代码设法自己工作 - 它可能不太优雅,但也使用动态调整大小来完成工作:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沧桑㈠ 2025-01-04 06:09:39

我还没有尝试详细了解您的代码是如何工作的,但对我来说它看起来不必要地复杂。鉴于您不需要动态调整大小,您可以使用数组更轻松地做到这一点:

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

请注意,虽然这适用于“简单”字符(其中数组的每个元素独立于其余元素),但它不会尝试考虑任何形式的“复合”字符,例如由两个 UTF-16 代码单元(代理对)形成的字符或组合字符,例如“e + 急性重音符号”。进行这种上下文感知的交换需要付出更多的努力。

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.

遗忘曾经 2025-01-04 06:09:39

这看起来像是家庭作业,所以我将把我的答案限制在一些提示上。

  1. 我会将结果累积在 StringBuilder 中(在下文中称为 sb)。
  2. 我将有一个循环(for i = 0; i < w.length(); i += 2)。在这个循环中我会做两件事:
    • 如果i + 1在字符串的范围内,我会将第i + 1个字符附加到sb
    • 将第 i 个字符附加到 sb
  3. 最后,调用 sb.toString()。

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().
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文