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))
Java is a dynamic language .... (and comparison)

Many older programmers are used to setting up an array of a fixed size and saying "that's fixed" ... for the duration of the program, if not for all time until the application is recompiled.

Languages such as C gave us malloc, calloc and realloc (and things have progress on somewhat in C++) to let us dynamically set up arrays, and languages like Perl and Python have move on to lists which are totally different structures. PHP uses the word "array" but really they are lists!

An array (in computer science terms) is defined as sequential memory locations and so by definition if you run off the end of it, you're going to be "out of bounds" and something nasty will happen. A list is defined as a series of items linked on to each other in sequence, which can be extended later.

What about Java?

Java uses arrays. However, they can be dynamically allocated so that the size can be controlled at run time, and all Java arrays are objects which means that you can re-allocate a name that you've already used to a new block of memory or another differing array as your program runs - thus getting a far more dynamic effect.

Example?


Let's calculate a result that processes an array to produce a new and longer array many times over.

Initialization and setup:

int [] previous, current;
previous = new int[0];


The names current and previous are set up in the first line, but with no particular size. Then previous is set to an empty array with no elements, basically to keep the compiler happy.

Let's now run the iteration loop:

for (int k=0; k<rows; k++) {
current = new int[previous.length+1];


We're going through the loop as many times as necessary, setting up the current row to be one longer than the previous.

At this point, calculations to generate current from previous!

Finally, set "previous" to be the array named in "current". The old previous will be lost and the memory released collected up in due course by the garbage collector, and we can run around the loop again to calculate the next row.

previous = current;
}


This "moving up" is cheap - we're only moving up a reference or pointer - a single memory address - and not what could be a major piece of storage. We're renaming a town, not building a complete new one if you like!

There's a complete program example here which shows Pascal's triangle being generated with this dynamic technique.

For even greater flexibility, see java.lang.util
(written 2008-01-08, updated 2008-01-09)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
J705 - Java - Arrays
  [1497] Training Season Starts again! - (2008-01-07)
  [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)
  [3118] Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02)
  [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)


Back to
Training Season Starts again!
Previous and next
or
Horse's mouth home
Forward to
Climate change, renewable resources and paper v plastic
Some other Articles
Java, sorting, ArrayList example, generics
Java - using super to call a method in the parent class
First Class Java. First step and moving forward.
Climate change, renewable resources and paper v plastic
Java is a dynamic language .... (and comparison)
PHP / Web 2 logging
Single login and single threaded models - Java and PHP
A time to update pictures
Comparison - cellpadding versus cellspacing
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.info/mouth/1498_Jav ... ison-.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb