You are on page 1of 23

Kwame Nkrumah University of

Science & Technology, Kumasi, Ghana

COE 351
OBJECT ORIENTED PROGRAMMING:

Java: Arrays & ArrayList

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

Declaring and Creating Arrays


Ø Array objects occupy space in memory
Ø Like other objects, arrays are created with keyword
new
Ø  To create an array object, you specify the type of the
array elements and the number of elements as part
of an array-creation expression that uses keyword
new
Ø  The following declaration and array-creation
expression create an array object containing 12 int
elements and store the array’s reference in array
variable c: int c [] = new int [12]; 3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana

Declaring and Creating Arrays…


Ø This expression can also be performed in two steps
int c [] = new int [12];

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

Declaring and Creating Arrays…


Ø  When only one variable is declared in each
declaration, the square brackets can be placed
either after the type or after the array variable
name, as in:
For example:
String[] b = new String[ 100 ]; // create array b
String[] x = new String[ 27 ]; // create array x
the
String b [] = new String[ 100 ]; // create array b same
String x [] = new String[ 27 ]; // create array x
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:

int intArray [];

if( intArray == null)

{ System.out.println("yes"); }

else { System.out.println("no"); }
This causes yes to be displayed.
3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana

Creating and Initializing an Array


public class InitArray {

public static void main( String[] args ) {


int [] array; // declare array named array
array = new int[ 5 ]; // create the array object
System.out.printf( "%s%8s\n", "Index", "Value" )
for ( int counter = 0; counter < array.length; counter++ )
System.out.printf( "%5d%8d\n", counter,
array[ counter ] );
} 3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana

Creating and Initializing an Array …



Output:
Index Value
0 0
1 0
2 0
3 0
4 0

3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana

Using an Array Initializer


Ø  You can create an array and initialize its elements
with an array initializer a comma-separated list of
expressions enclosed in brace. For example, creates
a five-element array with index values 0–4
int [] n ={ 10,20,30,40,50};
Element n[0] is initialized to 10, n[1] is initialized to 20,
and so on

3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana

Using an Array Initializer…



public class InitArray {

public static void main( String[] args ) {

int [] array = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };

System.out.printf( "%s%8s\n", "Index", "Value" );

for ( int counter = 0; counter < array.length; counter++ )

System.out.printf( "%5d%8d\n", counter, array[ counter ] ); }


3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana

Using an Array Initializer…



output:

3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana

Using Command-Line Arguments


Ø It’s possible to pass arguments from the command line

(these are known as command-line arguments)


Ø First encounter with arrays:
§  public static void main( String [] args )
§  (i.e., an array of Strings) in the parameter list of main
§  When an application is executed using the java
command, Java passes the command-line arguments that
appear after the class name in the java command to the
application’s main method as Strings in the array args 3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana

Using Command-Line Arguments…


Ø The number of command-line arguments is obtained by

accessing the array’s length attribute.


Ø Command-line arguments are separated by white space,
not comma

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

time to accommodate additional elements


Ø  The collection class ArrayList <T> (from package java.util)
provides a convenient solution to this problem
Ø It can dynamically change its size to accommodate more
element
Ø The T (by convention) is a placeholder
Ø When declaring a new ArrayList, replace T with the type
of elements that you want the ArrayList to hold 3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana

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

ArrayList common Methods


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

You might also like