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

CC102 Fundamentals of Computer Programming

Fundamentals of
Computer Programming

XENIORITA ALONDRA BIO WEEK 12-13


Introduction to
ARRAYS
“And in such indexes, although small pricks To
their subsequent volumes, there is seen The baby
figure of the giant mass Of things to come. ”
—WILLIAM SHAKESPEARE, Troilus and Cressida
• To be introduced to the array data structure.
• To understand the use of arrays to store, sort and search
lists and tables of values.
• To understand how to declare an array, initialize an array
and refer to individual elements of an array.
• To be able to pass arrays to methods.
• To be able to declare and manipulate multidimensional
arrays.
Introduction to Array
An array is a special kind of object used to store a collection of data. An array
differs from the other objects you have seen in two ways:

❑ All the data stored in an array must be of the same type. For example, you
might use an array to store a list of values of type double that record rainfall
readings in centimeters. Or you might use an array to store a list of objects of
some class called Species that contain the records for various endangered
species.
❑ An array object has only a small number of predefined methods. Because
arrays were used by programmers for many years before classes were
invented, they use a special notation of their own to invoke those few
predefined methods, and most people do not even think of them as methods.
A common way to visualize an Array
DECLARING AND CREATING AN ARRAY
You declare and create an array in almost the same way that you declare And create
objects of classes. There is only a slight difference in the syntax.
SYNTAX:
Base_Type[] Array_Name = new Base_Type[Length];
EXAMPLES:
char[] symbol = new char[80];
Int[] num = new int[20];
double[] reading = new double[100];
Char[] letters = new char[10];
String[] names = new String[5];
DECLARING AND CREATING AN ARRAY
ARRAY DETAILS:
You create an array in the same way that you would create an object of a class type using
the operation new, but the notation is slightly different. When creating an array of
elements of type Base_Type, the syntax is as follows:
Base_Type[] Array_Name = new Base_Type[Length];
For example, the following creates an array named pressure that is equivalent to 100
variables of type int:
int[] pressure = new int[100];
Alternatively, the preceding can be broken down into two steps:
int[] pressure;
pressure = new int[100];
ARRAY DETAILS:
❑ The first step declares as an array of integers. The second step allocates
enough memory for the array to hold up to 100 integers. The type for the
array elements is called the base type of the array. In this example, the base
type is int.
❑ The number of elements in an array is called the length, size, or capacity of the
array. So this sample array pressure has length 100, which means it has
indexed variables pressure[0] through pressure[99]. Note that because the
indices start at 0, an array of length 100, such as pressure, will have no
indexed variable pressure[100].
ACCESSING ARRAY
❑ In Java, an array is a special kind of object, but it is often more useful to think of an array as a
collection of variables of the same type. For example, an array consisting of a collection of six
variables of String can be created as follows:
❑ String[] names = new String[6];
❑ names[0], names[1], names[2], names[3], names[4], names[5]
❑ Variables like names[0] and names[1] that have an integer expression in square brackets are called
indexed variables, subscripted variable, array elements, or simply elements. An index or a
subscript is an integer contained within square brackets that indicates one of an array’s variables,
or elements. Note that the numbering starts with 0, not 1 and last index will always be length -1.
❑ Elements within the array are accessed using square brackets `[ ]` with the index value inside.
❑ String firstName = names[0]; //accessing the first element in `names` array.
POPULATING AN ARRAY
POPULATING AN ARRAY
❑ Arrays can have non-default values assigned during creation.
❑ To initialize an array, use a list of values enclosed in curly braces, separated by
commas.
❑ Example: int[] someNum = {10, 20, 30, 40, 50};
❑ Include a semicolon at the end of the statement.
❑ Populating an array during creation means providing values without explicitly
specifying the size.
❑ The array size is determined by the number of values in the initializing list.
❑ The someNum array in the example has a size of 5.
POPULATING AN ARRAY
USING A FOR LOOP TO ACCESS ARRAY ELEMENTS
ARRAY INDEX
❑ The first element in a Java array has an index of 0, and the last valid index for an array of
length n is n − 1.
❑ For an array named temperature, the last valid index is names.length - 1.
❑ In the array declaration double[] entry = new double[5];, valid indices are 0, 1, 2, 3, and 4.
❑ Any indexed expression, such as entry[n + 2], must result in an index within the valid range
(0 to length - 1).
❑ Using an index outside this range is considered out of bounds or invalid, and it's a common
mistake when working with arrays.
❑ You can obtain the length of an array using the length property. For example, int[] numbers
= {1, 2, 3}; int length = numbers.length;.
USING VARIABLES IN AN ARRAY
❑ Java allocates memory for an array and other objects during execution.
❑ To dynamically determine the array size, you can read the length from the keyboard.
❑ This approach allows flexibility when the array size is not known at the time of program writing.
❑ Reading the array length from the keyboard enables user input to define the size during runtime.
SEARCHING AN ARRAY
SEARCHING AN ARRAY
❑ To find an element in an array, we look through each item in the list and
compare it to what we're searching for.
❑ This process can be done using loops, like for loops or while loops.
❑ After going through the whole array, we use a special switch (a boolean
variable) to indicate whether we found the element or not.
❑ Initially set as "not found" (false), it changes to "found" (true) if we discover the
element.
❑ This method helps us determine if an element exists in the array.
SEARCHING AN ARRAY
SEARCHING AN ARRAY
❑ Similarly we can also use the index to find the element of an array.
❑ We begin by examining each item in the array (numbers).
❑ Using a loop (in this case, a for loop), we traverse through each element in the list.
❑ For each element, we compare it to the value we're looking for (target).
❑ If an element matches the target, we record its index.
❑ If the element is found, we display a message stating the index where it was found. If no
match is found, we print a message indicating the element was not present in the array.
❑ Initially set as -1 (indicating "not found"), it changes to the index if we discover the
element.
❑ This variable helps us confirm the existence of the element within the array.
SEARCHING AN ARRAY
Fundamentals of Computer Programming

The End

XENIORITA ALONDRA BIO WEEK 12


“You all have the talent,
knowledge, and dedication to
achieve all of your goals.”

WEEK 5

You might also like