• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to create dynamic string array if we dont know number of strings in the beginning?  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to create dynamic string array if we dont know number of strings in the beginning?
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nil wangad wrote:how to create dynamic string array if we dont know number of strings in the beginning?

Does it have to be an array ? This is the sort of thing an ArrayList was designed for.
 
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart A. Burkett wrote:
nil wangad wrote:how to create dynamic string array if we dont know number of strings in the beginning?

Does it have to be an array ? This is the sort of thing an ArrayList was designed for.

Or one of Set implementations if there sould be no duplicates.
For just storing Strings use HashSet.
If you need to remember insertion order use LinkedHashSet.
If you need your Strings sorted use TreeSet.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or even just a LinkedList<String>.

Winston
 
Bartender
Posts: 321
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi nil,

I kind of feel your original question has been lost in the heated (and increasingly personal) debate over if an ArrayList is an dynamic array or not.

The bottom line regarding your question is that the original responses to use a List if you need a dynamic array is correct.
On the other hand, if this is for a test (not something I would do in production code), you could create a dynamic String array using a helper method. For example:



That would check to see if the last element of the String array is still null. If it is not, the array would increase in size by 1 and the new element would be added - returning the new array for you to use.

Cheers!
Chris
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris R Barrett wrote:That would check to see if the last element of the String array is still null. If it is not, the array would increase in size by 1 and the new element would be added - returning the new array for you to use.

I certainly agree with your point, but your method will only work if oldStringArray.length is initially 0 or 1; otherwise you'll have a "gap" at the start - although, in your defence, the post was about dynamic arrays.

I'd also be tempted to separate the business of "resizing" from that of adding a new element completely; that way, your logic is less tightly coupled.

Winston
 
Chris Barrett
Bartender
Posts: 321
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Winston,

You are right. A full MyStack Class would make more sense than just a "do it all" helper method.

Ideally, it would iterate through the existing String array to confirm there are no null elements that could be used before increasing the size.

Thank you for pointing out my late night logic problems.

Cheers!
Chris
 
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris R Barrett wrote:I kind of feel your original question has been lost in the heated (and increasingly personal) debate over if an ArrayList is an dynamic array or not.

You're right. I've therefore moved the heated debate to a different thread in our Java in General forum.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!