Results 1 to 13 of 13

Thread: Array Issues

  1. #1
    Sharoo is offline Member
    Join Date
    Nov 2011
    Location
    Australia
    Posts
    28
    Rep Power
    0

    Default Array Issues

    Hi everyone,
    I just have a quick question that probably has a really simple answer too. I've only just begun using arrays and I was wondering if it was possible to have an array that has 5 values initialised but also has room for it to be expanded, for example 15 extra positions giving a total of 20 with the initialised values.
    Thanks :)

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

    Default Re: Array Issues

    What happens when you code it and compile it?

  3. #3
    Sharoo is offline Member
    Join Date
    Nov 2011
    Location
    Australia
    Posts
    28
    Rep Power
    0

    Default Re: Array Issues

    That's my problem, I'm not sure how I would code it.
    I know that if I was just to make an array without any initialised values I would write
    private int sandwichesPurchased[] = new int[20]; for example
    and if I was to initialise values I would do this
    private int sandwichesPurchased[] = {1,2,3 . . .};
    but I'm not sure how I would incorporate the both of them into one.

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

    Default Re: Array Issues

    Ask the compiler if your code is valid.

  5. #5
    Sharoo is offline Member
    Join Date
    Nov 2011
    Location
    Australia
    Posts
    28
    Rep Power
    0

    Default Re: Array Issues

    I complied it with this as the array
    private int sandwichesPurchased[] = new int[20];
    and there are no errors.

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

    Default Re: Array Issues

    If the compiler doesn't complain, then it should work.

  7. #7
    Sharoo is offline Member
    Join Date
    Nov 2011
    Location
    Australia
    Posts
    28
    Rep Power
    0

    Default Re: Array Issues

    But when I compile this array
    private int sandwichesPurchased[] = new int[20];
    there are no values initialised in the array, all we know is that there are 20 positions in it.
    I was wondering if I could make an array that has initialised values as well as space if other values were needed to be input in later.

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

    Default Re: Array Issues

    I was wondering if I could make an array that has initialised values as well as space if other values were needed to be input in later.
    Did you ask the compiler?

  9. #9
    Sharoo is offline Member
    Join Date
    Nov 2011
    Location
    Australia
    Posts
    28
    Rep Power
    0

    Default Re: Array Issues

    How do I ask the compiler? Sorry for my lack of knowledge

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

    Default Re: Array Issues

    Type in a line of code and compile it.

  11. #11
    Sharoo is offline Member
    Join Date
    Nov 2011
    Location
    Australia
    Posts
    28
    Rep Power
    0

    Default Re: Array Issues

    I am not sure what I would put into so I could compile it.

  12. #12
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Array Issues

    Quote Originally Posted by Sharoo View Post
    That's my problem, I'm not sure how I would code it.
    I know that if I was just to make an array without any initialised values I would write
    private int sandwichesPurchased[] = new int[20]; for example
    and if I was to initialise values I would do this
    private int sandwichesPurchased[] = {1,2,3 . . .};
    but I'm not sure how I would incorporate the both of them into one.
    What do you mean incorporate both into one?

    Are you trying to create an empty array which isn't empty? o_O

    Or are you looking for something that is CONDITIONALLY empty?

    And what do you mean by empty, exactly? What does an empty element point to?
    Last edited by 2by4; 12-12-2011 at 02:28 PM.

  13. #13
    Junky's Avatar
    Junky is online now Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: Array Issues

    Lucky for you I'm on leave in a few days and I'm feeling benevolent. Here's an early Christmas present.
    Java Code:
    int[] arr = {1,2,3,4,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

Similar Threads

  1. Array traversal issues
    By sondratheloser in forum New To Java
    Replies: 3
    Last Post: 08-13-2012, 12:49 AM
  2. help! shapes output and array issues!
    By japgoth in forum New To Java
    Replies: 2
    Last Post: 01-25-2011, 09:49 PM
  3. jdk issues
    By artemff in forum New To Java
    Replies: 3
    Last Post: 01-02-2010, 03:18 AM
  4. Array issues
    By joka22 in forum New To Java
    Replies: 6
    Last Post: 10-07-2008, 11:05 AM
  5. Array issues
    By Neo82 in forum New To Java
    Replies: 1
    Last Post: 12-31-2007, 03:22 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
  •