Results 1 to 3 of 3
  1. #1
    frosty is offline Member
    Join Date
    Aug 2010
    Posts
    1
    Rep Power
    0

    Default non primitive arrays

    hi, i have an exam ICT tomorrow but i am stuck with one thing that i can't seem to understand, its about NON-primitive arrays. Is it possible to explain the java code with a easy example? i work with Jcreator.

    thanks in advance

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,775
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by frosty View Post
    hi, i have an exam ICT tomorrow but i am stuck with one thing that i can't seem to understand, its about NON-primitive arrays. Is it possible to explain the java code with a easy example? i work with Jcreator.

    thanks in advance
    There is nothing special about arrays of objects (non-primitives) given a type T, an arrays of references to T is defined as follows:

    Java Code:
    T[] array= new T[42];
    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(1), new T(2),  new T(3) };
    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.

    kind regards,

    Jos

  3. #3
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    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

  1. Primitive data type and class
    By Roselicious in forum New To Java
    Replies: 3
    Last Post: 04-19-2010, 04:27 PM
  2. JNI accessing non primitive data type
    By H_P in forum Advanced Java
    Replies: 1
    Last Post: 04-14-2010, 06:43 AM
  3. primitive Data types
    By Manfizy in forum New To Java
    Replies: 2
    Last Post: 07-07-2009, 09:29 PM
  4. Primitive data types of Java
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-28-2008, 08:29 PM
  5. Uisng primitive type values as keys for Hashtable
    By ravian in forum New To Java
    Replies: 3
    Last Post: 11-21-2007, 11:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •