Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
This week, we're updating our course layouts and descriptions. Presentation and materials always gently change over time, but just occasionally there's a need to make a step change to clear out some of the old and roll in the new. That's now happening - but over a long and complex site it's not instant and you'll see sections of the site changing up to and including 19th September.

See also [here] for status update
 
Want something more dynamic than an array?

Most traditional programming languages include support for "arrays", in which the program can store a whole series of pieces of information in the same type. For fast access, the elements of an array are usually held in successive memory locations, and each element takes the same amount of storage.

With languages such as C, the number of elements in an array (its dimension) was defined at compile time, and was then fixed in the executable program file. This created a problem if the programmer didn't know what the maximum size would be, and a number of mechanisms such as "malloc" were added to allow what was in effect an array to be defined while the program ran.

By contrast to C, the Java language has a dynamic memory allocation model, so arrays can be dimensioned at run time. You can write a program that works out how big an array needs to be and THEN dimension it to allocate the memory. Alas, once an array is dimensioned even in Java you can't come back later and simply extend it - after all, there's probably something else in the computer's memory directly after the end of your array by this time.

Java supports a number of collection types (via standard classes) that let you define structures to hold a whole series of objects, even if you don't know how many objects you'll want to hold in your structure. Vectors (or also ArrayLists in Java 2) can hold as many objects as you like, and you don't have to tell Java how many. The information isn't stored sequentially in memory, so their use isn't as efficient as an array, and they're not a part of the base Java language so you have to access them through an object syntax, but they do solve the problems of changing an array size even after it has some data stored in it!

You can go a stage further too. Array elements are traditionally numbered (indexed) from 0 or 1 up - that's fine for many uses, but there are often times you would prefer to give each element a unique name. Java's standard HashTable class (also a HashMap from Java 2) give you this ability, though it brings in a whole series of new considerations too if you need to be able to iterate through all the elements in such a collection.

==========================================================

If you're not a Java writer ...

Perl's "lists" are the equivalent of a Vector in Java, and "hashes" are the equivalent of a HashTable - Perl doesn't actually use arrays even though the word used to be mistakenly used to describe both lists and hashes.

In Tcl, an "array" is the equivalent of a HashTable in Java. As all data in Tcl is held in strings, there is no equivalent to an array or a Vector in Java - elements simply can't be counted like that.

PHP arrays can be treated as either HashTables or as Vectors - they can always be dynamically extended, and you can reference then by number (position) or by text key as you wish.


See also Java Utility Classes

Please note that articles in this section of our web site were current and correct to the best of our ability when published, but by the nature of our business may go out of date quite quickly. The quoting of a price, contract term or any other information in this area of our website is NOT an offer to supply now on those terms - please check back via our main web site

Related Material

Java - Fundamental classes
  [3048] String handling - from first steps to practical examples - (2010-11-13)
  [2920] Sorting - naturally, or into a different order - (2010-08-14)
  [2734] for and foreach in Java - (2010-04-22)
  [2649] Length, size or capacity in Java? - (2010-02-24)
  [2421] Sorting Collections of Objects in Java - (2009-09-25)
  [2418] Viv.java uses unchecked or unsafe operations - explanation and cure - (2009-09-24)
  [2323] Java Collection Objects in the java.util package - (2009-08-05)
  [1910] Java - Generics - (2008-11-27)
  [1502] Java, sorting, ArrayList example, generics - (2008-01-11)
  [1062] Java sorting - comparable v comparator - (2007-02-02)
  [42] Do languages change? - (2004-09-08)

C and C based languages - Memory Management
  [4128] Allocating memory dynamically in a static language like C - (2013-06-30)
  [3416] Storing Tcl source code encoded, and running via your own C program - (2011-09-02)
  [3386] Adding the pieces together to make a complete language - C - (2011-08-11)
  [3144] Setting up arrays in C - fixed size at compile time, or dynamic - (2011-01-24)
  [3118] Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02)
  [2848] C course - final course example puts it all together - (2010-07-02)
  [2669] Efficient use of dynamic memory - C and realloc - (2010-03-10)
  [1845] Passing a table from Lua into C - (2008-10-18)
  [1670] Dynamic Memory Allocation in C - (2008-06-09)
  [1589] Dynamic Memory Allocation in C - calloc, realloc - (2008-03-22)
  [1581] What is an lvalue? (Perl, C) - (2008-03-18)
  [1497] Training Season Starts again! - (2008-01-07)

resource index - Deployment
Solutions centre home page

You'll find shorter technical items at The Horse's Mouth and delegate's questions answered at the Opentalk forum.

At Well House Consultants, we provide training courses on subjects such as Ruby, Lua, Perl, Python, Linux, C, C++, Tcl/Tk, Tomcat, PHP and MySQL. We're asked (and answer) many questions, and answers to those which are of general interest are published in this area of our site.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/solutions/general- ... array.html • PAGE BUILT: Wed Mar 28 07:47:11 2012 • BUILD SYSTEM: wizard