【问题标题】:How to add multiple characters to one index in a Char Array?如何将多个字符添加到字符数组中的一个索引?
【发布时间】:2018-09-24 05:04:47
【问题描述】:

我目前正在尝试创建一个函数,其中我的输入是一个字符串,例如“AABBCCDDEE”,该函数输出一个字符串数组“AA”“BB”“CC”等等。

    public static char[] stringSplitter(final String input) {

            String[] strarray = new String[input.length()];
            if (input == null) {
                return null;
            }
            char[] chrarray = input.toCharArray();
            char[] outputarray = new char[input.length()];
            int j = 0;
            for (int i = 0; i < chrarray.length; i++) {
                char chr = chrarray[i];
                System.out.print(chr);
                outputarray[j] = chrarray[i]; //here i need to find a way to add the characters to the index at j if the preceding characters are equal
                if (i + 1 < input.length() && chrarray[i + 1] != chr) {
                    j++;
                    outputarray[j] = chrarray[i + 1];
                    System.out.println(" ");
                }
            }
}

【问题讨论】:

  • 所以你只想找到“runs”?相同的连续字符序列?
  • 是的,每次运行都应该在一个单独的索引中
  • 您意识到您不能在单个索引中存储 *multiple* 个字符,对吧?您需要使用二维字符数组、字符串或其他内容。
  • 我不知道,谢谢你的澄清。这意味着我只能使用字符串数组

标签: java arrays string character


【解决方案1】:

数组是固定长度的,因此您不能对数组执行此操作,除非您预先分配一个具有足够额外空间的数组(这需要通过字符串来找出您需要多少额外空间)。

请考虑使用StringBuilder 作为输出,完成后您可以将其转换为char 数组。

【讨论】:

  • 如何从 StringBuilder 转换为 chararray。有没有 .toCharArray 方法?
  • @airbmx - 请参阅上面链接的文档。没有toCharArray,不,但是有一个getChars 可以让你到达那里。 (或者先转换成String,然后使用StringtoCharArray。)
【解决方案2】:

如果我理解正确,您想将字符串中的字符拆分,以便相似的连续字符保持在一起。如果是这样的话,我会这样做:

public static ArrayList<String> splitString(String str) {
    ArrayList<String> output = new ArrayList<>();
    String combo = "";

    //iterates through all the characters in the input
    for(char c: str.toCharArray()) {
        //check if the current char is equal to the last added char
        if(combo.length() > 0 && c != combo.charAt(combo.length() - 1)) {
            output.add(combo);    
            combo = "";
        }

        combo += c;
    }

    output.add(combo); //adds the last character  
    return output;
}


请注意,我没有使用数组(具有固定大小)来存储输出,而是使用了具有可变大小的ArrayList。另外,请注意,它是字符串列表(存储字符串),而不是字符。这样做的原因是,如果它是一个字符列表,我将无法在同一个索引中存储多个字符。

在循环的每次迭代中,我都会检查当前字符与它是否连续。变量combo 用于在字符转到output 之前临时存储字符(在字符串中)。

现在,以清晰的方式打印结果:

public static void main(String[] args) 
{
    String input = "EEEE  BCD DdA";
    ArrayList<String> output = splitString(input);

    System.out.print("[");
    for(int i = 0; i < output.size(); i++) {
        System.out.print("\"" + output.get(i) + "\"");

        if(i != output.size()-1) 
            System.out.print(", ");
    }
    System.out.println("]");
}


运行上述代码时的输出将是:

["EEEE", " ", "B", "C", "D", " ", "D", "d", "A"]

【讨论】:

  • 谢谢你,不幸的是我对数组列表并不精通,所以在我完全理解你的代码之前,我必须深入了解它的 java 文档。非常感谢!
  • 不客气!简而言之,ArrayList 是一个大小可变的数组。它的使用与普通数组没有太大不同。我还在答案中添加了对我的代码的更多解释,检查一下,看看它是否有助于您更好地理解它。
【解决方案3】:

您可以使用String类型的ArrayList来存储拆分后的连续字母字符串。这段代码应该适合你。

import java.util.*;
public class StringSplitter{
        static ArrayList<String> splitString(String str)
        {
            ArrayList<String> result_list= new ArrayList<String>();
            int last_index;
            if(str == null)
            {
                return null;
            }
            else
            {
                while(str.length() != 0)
                {
                last_index = str.lastIndexOf(str.charAt(0));
                result_list.add(str.substring(0, last_index+1));
                str = str.substring(last_index+1);
                }
            }
            return result_list;
            }

        public static void main(String[] args)
        {
            ArrayList<String> result = splitString("AABBCCDDEEE");
            System.out.println(result);

        }
     }

我使用了 ArrayList,因为它不需要您在声明时固定大小。

【讨论】:

    猜你喜欢
    • 2014-08-22
    • 2020-07-31
    • 1970-01-01
    • 2013-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-08
    • 2017-07-14
    相关资源
    最近更新 更多