Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Arrays of arrays - or 2D arrays. How to program tables.

It's shorthand when you're learning a new programming language to ask "how does it handle multiple dimension arrays" ... but in practise very few languages actually support true multidimensional arrays these days, and in those which do you might be well advised to use an alternative that's more flexible.

What is a true - proper - two dimensional array? It's a series of successive locations in the computer memory which represents a rectangular table. For example, I could have each 'row' representing a room at our hotel (so that's 1 to 5) and each 'column' representing a day of the month (1 to 31 this month). So that would be 155 memory locations, each of which might contain a number which is the amount we're charging for that room on that night.
  Row 1, Column 1. Cell 1. 95.00 (1st of month, room 1)
  Row 2, Column 1. Cell 32. 85.00 (1st of month, room 2)
  Row 4, Column 10. Cell 103. 85.00 (10th of month, room 4)

Where you need a fixed number of columns in each row, this system can work very well ... but it turn out that this is rarely the case. If you have a separate row for each member of your staff, and then columns to hold the names of their children, you'll find yourself having to decide very early on (perhaps far too early) what the maximum number of children should be, and then you'll find that you've often got a very great deal of wasted memory. We used to have a staff member with six children, and we have a delegate on a course a couple of years back who was one of 22 siblings - just think how much memory would have to be put aside "just in case" if we were to use a rectangle system, where every row must allow for the same number of columns. Diagram - a true two dimensional array, showing how only a few of the cells are actually used


Let me describe the better alternative - and that is an array of arrays (or a list of lists). In this case, you'll have an array with just a single dimension which holds the memory address at which the actual data for each column starts. In the case of my rooms / rate each day, this first single-dimensioned array would perhaps contain:
  Row 1 starts at Memory address 1000
  Row 2 starts at Memory address 2000
  Row 3 starts at Memory address 2400 etc
and then the size of each of the arrays that they point to can differ. In the case of days of the month, it's unlikely to make any great difference to the 2 dimensional array (in fact it may be marginally less efficient), but in the majority of cases - of which my staff member example is just one - you'll save huge amounts of space.

Languages such as C and C++, in their fundamental form, use true two dimensional arrays, but using functions such as malloc (in C) and heap objects (C++) you can use the "array of arrays" model instead. Not only does it make for a saving of space, but it also means that memory can be dynamically allocated as you write your program, rather than you having to make a decision as to what maximum you should allow at some point in the development process.Diagram - a list of lists showing the saving of memory and the extra flexibility


With most of the other languages that we teach, you'll actually use arrays of arrays all the time - although the syntax used is very often such that it looks like you are using true two dimensional structures. There's an example of that [here] in PHP, and one [here] in Perl. There's one [here] in Java and one [here] in Python.

((There's an example of a true two dimensional array in C [here] and various C examples that use malloc [here].))
(written 2011-01-02, updated 2011-01-03)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Y104 - Python - Lists and Tuples
  [383] Overloading of operators on standard objects in Python - (2005-07-19)
  [657] The ternary operator in Python - (2006-03-25)
  [899] Python - extend v append on a list - (2006-10-20)
  [955] Python collections - mutable and imutable - (2006-11-29)
  [1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06)
  [1641] Tektronix 4010 series / Python Tuples - (2008-05-13)
  [1789] Looking for a value in a list - Python - (2008-09-08)
  [2280] Creating and iterating through Python lists - (2009-07-12)
  [2284] Strings as collections in Python - (2009-07-12)
  [2368] Python - fresh examples of all the fundamentals - (2009-08-20)
  [2719] Traffic lights in Python - (2010-04-13)
  [2996] Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12)
  [3181] Beware - a=a+b and a+=b are different - Python - (2011-02-23)
  [3257] All possible combinations from a list (Python) or array (Ruby) - (2011-04-23)
  [3348] List slices in Python - 2 and 3 values forms, with an uplifting example - (2011-07-06)
  [3669] Stepping through a list (or an array) in reverse order - (2012-03-23)
  [3763] Spike solutions and refactoring - a Python example - (2012-06-13)
  [4027] Collections in Python - list tuple dict and string. - (2013-03-04)
  [4368] Shuffling a list - Ruby and Python - (2014-12-28)
  [4722] Embedding more complex code into a named block - (2016-11-04)

P217 - Perl - More than Simple Lists and Hashes!
  [43] Hash of lists in Perl - (2004-09-09)
  [293] Course follow-ups - (2005-04-27)
  [1514] Autovivification - the magic appearance of variables in Perl - (2008-01-21)
  [2241] Perl references - $$var and \$var notations - (2009-06-15)
  [2840] Just pass a pointer - do not duplicate the data - (2010-06-30)
  [2877] Further more advanced Perl examples - (2010-07-19)
  [3007] Setting up a matrix of data (2D array) for processing in your program - (2010-10-21)
  [3072] Finding elements common to many lists / arrays - (2010-11-26)
  [3105] Adventure with references to lists and lists of references - (2010-12-26)
  [3399] From fish, loaves and apples to money, plastic cards and BACS (Perl references explained) - (2011-08-20)
  [3406] Not multidimentional arrays - but lists of lists. Much more flexible. Perl! - (2011-08-26)
  [3444] Take the dog on a lead - do not carry her. Perl references. - (2011-09-17)
  [3577] How to do multidimensional arrays (or rather lists and hashes) in Perl - (2012-01-14)
  [3906] Taking the lead, not the dog, for a walk. - (2012-10-28)

J705 - Java - Arrays
  [1497] Training Season Starts again! - (2008-01-07)
  [1498] Java is a dynamic language .... (and comparison) - (2008-01-08)
  [1614] When an array is not an array - (2008-04-17)
  [2648] Java arrays - are they true arrays or not? - (2010-02-23)
  [3038] Setting up individual variables, and arrays, in Java - some commented examples - (2010-11-09)
  [3039] Fresh Paint - Java Arrays - (2010-11-09)
  [4347] Arrays in Java - an introduction for newcomers - (2014-12-10)
  [4413] Binomial Coefficient (Pascal Triangle) objects in Java - (2015-02-03)
  [4428] Using the lead - passing arrays and other collections in Java - (2015-02-16)

H999 - Additional PHP Material
  [54] PHP and natural sorting - (2004-09-19)
  [239] What and why for the epoch - (2005-03-08)
  [320] Ordnance Survey - using a 'Get a map' - (2005-05-22)
  [322] More maps - (2005-05-23)
  [337] the array returned by preg_match_all - (2005-06-06)
  [372] Time calculation in PHP - (2005-07-08)
  [468] Stand alone PHP programs - (2005-10-18)
  [483] Double Dollars in PHP - (2005-11-02)
  [493] Running a Perl script within a PHP page - (2005-11-12)
  [563] Merging pictures using PHP and GD - (2006-01-13)
  [603] PHP - setting sort order with an associative array - (2006-02-13)
  [665] PHP Image viewing application - (2006-04-01)
  [687] Presentation, Business and Persistence layers in Perl and PHP - (2006-04-17)
  [789] Hot answers in PHP - (2006-07-02)
  [806] Check your user is human. Have him retype a word in a graphic - (2006-07-17)
  [822] PHP - a team member leaves - (2006-08-04)
  [839] Reporting on the 10 largest files or 10 top scores - (2006-08-20)
  [917] Syntax checking in PHP - (2006-11-07)
  [937] Display an image from a MySQL database in a web page via PHP - (2006-11-22)
  [1010] Dates, times, clickable diarys in PHP - (2006-12-28)
  [1020] Parallel processing in PHP - (2007-01-03)
  [1053] Sorting people by name in PHP - (2007-01-26)
  [1104] Drawing dynamic graphs in PHP - (2007-03-09)
  [1194] Drawing hands on a clock face - PHP - (2007-05-19)
  [1270] PHP Standalone - keyboard to screen - (2007-07-18)
  [1389] Controlling and labelling Google maps via PHP - (2007-10-13)
  [1390] Converting from postal address to latitude / longitude - (2007-10-13)
  [1391] Ordnance Survey Grid Reference to Latitude / Longitude - (2007-10-14)
  [1451] More PHP sample and demonstration programs - (2007-12-01)
  [1485] Copyright and theft of images, bandwidth and members. - (2007-12-26)
  [1505] Script to present commonly used images - PHP - (2008-01-13)
  [1519] Flipping images on your web page - (2008-01-26)
  [1623] PHP Techniques - a workshop - (2008-04-26)
  [2073] Extra PHP Examples - (2009-03-09)
  [2215] If nothing, make it nothing. - (2009-06-02)
  [2684] Exception handling in PHP - (2010-03-18)
  [3210] Catchable fatal error in PHP ... How to catch, and alternative solutions such as JSON - (2011-03-22)
  [4655] Image indexer / thumbnail display scripts in PHP - (2016-02-25)

C212 - C and C based languages - Memory Management
  [1581] What is an lvalue? (Perl, C) - (2008-03-18)
  [1589] Dynamic Memory Allocation in C - calloc, realloc - (2008-03-22)
  [1670] Dynamic Memory Allocation in C - (2008-06-09)
  [1845] Passing a table from Lua into C - (2008-10-18)
  [2669] Efficient use of dynamic memory - C and realloc - (2010-03-10)
  [2848] C course - final course example puts it all together - (2010-07-02)
  [3144] Setting up arrays in C - fixed size at compile time, or dynamic - (2011-01-24)
  [3386] Adding the pieces together to make a complete language - C - (2011-08-11)
  [3416] Storing Tcl source code encoded, and running via your own C program - (2011-09-02)
  [4128] Allocating memory dynamically in a static language like C - (2013-06-30)
  [4340] Simple C structs - building up to full, dynamic example - (2014-12-03)
  [4634] Regression testing - via a very short C testing framework - (2016-01-29)
  [4635] Encapsulating logic in functions and structs - the C approach to Object Oriented techniques - (2016-01-30)

C205 - C and C based languages - Arrays
  [2002] New C Examples - pointers, realloc, structs and more - (2009-01-20)
  [3121] New year, new C Course - (2011-01-05)
  [3245] Collections in C and C++ - arrays, vectors and heap memory blocks - (2011-04-12)
  [4338] Passing arrays into functions in C - (2014-12-02)
  [4566] C - why is slow to write and debug) but fast to run? - (2015-11-01)


Back to
Opportunities for Melksham - new businesses in the town
Previous and next
or
Horse's mouth home
Forward to
TransWilts - some things to see and do
Some other Articles
When is a program complete?
Learning to write good programs in C and C++ - separating out repeated code
TransWilts - some things to see and do
Arrays of arrays - or 2D arrays. How to program tables.
Opportunities for Melksham - new businesses in the town
Are there newspapers on New Years Day? Do the shops open on Easter Sunday? Do trains run at Christmas?
Perl, Python, PHP, Lua, Linux, and more - and business hotel too. Menu for 2011
Transwilts Link - both Wiltshire and beyond
Trowbridge and Melksham to Chippenham - more roadworks, even slower journey over the winter
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

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

PAGE: http://www.wellho.net/mouth/3118_Arr ... bles-.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb