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 Utility class - flexible replacement for array. Also cacheing in objects and multiple catch clauses example.

In Java, utility class objects are used as flexible alternatives to arrays to hold collections of other obejcts. ArrayLists and Vectors hold an ordered (indexed) set of other objects, but due to the structure used in memory, there's no need to know how many items there are going to be in the collection when you start. So that makes them excellent for appications which read from a stream of data (file, keyboard, socket, URL etc) and store all (or some) or the data read (typically as objects for later processing.

From this week's Java course, an example [here] in which I've commented out much of the code that I used for an earlier iteration (with an array) and replaces it with a Vector. You'll note that I've had to move away from the [] notation that's used with arrays, and move to methods so that (for example)
  places[nStations] = current;
is replaced by
  places.addElement(current);

Although this might look like a retrograde step if you're doing straightforward code conversion, if you refactor as you more on to a vector you'll find other savings such as the saving of the need to keep count of the number of elements you have in your collection. With an array you had the number of elements occupied to keep a track of yourself AS WELL AS the separate length (= capacity) within the program. Once you've moved on to a vector, the size() method tells you all.

The Station class used in the example mayt be found [here]. Note that I've used this to illustrate the caching of calculation results - a simple division in this case but it could have been complex calculations and database and other resource references we didn't want to repeat. The caching design pattern allows for the calculation to be saved from repitition whether you have an application that creates huge numbers of objects but only calculates from a few, or creates a few objects and calculates (or at least enquires of) the all many times.

Finally, note that the exception handler in the main code catches two different types of exceptions in two different ways - a good example of the need for multiple catches. Well, if you look carefully it was an excellent example in an earlier iteration of my appliaction when I was using an array - in the latest code as the end of my demonstration, the replacement of the fixed length array with a Vector has meant that there's no longer any need to catch an overflow ... the vector expamds dynamically!

As an aside, another slight "down" on Vectors, ArrayLists, etc, is that they can only hold objects not primitives. In practical use, it turns out not to be a huge problem as you're likely to be handling collections of objects for the most part anyway. And if you really want to hold ints,for example, you could always hold Integer objects.

(written 2015-01-16, updated 2015-01-17)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Q908 - Object Orientation and General technical topics - Object Orientation: Design Patterns
  [485] North, Norther and Northest - PHP 5 Objects - (2005-11-04)
  [1224] Object Relation Mapping (ORM) - (2007-06-09)
  [2322] Looking for a practical standards course - (2009-08-05)
  [2741] What is a factory? - (2010-04-26)
  [2977] What is a factory method and why use one? - Example in Ruby - (2010-09-30)
  [3608] Design Patterns - what are they? Why use them? - (2012-02-12)
  [3716] Learning C++ - a design pattern for your first class - (2012-05-02)
  [3810] Reading files, and using factories to create vectors of objects from the data in C++ - (2012-07-21)
  [3843] Caching Design Patterns - (2012-08-20)
  [4021] Spike solution, refactored and reusable, Python - Example - (2013-02-28)
  [4096] Perl design patterns example - (2013-05-20)
  [4098] Using object orientation for non-physical objects - (2013-05-22)
  [4325] Learning to program - what are algorithms and design patterns? - (2014-11-22)
  [4330] Java - factory method, encapsulation, hashmap example - (2014-11-27)
  [4356] Object factories in C++, Python, PHP and Perl - (2014-12-19)
  [4359] How to avoid too many recalculations within an object - (2014-12-21)
  [4377] Designing a base class and subclasses, and their extension, in C++ - (2015-01-01)
  [4421] How healthy are the stars of stage and screen? - (2015-02-09)
  [4581] Thin application, thick objects - keep you main code simple. Example in Ruby - (2015-11-21)
  [4626] Singleton design pattern - examples and uses - (2016-01-20)
  [4663] Easy data to object mapping (csv and Python) - (2016-03-24)
  [4673] Separating detailed data code from the main application - Ruby example - (2016-05-16)

J712 - Java - Exceptions
  [1066] Final, Finally and Finalize - three special words in Java - (2007-02-05)
  [1875] What are exceptions - Python based answer - (2008-11-08)
  [2420] Exceptions in Java - why and how - (2009-09-24)
  [2622] Handling unusual and error conditions - exceptions - (2010-02-03)
  [2862] Fail Safe Error Handling in Java via Exceptions - (2010-07-09)
  [3045] After Course Resources - do we publish sample answers. Example from Java Exceptions module. - (2010-11-13)
  [3048] String handling - from first steps to practical examples - (2010-11-13)
  [4350] Keyboard reader for Java programming newcomers - (2014-12-12)

J714 - Java - Fundamental classes
  [42] Do languages change? - (2004-09-08)
  [1062] Java sorting - comparable v comparator - (2007-02-02)
  [1502] Java, sorting, ArrayList example, generics - (2008-01-11)
  [1910] Java - Generics - (2008-11-27)
  [2323] Java Collection Objects in the java.util package - (2009-08-05)
  [2418] Viv.java uses unchecked or unsafe operations - explanation and cure - (2009-09-24)
  [2421] Sorting Collections of Objects in Java - (2009-09-25)
  [2649] Length, size or capacity in Java? - (2010-02-24)
  [2734] for and foreach in Java - (2010-04-22)
  [2920] Sorting - naturally, or into a different order - (2010-08-14)
  [4431] A Java servlet that is also a stand alone program. And a server that is also a web client. - (2015-02-19)


Back to
Preparing data through a little bit of Perl
Previous and next
or
Horse's mouth home
Forward to
TransWilts / Press and Publicity report for AGM / 30th January 2015
Some other Articles
Commenting out an echo killed my bash backup script
The Campus in Melksham - name, logo, and livery choice
Accessing variables across subroutine boundaries - Perl, Python, Java and Tcl
TransWilts / Press and Publicity report for AGM / 30th January 2015
Java Utility class - flexible replacement for array. Also cacheing in objects and multiple catch clauses example.
Preparing data through a little bit of Perl
Philosophy behind object design - and how I applied in to a Java example
Splitting Java strings and extracting numbers
Interview conditions,and other instructions to staff
Refactoring Perl applications to give them a rosy future
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/4396_Jav ... mple-.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb