• Post Reply Bookmark Topic Watch Topic
  • New Topic

Some String-like questions  RSS feed

 
Brandt Charles
Ranch Hand
Posts: 57
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. While I understand StringTokenizer, per Java docs, is not recommended to be used, my instructors insist on requiring its use. Is each token considered data type String or StringTokenizer?

2. split() returns an array of strings, yet the length of an array cannot be increased (if I remember correctly) once it's been set. If I clear the previously split String array, and split another of the same name, will it allow a length large enough to accomodate the current number of elements? For example, say the first string splits into 3 elements. The next will be split into 5 elements. Will this generate an out of bounds exception or other error?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Brandt Charles:
1. While I understand StringTokenizer, per Java docs, is not recommended to be used, my instructors insist on requiring its use. Is each token considered data type String or StringTokenizer?


The type returned by nextToken() is String.

2. split() returns an array of strings, yet the length of an array cannot be increased (if I remember correctly) once it's been set. If I clear the previously split String array, and split another of the same name, will it allow a length large enough to accomodate the current number of elements? For example, say the first string splits into 3 elements. The next will be split into 5 elements. Will this generate an out of bounds exception or other error?


Each time you call split(), it will produce a new array. The size of the array doesn't change. You are correct that the size of an array cannot be increased once its created. However, a reference to an array can be assigned to refer to another array of that type as long as the reference isn't declared final.
[ July 05, 2006: Message edited by: Keith Lynn ]
 
Brandt Charles
Ranch Hand
Posts: 57
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Keith. I think I'm going to try this with split()
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!