Results 1 to 3 of 3
Thread: non primitive arrays
- 08-26-2010, 08:57 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 1
- Rep Power
- 0
- 08-26-2010, 09:06 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
There is nothing special about arrays of objects (non-primitives) given a type T, an arrays of references to T is defined as follows:
This gives you 42 elements of type T and all of them are null. You have to set them to a non-null value yourself. You can also do that in an array initializer:Java Code:T[] array= new T[42];
This assumes that the type T has a constructor that takes one int argument. This defines an array of three T type objects, each instantiated in the array initializer list.Java Code:T[] array= { new T(1), new T(2), new T(3) };
kind regards,
Jos
- 08-27-2010, 02:55 AM #3
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Jos explained it but I will try easier again
say you have a class called Player
well if you want an array of Player then there is no big difference than those of primitive types
Player [] player=new Player[5]; // this gives you 5 Player types uninitialised of course.
to initialise just do this
player[0]=new Player(); // if player has a overrriden constructor just add the arguments
you could also use a for loop to initialise the whole array of Player objects.
Similar Threads
-
Primitive data type and class
By Roselicious in forum New To JavaReplies: 3Last Post: 04-19-2010, 03:27 PM -
JNI accessing non primitive data type
By H_P in forum Advanced JavaReplies: 1Last Post: 04-14-2010, 05:43 AM -
primitive Data types
By Manfizy in forum New To JavaReplies: 2Last Post: 07-07-2009, 08:29 PM -
Primitive data types of Java
By Java Tip in forum Java TipReplies: 0Last Post: 03-28-2008, 07:29 PM -
Uisng primitive type values as keys for Hashtable
By ravian in forum New To JavaReplies: 3Last Post: 11-21-2007, 10:13 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks