Professional Documents
Culture Documents
COE 351
OBJECT ORIENTED PROGRAMMING:
1
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
Lecture Outline
Ø Array
Ø Declaring and Creating Array
Ø Using Arrays
Ø Using Command-Line Arguments
Ø ArrayList
§ Declaring ArrayList
§ Using ArrayList
2
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
Arrays
Ø Data structures consisting of related data items of the
same type
Ø Arrays in Java are actually objects so they’re
considered reference types. Arrays found in java.util
package
Ø Arrays can be made of entities of any type: objects or
primitive types
Ø Variables contain in an array are called elements or
components
Ø The position number of the element is called the
element’s index or subscript 3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
Arrays..
Ø A program refers to any one of these elements with an
array-access expression that includes the name of the
array followed by the index of the particular element in
square brackets ([])
Ø The first element in every array has index zero and is
sometimes called the zeroth element
3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
Arrays..
Example
3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
Arrays..
Ø Thus, the elements of array c are c[0], c[1], c[2] and
so on
Ø The highest index in array c is 11, which is 1 less than
12—the number of elements in the array
Ø An index must be a nonnegative integer
Ø Every array object knows its own length and stores it
in a length instance variable
Ø For example, the expression c.length accesses array
c’s length field to determine the length of the array
3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
int [] c;
c=new int[ 12 ];
In the declaration, the square brackets following the
type indicate that c is a variable that will refer to an
array(i.e., the variable will store an array reference ).
In this code, second line causes an array of 12 int’s to
be created 3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
Using Arrays
Ø As Arrays are objects, this just creates the array variable: it
does not actually create an array.
Ø Thus the code:
{ System.out.println("yes"); }
else { System.out.println("no"); }
This causes yes to be displayed.
3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
int [] array = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };
3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
Example:
3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
ArrayList
Ø Arrays do not automatically change their size at execution
ArrayList…
Ø Only non-primitive types can be used with these collection
classes
For example:
ArrayList< String > list;
This declares list as an ArrayList collection that can store only
Strings.
Ø Array lists are created with an initial size. When this size is
exceeded, the collection is automatically enlarged. When
objects are removed, the array may be shrunk.
3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
ArrayList
Example:
3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
References:
1. Java: How to Program, 9th ed., P. Deitel, H. Deitel,
Prentice Hall, 2012
2. Head First: Object-Oriented Analysis and Design,
D. Brett et al., O’Reilly, 2007
3. Programming and Problem Solving with Java, N.
Dale, C. Weems, M. Headington, Jones and Bartlett
Publishers, Inc., 2003
4. https://www.tutorialspoint.com 3