How to create an array of characters in Java without a specified length?

    char ret[] = {};

Doesn't work, seems to work, and I'm not sure what the best way to do this.

Any help would be greatly appreciated.

+3
source share
9 answers

You are probably looking ArrayList<Character>.

+14
source

Arrays must have a fixed length.

If your goal is to have a dynamically expandable list, consider List. Each time you add an element by a method add(), it will grow dynamically when necessary.

List<Character> chars = new ArrayList<Character>();
// ...

See also:

+15
source
char[] ret = new char[0];

. , .

, , ArrayList<Character>

+4

0, ?

+2

char Character char - StringBuilder. char . , getChars() char [].

+2

:

char[] ret = {};
+2

; . , ?

char[] ret = null;

, null. .

+1

Thinks you are still thinking in C or Pascal. The functionality you want is probably a Java String. char[]rare in Java. (You can iterate over the characters of a string with charAt, among other possibilities.)

+1
source

You probably came from PHP or another programming language that has no real arrays. (An array in PHP is a kind of dictionary / hashmap)

Arrays in JAVA are fixed length.

Without a fixed length, you can use Vector. If you want to index something else besides integers from 0 to length, you can use a dictionary.

0
source

All Articles