Results 1 to 5 of 5

Thread: JavaScript Arrays...Dynamic or Static size

  1. #1

    Thread Starter
    Hyperactive Member Sneeden's Avatar
    Join Date
    Oct 2001
    Location
    Sneedville
    Posts
    258

    JavaScript Arrays...Dynamic or Static size

    Is there something equivalent to redim? Or does javascript support Arraylists or Vectors?

  2. #2
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724
    They're always dynamic
    Code:
    var myArray = new Array("abc", "xyz");
    myArray[myArray.length] = "ghi";
    alert(myArray[2]);

  3. #3

    Thread Starter
    Hyperactive Member Sneeden's Avatar
    Join Date
    Oct 2001
    Location
    Sneedville
    Posts
    258
    Maybe you misunderstood (or maybe not).

    In vb you can
    VB Code:
    1. dim MyArray()
    2.  
    3. redim MyArray (100)
    4.  
    5. 'do stuff with array. Decide you need a bigger array.
    6.  
    7. redim preserve MyArray (150)
    8.  
    9. 'now the 1st 100 elements still contain that original data, except there are 50 more elements. I am sorry if your example does that. I don't have time to check before leaving work today.
    Any one else feel free to comment please.

    Thanks.

  4. #4
    Addicted Member
    Join Date
    Sep 2002
    Location
    Durham, NC, US
    Posts
    218
    Axion's example does that. JavaScript arrays are always dynamic. The code handles the array's dimensions for you, you never have to worry about it.
    Code:
    var myArray = new Array();  //currently the array is completely empty, it will return no elements, but will support push() and such
    myArray[0] = "foo";  //The array now has a length of 1, and the value of the first element is a string
    myArray.push("bar");  //The array now has a length of 2
    myArray[99] = "blah";  //The array now has a length of 100
    //but only the first two and the last one elements have values assigned
    //the rest are still empty
    Travis, Kung Foo Journeyman

    Web Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.5 Guide and Reference
    Perl: Documentation, Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    OSS: Mozilla, MySQL (Manual)

  5. #5

    Thread Starter
    Hyperactive Member Sneeden's Avatar
    Join Date
    Oct 2001
    Location
    Sneedville
    Posts
    258
    Great, your guys are just full of useful info.

    thx again.

    sneed

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