In Java, arrays are
objects but with some extra syntax which gives you additional ways to access them, for example using a traditional subscripting notation. From the
Java Course I am running this week, an example written in front of my delegates is
[here].
Setting up an array ...
float [] delays;
delays = new float[WEEKLENGTH];
The first line creates an object reference - that's a variable that can hold an address where object data is stored, and an indicator of the type of data to be found there. It's only going to be a few bytes long, no matter how big the actual data array turns out to be, and it doesn't hold even a single
float - just a pointer to a whole series - one or more - floats stored somewhere else (and also stored at that "elsewhere" is management information such as the length / number of elements in that stored data.
The second line actually allocates storage ("creates") the array, probably held on the heap, sized to be able to store WEEKLENGTH values of type float plus management data. The reference to that storage (its address) is stored in the variable called
delays that was created in the first line.
Referencing an array ...
Array elements are numbered from zero up ... so an array of 10 elements has elements numbered 0 to 9 (not 10!) and individual elements are referenced by using the array name forllowed by the position you want in square brackets directly after the name - the whole thing being uses in just the same was as if it was a regular variable of the same type. For example:
delays[k] = wellreader.read_number();
If you want to refer to the whole of an array, for example to pass it into another named block of code, you'll simply use the name - so for example:
float avg = average(delays);
and you'll then "pick up" the array in the named block of code (method) by declaring its type and specifiying the name that's to be used for it within the method:
public static float average(float [] samples) {
You can find out how long an array is by looking at the length variable within the array object - for example, here is how I would step through every element of the array we're using as our example within the function
for (k=0; k<samples.length; k++) {
You need to be very careful indeed here - you are looking at the amount of storage that's been allocated to the array, and not necessarily the number of elements that you've used ... you may have used less. If that's the case, you need to keep track of the number of elements used yourself, or add a key "cardinal value" onto the end so that you can stop processing the array when you get to that point. If you're going to do this, remember to allow one extra element ho told that terminsation value, an choose something that will
never occur in the data stream!
There is no danger in Java (unlike in C) of overrrunning past the end of the array, as you'll get an exception thrown and not be allowed if you try!
Data with two names at the same time ...
Please note that in Java, the data in the array is NOT copied into a function when you call it - it's just te reference (the pointer) that's passed into the function. This means
• if you alter the contents of the array in the function, your changes will be see outside too
• passing an array is very efficient with just a few bytes moved around even for a huge structure
Once you realise this is happening, it's wonderful. Before you understand, it can be a big fright.
Alternatve collections ...
If an array doesn't have the flexibility you need - if you want to be able to dynamically expand it, for example, of if you want an efficient way to remove an element from the middle and move up the rest, take a look at Java's Utility classes and classes like an ArrayList - see
[here] for further articles and examples of this. On this week's course, I'll introduce those classes on Thursday and it's now just Tuesday evening!
(written 2014-12-10, updated 2014-12-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)
[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)
[3118] Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02)
[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)
Some other Articles
A little thing can make a big differenceKeyboard reader for Java programming newcomersWhen I am old, what will worry me?Taking my life in my hands in SwanseaArrays in Java - an introduction for newcomersA behaviour driven example of writing a Java programIncrementing a variable in Java - Pre and Post IncrementPython base and inherited classes, test harness and unit testing - new examplesPolitics and the railway the connects WiltshireMy first official measure - Passenger Entrys and Exits - revisited 9 years later