Ted pottel
Ted pottel

Reputation: 6983

Dynamically Changing the Size of an Array

I'm writing a simple GPS program that stores locations in a array of String objects. Right now the array is set to 100. When another String is added, it creates a new String array of 200, copies the old to the new, then sets the variables equal to the new one. Is there a way to just dynamically enlarge the size of an array, without having to create a new array every time?

Upvotes: 1

Views: 160

Answers (4)

user1106888
user1106888

Reputation: 255

best way is to use the ArrayList

Upvotes: 0

Andrew White
Andrew White

Reputation: 874

It sounds like you should use the ArrayList class. It gives you methods for growing your array dynamically.

Upvotes: 4

Sam Dozor
Sam Dozor

Reputation: 40744

Or you can use ArrayList<String> and just keep adding to it as you see fit.

Upvotes: 1

ajpolt
ajpolt

Reputation: 1002

Try a Vector?

Obviously, you could also use any of the other classes in the Java Collections Framework as well, depending on your requirements.

Upvotes: 1

Related Questions