Arrays, Casting & User Defined Types 11 Arrays, Casting & User Defined Types
Previously Types of Errors When errors appear Compiler Errors Run-time Errors Fixing Errors Find Errors
Overview Data Structures Arrays Java Arrays Java Enhanced for loop User Defined Data Types
Data Structures A data structure is an organised collection of related data Related data items stored together Data structures contain more than one item of data Variables stores data Arrays are data structures
Data Structures To represent a car we would use a data structure composed of its characteristics What information related to a car would you store? Colour Maker Registration int, RGB encoding String String
Arrays Group of data items All items of the same type, e.g. int Items accessed by integer indexes Starting index with zero Last index is one less than the length Of pre-determinate size The length of an array is the number of items it contains The length is fixed at creation time
Java Arrays Arrays are variables Arrays must be declared <type>[] <identifier>; or <type> <identifier>[]; Examples: String[] astrCityNames; or String astrCityNames[]; int[] aiAmounts; or int aiAmounts[]; Variable identifiers always start by lowercase letters, ‘_’, ‘$’ or ‘£’ (camel format)
Java Arrays Access to the size of the array by using length astrCityNames.length Trying to access items out of the size of the array will throw an exception ArrayIndexOutOfBoundsException of type IndexOutOfBoundsException
What is the length of the above array? Java Arrays Arrays initialisation Initialised when declared String[] astrCityNames = {"Valencia", "Madrid", "London", "Rome"}; What is the length of the above array? 4
Java Arrays Declare, create and initialise // Declaration String[] astrCityNames; // Creation astrCityNames = new String[4]; // Initialisasion astrCityNames[0] = "Valencia"; astrCityNames[1] = "Madrid“; astrCityNames[2] = "London“; astrCityNames[3] = "Rome";
Java Arrays Arrays when created are automatically initialised to default values Primitives to zero int[] aiAmounts = new int[2]; System.out.println("Amount at 0 is " + aiAmounts[0]); System.out.println("Amount at 1 is " + aiAmounts[1]); Amount at 0 is 0 Amount at 1 is 0
Java Arrays Arrays when created are automatically initialised to default values (continuation) Objects to null String[] astrCityNames = new String[2]; System.out.println("City at 0 is " + astrCityNames[0]); System.out.println("City at 1 is " + astrCityNames[1]); City at 0 is null City at 1 is null
Lets do an exercise for arrays in Eclipse Java Arrays Lets do an exercise for arrays in Eclipse
Java Enhanced for loop Enhanced for loop Java 1.5 or higher Simplifies looping through a collection or array String[] astrNames = {"Adam", "Amadeo", "John"}; for (String strName : astrName) { System.out.println(strName); } // end for String astrNames astrName Adam Amadeo John
Java Enhanced for loop Previous is equivalent to String[] astrNames = {"Adam", "Amadeo", "John"}; for (int iIndex = 0; iIndex < astrName.length(); ++iIndex) { String strName = astrName[iIndex]; System.out.println(strName); } // end for
User Defined Data Types A data structure Contains different data types Example of car Colour Maker Registration Lets create the car data type! int, RGB encoding String String
User Defined Data Types class Car { // Member variables int miColour; // RGB String mstrMaker; String mstrRegistration; } // end class Car Member variables if not explicitly initialised then they are initialised to default values by Java Initialisation can be done at declaration time
User Defined Data Types class Car { // Member variables int miColour; // RGB String mstrMaker; String mstrRegistration; public static void main(String[] astrArgs) { Car myCar; // define myCar = new Car(); // create instance of the data type myCar.miColour = 0xff0000; // red - rrggbb myCar.mstrModel = "Ferrari"; } // end main() } // end class Car My car is red and it is a Ferrari I WISH! Comments should exist but we don’t have space in the presentation
User Defined Data Types Lets build the car project in Eclipse!