Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Array of a Class Type containing an array -- array sizes not know at compile time.

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Array of a Class Type containing an array -- array sizes not know at compile time.

    I have the following types declared.

    class Piece
       {
       public byte type;
       public byte loc;
       }
     
    class Config
       {
       public Config( int n ) { piece = new Piece[ n ]; }
       private Piece [] piece;
       private int next;
       private int parent;
       }
     
    class QHT
       {
       private Config [] conf;
       public QHT( int m, int n );
       }
    In the constructor for QHT, I want to allocate "conf" to an array of m "Config"s where each Config contains an array of n "Piece"s. The values of m and n are not known at compile time. (If necessary, I could fix the value of m at compile time but
    I can't do this for n. Also, m will be rather large.)

    How can I do this in Java?


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Array of a Class Type containing an array -- array sizes not know at compile time.

    Quote Originally Posted by GCRhoads View Post
    ...In the constructor for QHT, I want to allocate "conf" to an array of m "Config"s where each Config contains an array of n "Piece"s...
    Two steps in the constructor for QHT:

    1. Set conf equal to a new array of m Configs.

    2. Make a loop that sets each element of conf equal to a new Config(n).


    Quote Originally Posted by GCRhoads View Post
    ...Also, m will be rather large....
    So???


    Cheers!

    Z

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array of a Class Type containing an array -- array sizes not know at compile time.

    Quote Originally Posted by Zaphod_b View Post
    Two steps in the constructor for QHT:

    1. Set conf equal to a new array of m Configs.

    2. Make a loop that sets each element of conf equal to a new Config(n).


    Z
    Thanks!

Similar Threads

  1. trouble with making a simple construct for an array of double type
    By Abadude in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 29th, 2012, 05:05 PM
  2. having problem declaring a generic type array in OrderSet class
    By mia_tech in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 10th, 2012, 06:39 PM
  3. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  4. Creating array from generic type objects?
    By AndrewMiller in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 13th, 2010, 09:22 PM
  5. Error of missing return statement while implementing Array
    By MS_Dark in forum Collections and Generics
    Replies: 1
    Last Post: December 10th, 2008, 03:18 PM