0

If I don't know the size of array of chars I'm using, is there a way to use it without setting the size upfront ?

2 Answers 2

2

In general, you can use a java.util.List when you don't know the size up front.

List<Character> chars = new ArrayList<Character>();
chars.add('f');
chars.add('o');
chars.add('o');

Depending on your needs, a StringBuilder might make more sense than a List<Character>.

StringBuilder sb = new StringBuilder();
sb.append('f')
  .append('o')
  .append('o');

If you want to get really slick, use Trove for its list-of-primitives so you can work with the (third-party equivalent of) a List<char>. nevermind. Trove does not have a TCharArrayList.

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

Comments

0

Can you use a String or a StringBuilder and then convert it to a char array later?

Comments

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.