Results 1 to 3 of 3
  1. #1
    Ujemny is offline Member
    Join Date
    Nov 2013
    Location
    Poland
    Posts
    8
    Rep Power
    0

    Default Creating arrays in constructor

    Hello guys!
    My problem is: i wanna send two arrays to constructor.
    I mean i have class Date and constructor which save arrays which i gave as arguments in main class.

    My main class would look like this:

    Date date = new Date([12,3,45],[12,45,678])

    Is it possible to give arrays as arguments in main class and send it to construcotr? Creating method Add which adds one element to array is confusing and not elegant way.
    How my constructor should looks like?

    I hope you understand my problem.
    Sorry for my english.

    Ty for advice!

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,395
    Rep Power
    25

    Default Re: Creating arrays in constructor

    To see what is allowed in a class's constructor, read the class's API doc.
    The syntax: [12,45,678] is not legal java.
    Use the new statement to define an array: new int[] {1,2};


    BTW Date is the name of a java SE class, better to choose a new unique name.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Ujemny is offline Member
    Join Date
    Nov 2013
    Location
    Poland
    Posts
    8
    Rep Power
    0

    Default Re: Creating arrays in constructor

    Oh god, you save my life!
    I'm programming in python and java.. that's why i sometimes mix syntax ;x

    But it works!

    My constructor looks like

    Datas(int[] A, int[] B){
    data1 = A;
    data2 = B;
    }
    and main:

    Datas data = new Datas(new int[] {12,3}, new int[] {1,2,3,4});

    Thank you a lot!
    Last edited by Ujemny; 11-19-2013 at 02:27 AM.

Similar Threads

  1. Creating Arrays
    By steveday77 in forum New To Java
    Replies: 3
    Last Post: 05-01-2013, 12:47 AM
  2. Replies: 4
    Last Post: 04-02-2012, 01:50 PM
  3. Replies: 0
    Last Post: 11-03-2011, 07:33 AM
  4. Creating a constructor with arrays and arguments - Part 2
    By fullmetaljacket in forum New To Java
    Replies: 15
    Last Post: 07-05-2009, 01:38 PM
  5. Creating a constructor with arrays and arguments
    By fullmetaljacket in forum New To Java
    Replies: 38
    Last Post: 07-03-2009, 06:09 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
  •