Results 1 to 7 of 7

Thread: The size of an array

  1. #1

    Thread Starter
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Resolved The size of an array

    I have created an integer array, i want to store the size of the array in a variable (for an insertion sort). But i get an error when i do the following code.
    Code:
    int[] myArray;
    int lastPos = myArray.length();
    The error is "cannot perform length() on an integer array" (or something similar to that, i'm not at my computer now so i cant see the error).

    What is the problem?
    Last edited by x-ice; Feb 25th, 2007 at 07:24 PM.

  2. #2
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: The size of an array

    The array hasn't been initialised.
    int[] a = new int[3];

  3. #3

    Thread Starter
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Re: The size of an array

    Quote Originally Posted by DeadEyes
    The array hasn't been initialised.
    int[] a = new int[3];
    How can i initialise an array that i dont know the size of, also can i change the size at a later time? I dont want to use an ArrayList (these are dynamic, size isn't a problem with these).

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: The size of an array

    You can add to the array any time you want.
    But you can't assign the length to a variable until it has a value
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5

    Thread Starter
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Re: The size of an array

    Quote Originally Posted by ComputerJy
    You can add to the array any time you want.
    But you can't assign the length to a variable until it has a value
    So if for example i assign 10 spaces for the array when i initialise it, can i then add more spaces once there is data in the array without losing the data?

  6. #6
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Re: The size of an array

    Quote Originally Posted by x-ice
    So if for example i assign 10 spaces for the array when i initialise it, can i then add more spaces once there is data in the array without losing the data?
    Normally for a dynamically sized list, you would use one of the many Collection classes, like ArrayList for instance.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  7. #7
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: The size of an array

    Quote Originally Posted by x-ice
    So if for example i assign 10 spaces for the array when i initialise it, can i then add more spaces once there is data in the array without losing the data?
    You can use arrays concatenation any time, without losing data
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width