Arrays IST 311 MIS 307 Arrays Array named

  • Slides: 19
Download presentation
Arrays IST 311 / MIS 307

Arrays IST 311 / MIS 307

Arrays Array – named “collection” of contiguous storage locations, i. e. 100 int values

Arrays Array – named “collection” of contiguous storage locations, i. e. 100 int values – all containing the same type of data – each element in array referred to by position

Arrays int my. Array [ ] = new int [9]; – treats arrays like

Arrays int my. Array [ ] = new int [9]; – treats arrays like objects – must instantiate with new operator; create the name for the array and then the array itself – may use any primitive and reference types to create array: int char, boolean, String, etc. – subscript/index used to access an element in array; must be an integer value Position Data 0 5 1 9 2 3 3 4 4 6 5 10 6 8 7 1 8 2

Arrays my. Array[1]; my. Array[4]; my. Array[15]; int i = 5; my. Array[i]; Position

Arrays my. Array[1]; my. Array[4]; my. Array[15]; int i = 5; my. Array[i]; Position Data 0 5 1 9 2 3 3 4 4 6 5 10 6 8 7 1 8 2

Arrays Each array has a length instance variable which holds the size of the

Arrays Each array has a length instance variable which holds the size of the array for(int i = 0; i < my. Array. length; i++) my. Array[i] = my. Array[i] + 1;

Array Objects When an array of objects is created, the array’s elements are references

Array Objects When an array of objects is created, the array’s elements are references to the objects Must create the array with the new operator Must create the actual array elements

Array Objects String my. Str. Array[ ] = new String [5]; my. Str. Array[0]

Array Objects String my. Str. Array[ ] = new String [5]; my. Str. Array[0] = new String(“Nikki”); my. Str. Array[1] = new String(“Craig”); my. Str. Array[2] = new String(“April”); my. Str. Array[3] = new String(“Matt”); my. Str. Array[4] = new String(“Rishma”);

Array Objects String my. Str. Array[ ] = new String [5]; for (int i

Array Objects String my. Str. Array[ ] = new String [5]; for (int i = 0; i < my. Str. Array. length; i++) my. Str. Array[i] = new String( );

Array Objects String my. Str. Array[ ] = new String [5]; Creates reference to

Array Objects String my. Str. Array[ ] = new String [5]; Creates reference to an array.

Array Objects Value = “ “ Length. Value =0 =““ ““ Length. Value =

Array Objects Value = “ “ Length. Value =0 =““ ““ Length. Value = 0 =Value =““ Length = 0 =““ Length =Value 0 Length = 0 for (int i = 0; i < my. Str. Array. length; i++) my. Str. Array[i] = new String( );

Initializing Arrays Elements are automatically initialized to default values that depend on data type

Initializing Arrays Elements are automatically initialized to default values that depend on data type – Boolean elements are set to false – Integer and real types are set to 0 – Reference types (objects) are set to null

Pass Arrays to Methods public void print. Array(int[ ] array) { for…. . }

Pass Arrays to Methods public void print. Array(int[ ] array) { for…. . } Arrays may be passed to a method as a formal parameter Passed by reference

Inheritance

Inheritance

Inheritance Lets you create new classes from existing classes which are called superclasses or

Inheritance Lets you create new classes from existing classes which are called superclasses or base classes Any new class is called a subclass or derived class Inheritance is viewed as a tree-like or hierarchical structure

Inheritance Private members of a superclass cannot be directly accessed in a subclass Subclass

Inheritance Private members of a superclass cannot be directly accessed in a subclass Subclass may directly access public members of a superclass Subclass may include additional data and methods

Inheritance Subclass may redefine public methods of a superclass All data members of the

Inheritance Subclass may redefine public methods of a superclass All data members of the superclass are also data members of the subclass Constructor of a subclass cannot directly access private data members of a superclass

Inheritance A subclass can give a method the same name as a method in

Inheritance A subclass can give a method the same name as a method in the superclass This is called overriding or redefining Method must have the same: – Name – Type – Parameter list

Inheritance To access an overridden method from within the subclass, use reserve word super

Inheritance To access an overridden method from within the subclass, use reserve word super followed by the dot operator and method name super. print( )

Inheritance Constructors of a subclass can only initialize variables of the subclass Subclass must

Inheritance Constructors of a subclass can only initialize variables of the subclass Subclass must automatically execute one of the constructors of the superclass to initialize the superclass variables