Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

WEEK 9: ONE-DIMENSIONAL ARRAY

ESSENTIAL KNOWLEDGE

There will be FIVE sections in this week’s learning:

1. One-Dimensional Array
2. Processing One-Dimensional Array
3. Arrays with Methods
4. Array of Objects
5. Foreach Loop

1. One-Dimensional Array

• Definition: structured data type with a fixed number of elements


• Elements of an array are also called components of the array
• Every element is of the same type
• Elements are accessed using their relative positions in the array

• intExp = number of components in array >= 0


• 0 <= indexExp <= intExp
• In Java, [ ] is an operator called the array subscripting operator
• Example
Array num;
int[] num = new int[5];

• If we run the command


Int [ ] List = new int [10];

• List [5] = 34;

• List [3] = 10;


List [6] = 35;
List [5] = List [3] + List [6];
• You can also declare arrays as follows:
final int ARRAY_SIZE = 10;
int[ ] list = new int[ARRAY_SIZE];

• Example

• Arrays can also be initialized during declaration

• The initializer list contains values, called initial values, that are placed between braces and
separated by commas
• sales[0]= 12.25, sales[1]= 32.50, sales[2]= 16.90, sales[3]= 23.00, and sales[4]= 45.68
• When declaring and initializing arrays, the size of the array is determined by the number of
initial values within the braces
• If an array is declared and initialized simultaneously, we do not use the operator new to
instantiate the array object

• Associated with each array that has been instantiated, there is a public (final) instance variable
length
• The variable length contains the size of the array
• The variable length can be directly accessed in a program using the array name and the dot
operator
int[] list = {10, 20, 30, 40, 50, 60};
• This statement creates the array list of six components and initializes the components using the
values given
• Here list.length is 6

• int[] numList = new int[10];


• This statement creates the array numList of 10 components and initializes each component to
0
• The value of numList.length is 10
numList[0] = 5;
numList[1] = 10;
numList[2] = 15;
numList[3] = 20;

• These statements store 5, 10, 15, and 20, respectively, in the first four components of numList
• You can store the number of filled elements, that is, the actual number of elements, in the array
in a variable, say numOfElement
• It is a common practice for a program to keep track of the number of filled elements in an array

2. Processing One-Dimensional Arrays

• Loops used to step through elements in array and perform operations

• Some operations on arrays


o Initialize
o Input data
o Output stored data
o Find largest/smallest/sum/average of elements

• Code to initialize array to specific value


• Code to read data into array

• Code to print array

• Code to find sum and average of array

• Code to determine the largest element


• Array index out of bounds
o Array in bounds if:
o 0 <= index <= arraySize – 1
o If index < 0 or index > arraySize:
o ArrayIndexOutOfBoundsException exception is thrown
o Base address: memory location of first component in array

3. Arrays with methods


• A general syntax to declare an array as a formal parameter
dataType[] arrayName

• Assignment Operators and Arrays


• If we copy the lists with the command listB=listA;

This is shallow copying.


• The following needs to be executed:

This is called deep copying.

• Relational Operators and arrays


• if (listA == listB)...
o The expression listA == listB determines if the values of listA and listB are the same
and thus determines whether listA and listB refer to the same array
o To determine whether listA and listB contain the same elements, you need to compare
them component by component
o You can write a method that returns true if two int arrays contain the same elements
• Arrays as parameter methods
• Search for a specific item in an array

o Suppose that you want to determine whether 27 is in the list


o First you compare 27 with list[0]
o Because list[0] ≠ 27, you then compare 27 with list[1]
o Because list[1] ≠ 27, you compare 27 with list[2]; because list[2] = 27, the search
stops
o This search is successful

4. Array of Objects
• Can use arrays to manipulate objects
• Example: create array named array1 with N objects of type T
T[] array1 = new T[N]

• Can instantiate array1 as follows:


for (int j = 0; j <array1.length; j++)
array1[j] = new T();
• Arrays and variable length parameter list
o The syntax to declare a variable length formal parameter (list) is:
dataType ... identifier

• A method can have both a variable length formal parameter and other formal parameters;
consider the following method heading:
public static void myMethod(String name, double num, int ... intList)

• The formal parameter name is of type String, the formal parameter num is of type double, and
the formal parameter intList is of variable length
• The actual parameter corresponding to intList can be an int array or any number of int
variables and/or int values
• A method can have at most one variable length formal parameter
• If a method has both a variable length formal parameter and other types of formal parameters,
then the variable length formal parameter must be the last formal parameter of the formal
parameter list

5. Foreach Loop

• The syntax to use this for loop to process the elements of an array is:
for (dataType identifier : arrayName)
statements

• identifier is a variable, and the data type of identifier is the same as the data type of the array
components

• DO NOT USE this when you need to assign a value to an element. This loop can only be
used to access elements.

• DO NOT USE this when you need to compare two arrays in a situation

You might also like