Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array 1

Status
Not open for further replies.

monkey85

Programmer
Mar 31, 2005
7
0
0
GB
is there anyway of unspecifying the size of an array so the number of elements input can vary each time?
I'm currently using an array to store values entered which are then manipulated but the array is set to 100000 and is creating problems.

Thanks in advance
 
The rules about Java arrays are pretty strict when it comes to size: once established, there's no change.
But there is a usefull class in java.util package called ArrayList. That collection works just like an array except that you can change the size by adding/removing members.
Moreover, starting from JDK 5.0, you can add type to the arraylists (wasn't possible prior 5.0, ArrayList returned objects of type Object). Word of caution: generic array lists cannot be of primitive type, but the wrapper classes do the trick
Code:
import java.util.*

public class SomeName{
ArrayList a <theType>= new ArrayList<theType>(some members);

...//some code
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top