4
    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.

1
  • I think you are wanna a dynamic array. If specified length means dynamic length, you should set the length with a variable. So the length will not be fixed by constant value, while it will be set at runtime. char[] ret = new char[size]. The size will be set at runtime. Commented Jun 27, 2023 at 16:18

9 Answers 9

19

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<>();
// ...

See also:

Sign up to request clarification or add additional context in comments.

Comments

14

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

Comments

6
char[] ret = new char[0];

this will create an empty array. But you can use it only as a placeholder for cases when you don't have an array to pass.

If you don't know the size initially, but want to add to the array, then use an ArrayList<Character>

Comments

4

The best way to have an extensible array of char without the overhead of an Character object for each char, is to use a StringBuilder. This allows you to build an array of char in a wide variety of ways. Once you are finished you can use getChars() to extract a copy of the char[].

2 Comments

^ for idea other than collection and i think buffer have a less over head.
@dubey-theHarcourtians this is what we do. We have a buffer of 8-bit characters which can be off heap.
3

you should use:

char[] ret = {};

1 Comment

irrelevent.user was asking for runtime char Array?
2

You cannot create an array without a length only with a length of 0, and why would u want that?

Comments

1

Your question doesn't really make sense; an array always has some length. But is this what you were thinking of?

char[] ret = null;

This creates a reference to an array, but initialises it to null. There is no actual array yet.

Comments

1

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

Comments

0

You probably come from PHP or another programming language that hasn't got real arrays. (An array in PHP is a sort of dictionary/hashmap)

Arrays in JAVA are fixed length.

For no fixed length, you can use a Vector. If you want to index with something else than integers from 0 to length, you can use a Dictionary.

3 Comments

+0: IMHO Vector is a legacy class which was replaced in Java 1.2 (1998) by List Similarly Dictionary was replaced by Map. In the Javadoc for Dictionary it states NOTE: This class is obsolete. New implementations should implement the Map interface, rather than extending this class.
Hey Peter, I had no idea, thanks for that info! I'm actually more of a C# dev so I looked up the equivalent Java classes but I had no idea that Vector<> and Dictionary<,> where obsolete and replaced, well spotted!
Java is often very slow to phase out "legacy" classes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.