Results 1 to 4 of 4
Thread: Arrays in Java
- 07-24-2007, 07:00 AM #1
Member
- Join Date
- Jun 2007
- Location
- Colombo, Sri Lanka
- Posts
- 32
- Rep Power
- 0
- 07-24-2007, 11:54 AM #2
Member
- Join Date
- Jul 2007
- Location
- England, Bath
- Posts
- 47
- Rep Power
- 0
Arrays are declared using the [] notation so to declare an array of String objects you would do the following:
Now if you want to actually create the instance of the String array then you need to specify the size within the []'s:Java Code:String[] stringArray;
This then gives you an empty container to place your String objects into the array:Java Code:String[] stringArray = new String[7];
etc etc.Java Code:stringArray[0] = "First";
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:
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.Java Code:Bob[] bobs = new Bob[7]; bobs[0] = new Bob("Some", "Data"); bobs[1] = otherBobVariable;
Hope this helps.
- 07-25-2007, 08:04 PM #3
Member
- Join Date
- Jul 2007
- Posts
- 55
- Rep Power
- 0
I personally use ArrayList instead of Arrays. You can use a helper method to convert the arraylist to an array. That way you don't have to set the initial or default size of the array. It also prevents OutofBound exceptions.
- 07-30-2007, 09:10 AM #4
Member
- Join Date
- Jul 2007
- Posts
- 74
- Rep Power
- 0
Similar Threads
-
Arrays
By bunbun in forum New To JavaReplies: 1Last Post: 04-09-2008, 02:24 AM -
new to arrays
By jimJohnson in forum New To JavaReplies: 1Last Post: 04-08-2008, 02:45 PM -
Help needed with java arrays code
By d24706 in forum New To JavaReplies: 2Last Post: 03-07-2008, 01:11 AM -
2D-Arrays
By kbyrne in forum New To JavaReplies: 1Last Post: 02-07-2008, 10:08 PM -
arrays help
By Warren in forum New To JavaReplies: 6Last Post: 11-23-2007, 07:23 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks