如何通过使用chartAt()和数组技术将两个字符串合并为一个

时间:2018-09-13 11:24:37

标签: java algorithm sorting

如标题中所述,for循环的每一行都出现错误,提示(预期变量),这是我的代码

 String s = "ABC";
    String t = "DEFGH";
    String merge = "";
    // merge should looks like "ADBECFGH"
    int i = 0;
    for (; i < s.length(); i=i+2) {
        merge.charAt(i) = s.charAt(i/2);
        merge.charAt(i+1) = t.charAt(i/2);
    }
    for (; i < t.length()+s.length() ; i++) {
        merge.charAt(i) = t.charAt(i-s.length());
    }

我试图对数组使用相同的技术,我认为它非常有效。

6 个答案:

答案 0 :(得分:1)

如果您喜欢从第一个字符串中取出一个字母,然后再从另一个字符串中取出,请尝试以下操作:

WITH sampledata1
     AS (SELECT '1' ctrl_id, '23' list_val1, 'Textfield' list_val2 FROM DUAL),
     sampledata2
     AS (SELECT '2' ctrl_id, 'Textfield' list_val1, '45' list_val2 FROM DUAL),
     sampledata3
     AS (SELECT * FROM sampledata1
         UNION
         SELECT * FROM sampledata2),
     sampledata4
     AS (SELECT LENGTH (TRIM (TRANSLATE (ctrl_id, ' +-.0123456789', ' ')))
                   ctrl_id,
                LENGTH (TRIM (TRANSLATE (list_val1, ' +-.0123456789', ' ')))
                   list_val1,
                LENGTH (TRIM (TRANSLATE (list_val2, ' +-.0123456789', ' ')))
                   list_val2
           FROM sampledata3 qd                   --           group by ctrl_id
                              )
(  SELECT CASE WHEN ctrl_id IS NULL THEN AVG (ctrl_id) ELSE 0 END ctrl_id,
          CASE WHEN list_val1 IS NULL THEN AVG (list_val1) ELSE 0 END list_val1,
          CASE WHEN list_val2 IS NULL THEN AVG (list_val2) ELSE 0 END list_val2
     FROM sampledata4
 GROUP BY ctrl_id, list_val1, list_val2)

这种情况使您可以迭代直到for (int i = 0; i < s.length() || i < t.length(); i++) { if (i < s.length()) { merge += String.valueOf(s.charAt(i)); } if (i < t.length()) { merge += String.valueOf(t.charAt(i)); } } 完成更长的时间

String

答案 1 :(得分:0)

charAt(int index)方法返回指定索引处的字符(它是GETTER而不是SETTER)。您不能将其用作 merge.charAt(i) = s.charAt(i/2)

执行此操作的最简单方法之一是使用字符串操作(例如,如下面的示例所示的conatination)

 s="abc";
 t="def";
 System.out.print(s.concat(t));

答案 2 :(得分:0)

另一个,因为要在循环中处理Strings,所以最好使用StringBuilder而不是String。

    String s = "ABC";
    String t = "DEFGH";
    StringBuilder merge = new StringBuilder();
    for (int i = 0; i < s.length() || i < t.length(); i++) {
        if (i < s.length()) {
            merge.append(s.charAt(i));
        }
        if (i < t.length()) {
            merge.append(t.charAt(i));
        }
    }

    System.out.println(merge.toString());

答案 3 :(得分:0)

    CharacterIterator a = new StringCharacterIterator("haaaaallo");
    CharacterIterator b = new StringCharacterIterator("12345");
    StringBuilder output = new StringBuilder();
    if(a.getEndIndex() < b.getEndIndex()) { //true -> swap a and b
        CharacterIterator holder = a;
        a = b;
        b = holder;
    }
    while (a.current() != CharacterIterator.DONE) {
        output.append(a.current());
        while (b.current() != CharacterIterator.DONE) {
            output.append(b.current());
            break;
        }
        a.next();
        b.next();
    }
    System.out.println(output.toString()); //h1a2a3a4a5allo

显示“手动方式”。使用的字符串可以是任意大小。

答案 4 :(得分:0)

尝试一下:连接两个没有内置方法和+运算符的字符串。

public static void main(String[] args) {
    String s = "ABC";
    String s1 = "DEF";
    String merge = "";
    char[]ch = new char[120];

    for(int i=0;i<s.length();i++) {
        ch[i] = s.charAt(i);
    }

    for(int i = 0;i<s1.length();i++) {
        ch[s.length()+i] = s1.charAt(i);
    }

    System.out.println(ch);
}

答案 5 :(得分:-1)

我会喜欢@Bartek的答案,它简单易记。 实现这一目标的复杂方法是​​

String a = "abcdefgh";
String b = "ijklmnopqrst";
int i = 0;
StringBuilder merge = new StringBuilder();
for (; i < a.length() && i < b.length(); i++) {
    merge.append(a.charAt(i)).append(b.charAt(i));
}
if (i < a.length())
    merge.append(a.substring(i));
if (i < b.length())
    merge.append(b.substring(i));

仅避免在for循环中使用if条件(仅在大字符串,长度截然不同的字符串中才是最佳条件)