Thread: Arrays in Java
View Single Post
  #2 (permalink)  
Old 07-24-2007, 01:54 PM
shanePreater shanePreater is offline
Member
 
Join Date: Jul 2007
Location: England, Bath
Posts: 47
shanePreater is on a distinguished road
Arrays are declared using the [] notation so to declare an array of String objects you would do the following:
Code:
String[] stringArray;
Now if you want to actually create the instance of the String array then you need to specify the size within the []'s:

Code:
String[] stringArray = new String[7];
This then gives you an empty container to place your String objects into the array:
Code:
stringArray[0] = "First";
etc etc.

Moving to something a bit more complex like your own classes the same prinicipal applies so if we had a class Bob which is constructed with a pair of string arguments it would be something like this:

Code:
Bob[] bobs = new Bob[7]; bobs[0] = new Bob("Some", "Data"); bobs[1] = otherBobVariable;
This is perfectly fine for fixed sized arrays but you may be better off using a collection instead and then using the collections toArray() methods to create your array when you need it.

Hope this helps.
__________________
Shane Preater -
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Reply With Quote