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 5 of 5

Thread: Need knowledge on initialising arrays of various types

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    2
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post Need knowledge on initialising arrays of various types

    For basic arrays, we can directly combine declaration and initialization as for eg: int[] num= new int[2]
    But can we do this do for forming class object arrays?
    like

    class stu
    {
    .....
    }
    stu[] s=new stu[2]

    ????


    please help. I am noob 2 java...


  2. #2
    Junior Member
    Join Date
    Sep 2014
    Posts
    21
    My Mood
    Cheeky
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Need knowledge on initialising arrays of various types

    Yes, you can make an array of objects.
    Remember code goes in between code tags =)

  3. The Following User Says Thank You to Hamenopi For This Useful Post:

    crisdeodates (September 18th, 2014)

  4. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    2
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need knowledge on initialising arrays of various types

    Thanks Hamenpoi,

    But I didnt get your point. I am new to java. Could you explain with an example?
    Thanks

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need knowledge on initialising arrays of various types

    Also for arrays of objects, the array definition only creates slots for objects, it does not assign values to any of those slots. The code will need to do that in separate steps.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    crisdeodates (September 18th, 2014)

  7. #5
    Junior Member
    Join Date
    Sep 2014
    Posts
    21
    My Mood
    Cheeky
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Need knowledge on initialising arrays of various types

    Quote Originally Posted by crisdeodates View Post
    Thanks Hamenpoi,

    But I didnt get your point. I am new to java. Could you explain with an example?
    Thanks
    public class MathEntity extends Entity {
      private MathEntity results[] = new MathEntity[3];
      private int result;
     
      public MathEntity() {
        id = EntityIDList.MATHENTITY;
      }
     
      public MathEntity(int val) {
        this();
        name = Integer.toString(val);
      }
     
      private void setAnswers() {
        results[0] = new MathEntity(result);
        results[1] = new MathEntity(wrongAnswer(result));
        results[2] = new MathEntity(wrongAnswer(result));
      }

    Do you mean like this? It's no where near optimal but neither am I.

Similar Threads

  1. How much knowledge should I have to create a 2d game
    By SauronWatchesYou in forum Java Theory & Questions
    Replies: 2
    Last Post: April 14th, 2014, 04:53 PM
  2. Recipe Book: Arrays and Objects/Types
    By SilentNite17 in forum Object Oriented Programming
    Replies: 2
    Last Post: February 27th, 2013, 11:24 PM
  3. NEW COMER SEEKING KNOWLEDGE AND GURUS
    By Abu Muhammad in forum Member Introductions
    Replies: 1
    Last Post: March 17th, 2011, 04:34 AM
  4. increasing my java knowledge
    By doogiexiv in forum Java Theory & Questions
    Replies: 1
    Last Post: March 8th, 2010, 11:00 PM