如何用 java 中的不同字符填充一个字符数组?

how to fill a char array with DIFFRENT chars in java?


我只找到了如何用相同的字符填充一个字符数组,比如:

    char[] charArray = new char[5];
    char charValue = 'A';
    Arrays.fill(charArray, charValue);
    System.out.println("The char array content is: " + Arrays.toString(charArray));
    
   // output: The char array content is: [A, A, A, A, A]

我试过的是这样的:

    char[] abc = new char[5];
    
    Scanner sc = new Scanner(System.in);
    
    String s;
    char c;
    
    for (int i = 0; i < abc.length; i++) {
        s = sc.next();
        if (s.length() == 1) {
            c = s.charAt(0);
            System.out.println("for position " + i + " the character is: " + c);
            Arrays.fill(abc[i], c);
            System.out.println("so we get the current array " + Arrays.toString(abc));
        } else {
            System.err.println("pls give just one charcter");
            break;
        }
    }

我总是遇到错误,将 abc 数组从 char[] 更改为 long[]。\

编辑:
我想要的输出是我在最后给出 5 个字符(例如 a、b、c、d、e)我有数组

abc[] = {a, b, c, d, e}

提前谢谢你:)

您可以使用:

Arrays.fill(abc, c);

或者只是:

abc[i]=c;

如果只想填充数组的指定位置

似乎是正确的。

避免char

不幸的是,即使是您的代码的固定版本也会因大多数字符而失败。 char 类型自 Java 2 以来基本上已损坏,自 Java 5.

以来已成为传统。

要查看 char 中断,请使用“”之类的字符。

代码点

改用 code point 个整数。

StringBuilder 作为结果

类似于这个未经测试的代码。

int limit = 5 ;
StringBuilder sb = new StringBuilder() ;

Scanner sc = new Scanner(System.in);

String input;
int codePoint;

for ( int i = 0 ; i < limit ; i++ ) {
    input = sc.next();
    int[] codePoints = input.codePoints().toArray() ;
    if ( codePoints.length == 1 ) {
        codePoint = codePoints[ 0 ] ;
        System.out.println( "for position " + i + " the input character is: " + Character.toString( codePoint ) );
        sb.appendCodePoint( codePoint );
        System.out.println("so we get the current result " + sb.toString() );
    } else {
        System.out.println( "Done: " + sb.toString() ) ;
        break;
    }
}
System.out.println( "Done: " + sb.toString() ) ;

看到这个code run live at IdeOne.com

for index 0 the input character is: H
so we get the current result H
for index 1 the input character is: 
so we get the current result H
for index 2 the input character is: l
so we get the current result Hl
for index 3 the input character is: l
so we get the current result Hll
for index 4 the input character is: o
so we get the current result Hllo
Done: Hllo

作为结果的字符串(字符)列表

如果您想单独收集每个字符,请制作一个字符串列表。为每个代码点制作一个字符串,并添加到列表中。省略 StringBuilder.

List< String > characters = new ArrayList <>() ;
…
characters.add( Character.toString( codePoint ) ) ;

查看 this code run live at IdeOne.com 的示例。

for index 0 the input character is: H
so we get the current result [H]
for index 1 the input character is: 
so we get the current result [H, ]
for index 2 the input character is: l
so we get the current result [H, , l]
for index 3 the input character is: l
so we get the current result [H, , l, l]
for index 4 the input character is: o
so we get the current result [H, , l, l, o]
Done: [H, , l, l, o]

请注意,在某些语言中,您可能认为的一个“字母”实际上是 两个 个字符的组合,一个字母后跟一个“组合变音符号”。示例 here. Discussion here.

据我所知,您试图让用户将数据一个一个地输入到您的字符数组中...如果这就是您想要的,那么这应该有所帮助。

char[] input = new char[5];
for( int i = 0; i < input.length; i++ ) {
     System.out.println("Enter only one character");
     input[i] = (char)System.in.read();
     System.out.println("Thus we have the current array as " + Arrays.toString(input));
}

此代码一次只接受一个字符,比使用扫描仪更快。 这是此代码的干 运行:

Enter a charcater
a
Thus we have cuurent array as [a, ., ., ., .]
Enter a charcater
b
Thus we have cuurent array as [a, b, ., ., .]
Enter a charcater
c
Thus we have cuurent array as [a, b, c, ., .]
Enter a charcater
d
Thus we have cuurent array as [a, b, c, d, .]
Enter a charcater
a
Thus we have cuurent array as [a, b, c, d, e]

如果您想接受一个字符串并将其改为字符数组,您可以使用字符串函数toCharArray(),char[] input = "abcde".toCharArray()