简体   繁体   English

将char附加到char数组java

[英]appending char to char array java

I'm trying to write a method that takes in and a returns a character array. 我正在尝试编写一个接受并返回一个字符数组的方法。 I want to replace every space ' ' with 'h''i'. 我想将每个空格''替换为'h'i'。 I can't use the string class or any other helper classes. 我不能使用字符串类或任何其他帮助程序类。 I'm getting stuck when at "total+= ch;" 我在“ total + = ch;”时陷入困境 since they are two different types. 因为它们是两种不同的类型。
What i want 我想要的是

char[] characterStr = {'w','h','a','t','s',' ','u','p'}
System.out.println(characterStr);
System.out.println(Space(characterStr));

whats up
whatshiup

my code so far: 到目前为止我的代码:

public static char[] Space(char[] input){
    char[] total;

    for(char ch: input){
        if(ch==' '){
            ch = 'h'+'i';
            total += ch;
        }
        else{
            total += ch;
        }

    return total;

Your fundamental problem seems to be that you don't seem to understand how arrays work in Java. 您的根本问题似乎是您似乎不了解数组在Java中的工作方式。

In Java, arrays have a fixed length , and each location can hold only one of the declared data type. 在Java中,数组具有固定的length ,并且每个位置只能容纳一种声明的数据类型。 So for instance, if you have this char[] : [w, h, a, t, s, , u, p] , its length will permanently be fixed at 8 . 因此,例如,如果您具有此char[][w, h, a, t, s, , u, p] ,则其长度将永久固定为8

You can replace the character in a given location, but if you do that more than once, you will just overwrite the value. 您可以在给定位置替换字符,但是如果多次执行替换操作,则只会覆盖该值。

    char[] characterStr = {'w','h','a','t','s',' ','u','p'};
    characterStr[5] = 'h'; // [w, h, a, t, s, h, u, p]
    characterStr[5] = 'i'; // [w, h, a, t, s, i, u, p]

Also, you cannot use += to try to append a value: 另外,您不能使用+=尝试附加值:

    char[] characterStr = {'w','h','a','t','s',' ','u','p'};
    characterStr += 'h'; // compile error! operator += is undefined for these arguments

You have to assign a value to a given location, using the arrayname[index] = value; 您必须使用arrayname[index] = value;将值分配给给定位置arrayname[index] = value; syntax. 句法。

What you need to do (as mentioned in the comments), is first calculate the size of the array you are trying to create, by counting the number of spaces in the original array. 您需要做的(如注释中所述)首先是通过计算原始数组中的空格数来计算您要创建的数组的大小。 You are already looping through and looking for spaces, so you just need to replace the code where you try to modify the array with a count, and create the output array based on that size: 您已经在循环中并在寻找空格,因此您只需要在试图用一个计数修改数组的地方替换代码,然后根据该大小创建输出数组即可:

    int size = input.length;
    for (char ch: input) {
        if (ch == ' ') {
            size += 1;
        }
    }
    char[] total = new char[size];

Also note that I didn't just declare total as a variable of type char[] , but I used the new keyword to actually create it. 还要注意,我不仅将total声明为char[]类型的变量,而且还使用new关键字实际创建了它。 Like other objects, arrays in Java need to be explicitly created, or you will end up with a NullPointerException when you try to use it. 像其他对象一样,需要显式创建Java中的数组,否则当您尝试使用它时,您将最终遇到NullPointerException

Once you have created your new array, you can loop through your original input array and copy the non-space characters over (or 'h' and 'i' when you see a space). 一旦创建了新数组,就可以遍历原始输入数组并复制非空格字符(当看到空格时可以复制'h''i' )。

You won't want to use a for-each loop for this, since you'll need to manually keep track of the array indices. 您将不需要为此使用for-each循环,因为您需要手动跟踪数组索引。 (Hint: the index into the input array and the index into the output array won't be the same once you see a space). (提示:一旦看到空格,输入数组的索引和输出数组的索引将不同)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM