Do arrays need to be a predefined size?

I need a way to store multiple SQL statements in a String array, but the number of statements are unknown. Can I keep adding onto an array without giving it a pre-defined size? Eventually once the user has performed all the actions needed, I'll create a prepared statement what will read from the array using a 'for' statement and run all the queries that are in the array.

[383 byte] By [tristanlee85a] at [2007-11-27 9:35:17]
# 1
Use a collection. http://java.sun.com/docs/books/tutorial/collections/index.html
Navy_Codera at 2007-7-12 23:01:58 > top of Java-index,Java Essentials,Java Programming...
# 2
Yes they do. What you need is a List, most likely an ArrayList: http://java.sun.com/j2se/1.5.0/docs/api/java/util/List.html
hunter9000a at 2007-7-12 23:01:58 > top of Java-index,Java Essentials,Java Programming...
# 3
You can add,remove elements in a Vector/Arraylist but the size of an Array cannot be changed afterwards.correct me if im wrong.
deAppela at 2007-7-12 23:01:58 > top of Java-index,Java Essentials,Java Programming...
# 4
> You can add,remove elements in a Vector/Arraylist but> the size of an Array cannot be changed afterwards.> > correct me if im wrong.That's right.*cue the reflection haxors*
hunter9000a at 2007-7-12 23:01:58 > top of Java-index,Java Essentials,Java Programming...
# 5

> > You can add,remove elements in a Vector/Arraylist

> but

> > the size of an Array cannot be changed afterwards.

> >

> > correct me if im wrong.

>

> That's right.

>

> *cue the reflection haxors*

Even with reflection there is no possibility of resizing an array, array.length is a public final int.

-Kayaman-a at 2007-7-12 23:01:58 > top of Java-index,Java Essentials,Java Programming...
# 6

> Yes they do. What you need is a List, most likely an

> ArrayList:

>

> http://java.sun.com/j2se/1.5.0/docs/api/java/util/List

> .html

If your only operations are insert a statement and iterate over all statements, you may prefer a LinkedList over an ArrayList. Both will do fine in any case. If you declare the list variable to have type List<String>, you can easily swap between the two as you please.

OleVVa at 2007-7-12 23:01:58 > top of Java-index,Java Essentials,Java Programming...