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 in Java - an introduction for newcomers

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)


Back to
A behaviour driven example of writing a Java program
Previous and next
or
Horse's mouth home
Forward to
Taking my life in my hands in Swansea
Some other Articles
A little thing can make a big difference
Keyboard reader for Java programming newcomers
When I am old, what will worry me?
Taking my life in my hands in Swansea
Arrays in Java - an introduction for newcomers
A behaviour driven example of writing a Java program
Incrementing a variable in Java - Pre and Post Increment
Python base and inherited classes, test harness and unit testing - new examples
Politics and the railway the connects Wiltshire
My first official measure - Passenger Entrys and Exits - revisited 9 years later
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/4347_Arr ... omers.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb