如何在一个字符数组的一个索引中添加多个字符?

时间:2018-09-24 05:04:48

标签: java arrays string character

我当前正在尝试创建一个函数,其中我的输入是一个字符串,例如“ 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(" ");
                }
            }
}

3 个答案:

答案 0 :(得分:0)

数组是固定长度的,因此除非对数组分配足够的前期空间(否则需要通过字符串来查找所需的额外空间),否则无法对数组进行此操作。

请考虑使用StringBuilder作为输出,完成后可以将其转换为class GemStones # input variables name = "" color = "" price = 0 gemstoneNumber = 0 # output variable gemstoneNumber = 0 # processing print "How many gemstones do you want to enter? " gemstoneNumber = gets print "What is the name of the gemstone? " name = gets print "What is the color of the gemstone? " color = gets print "What is the price of the gemstone? " price = gets puts " You entered #{gemstoneNumber} The name is #{name}, the color is #{color} and price is $ #{price}" end 数组。

答案 1 :(得分:0)

如果我理解正确,则希望将字符拆分为一个字符串,以使相似的连续字符保持在一起。如果是这样,我将按照以下方式进行操作:

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"]

答案 2 :(得分:0)

您可以使用String类型的ArrayList将连续的字母String拆分后存储。该代码应该对您有用。

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,因为它不需要您在声明时固定大小。