Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a character array in Java without a specified length?

Tags:

java

    char ret[] = {};

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

Any help would be greatly appreciated.

like image 961
Shamoon Avatar asked Nov 27 '22 22:11

Shamoon


2 Answers

Arrays must have a fixed length.

If your goal is to have a dynamically expansible list, consider a List instead. Everytime you add an item by add() method, it will grow dynamically whenever needed.

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

See also:

  • Java Tutorials - Trail: Collections - The List Interface
like image 167
BalusC Avatar answered Dec 15 '22 04:12

BalusC


You're probably looking for an ArrayList<Character>.

like image 20
SLaks Avatar answered Dec 15 '22 04:12

SLaks