Results 1 to 5 of 5
  1. #1
    kev3kev3 is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Creating an array constructor and then creating an object in a main method

    Hi,

    I am new to programming and Java and get easily confused so any help is much appreciated.

    I have created a constructor as follows:


    private int [] myArray;

    public MinInt (int [] setArray)
    {
    myArray = setArray;
    }

    I know want to use this constructor in my main method to create an array of integers but can't seem to get the syntax correct to declare this.

    At the minute I have:

    MinInt theArray = new MinInt {2,4,3,1,6,8};

    But this does not work.

    I know this is probably very simple.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default Re: Creating an array constructor and then creating an object in a main method

    Define an array of ints
    Pass the name of that array in the constructor.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    kev3kev3 is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Re: Creating an array constructor and then creating an object in a main method

    So this would be correct?


    int [] anArray = {3,76,2,33,76,24,65,9};
    MinInt theArray = new MinInt (anArray);

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default Re: Creating an array constructor and then creating an object in a main method

    A good way to make sure would be to try it in the compiler.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    kev3kev3 is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Re: Creating an array constructor and then creating an object in a main method

    Yes, it worked.

    Just wanted to check that, how I have done it is the best way..

Similar Threads

  1. Replies: 3
    Last Post: 01-04-2012, 03:30 PM
  2. Replies: 9
    Last Post: 07-29-2011, 03:18 PM
  3. Creating a main method?
    By Brainz in forum New To Java
    Replies: 12
    Last Post: 11-16-2010, 11:56 AM
  4. No main method, constructor/object not working
    By sinista in forum New To Java
    Replies: 9
    Last Post: 10-14-2009, 03:25 PM
  5. Need help with creating array of type object
    By riz618 in forum New To Java
    Replies: 3
    Last Post: 01-29-2008, 07:14 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
  •