troubleshooting Question

Creating array of unspecified length in java, or adding to an array beyond dimension

Avatar of EMB01
EMB01Flag for United States of America asked on
Java
17 Comments1 Solution394 ViewsLast Modified:
I want to be able to create an array that I can add to without fear of going out of dimension.  For instance, the following code:

	public static void main(String[] args)
	{
		
		String[] array = new String[3];
		array[0] = "string1";
		array[1] = "string2";
		array[2] = "string3";
		
		for (String string : array)
		{
		
			System.out.println(string);
			
		}
		
		// following line makes the array go out of bounds
		array[4] = "string3";
		
	}

I don't really want to have to use a collection (or vector?) but I will if that's the only way to achieve dynamic array functionality.  Thank you.
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 17 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 17 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros