0% found this document useful (0 votes)
31 views

Arrays: Called Initial Values, That Are Placed

The document discusses various aspects of arrays in Java including how to declare, initialize, access elements, and perform common operations on arrays. It covers one-dimensional and multi-dimensional arrays. Various methods for processing arrays are also presented such as passing arrays as parameters to methods, using loops to iterate through elements, and determining the largest/smallest element in an array.

Uploaded by

KeziahMercado
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Arrays: Called Initial Values, That Are Placed

The document discusses various aspects of arrays in Java including how to declare, initialize, access elements, and perform common operations on arrays. It covers one-dimensional and multi-dimensional arrays. Various methods for processing arrays are also presented such as passing arrays as parameters to methods, using loops to iterate through elements, and determining the largest/smallest element in an array.

Uploaded by

KeziahMercado
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

• Java Programming: From Problem • Array num:

Analysis to Program Design, 3e int[] num = new int[5];

• Array List
Chapter 9
• Array List (continued)
• Arrays
• Array List (continued)
• Chapter Objectives
• Array List (continued)
• Learn about arrays
Specifying Array Size During Program Execution
• Explore how to declare and manipulate
data into arrays • The initializer list contains values,
called initial values, that are placed
• Understand the meaning of “array
between braces and separated by
index out of bounds”
commas
• Become familiar with the restrictions on
• Here, sales[0]= 12.25, sales[1]= 32.50,
array processing
sales[2]= 16.90, sales[3]= 23.00, and
• Chapter Objectives (continued) sales[4]= 45.68

• Discover how to pass an array as a • Array Initialization During Declaration


parameter to a method (continued)

• Discover how to manipulate data in a • When declaring and initializing arrays,


two-dimensional array the size of the array is determined by
the number of initial values within the
• Learn about multidimensional arrays
braces
• Array
• If an array is declared and initialized
• Definition: structured data type with a simultaneously, we do not use the
fixed number of elements operator new to instantiate the array
object
• Elements of an array are also called
components of the array • Arrays and the Instance Variable length

• Every element is of the same type • Associated with each array that has
been instantiated, there is a public
• Elements are accessed using their (final) instance variable length
relative positions in the array
• The variable length contains the size of
• One-Dimensional Arrays the array
• One-Dimensional Arrays (continued) • The variable length can be directly
• One-Dimensional Arrays (continued) accessed in a program using the array
name and the dot operator
• intExp = number of components in
array >= 0 int[] list = {10, 20, 30, 40, 50, 60};

• 0 <= indexExp <= intExp


• Arrays and the Instance Variable length //element of list
(continued)
for (i = 0; i < list.length; i++)
• This statement creates the array list of
list[i] = console.nextInt();
six components and initializes the
components using the values given for (i = 0; i < list.length; i++)
– Here list.length is 6 System.out.print(list[i] + " ");
int[] numList = new int[10]; • Arrays (continued)
• This statement creates the array • Some operations on arrays:
numList of 10 components and
initializes each component to 0 – Initialize

• Arrays and the Instance Variable length – Input data


(continued) – Output stored data
• The value of numList.length is 10 – Find
numList[0] = 5; largest/smallest/sum/average
of elements
numList[1] = 10;
• Code to Initialize Array to Specific Value
numList[2] = 15; (10.00)
numList[3] = 20; • Code to Read Data into Array
• These statements store 5, 10, 15, and • Code to Print Array
20, respectively, in the first four
components of numList • Code to Find Sum and Average of Array

• You can store the number of filled • Determining Largest Element in Array
elements, that is, the actual number of • Determining Largest Element in Array
elements, in the array in a variable, say (continued)
numOfElement
• Determining Largest Element in Array
• It is a common practice for a program (continued)
to keep track of the number of filled
elements in an array • Array Index Out of Bounds

• Processing One-Dimensional Arrays • Array in bounds if:

• Loops used to step through elements in 0 <= index <= arraySize – 1


array and perform operations • If index < 0 or index > arraySize:
int[] list = new int[100]; ArrayIndexOutOfBoundsException
int i; exception is thrown

for (i = 0; i < list.length; i++) • Base address: memory location of first


component in array
//process list[i], the (i + 1)th
• Declaring Arrays as Formal Parameters • Parallel Arrays
to Methods
• Arrays are parallel if corresponding
• A general syntax to declare an array as components hold related information
a formal parameter
• Arrays of Objects
dataType[] arrayName
• Can use arrays to manipulate objects
public static void
• Example: create array named array1
arraysAsFormalParameter(int[] listA,
with N objects of type T
double[] listB, int num)
T[] array1 = new T[N]
{
• Can instantiate array1 as follows:
//...
for(int j=0; j <array1.length; j++)
}
array1[j] = new T();
int[] intList = new int[10];
• Array of String Objects
double[] doubleNumList = new double[15];
String[] nameList = new String[5];
int number;
nameList[0] = "Amanda Green";
arraysAsFormalParameter(intList,
nameList[1] = "Vijay Arora";
doubleNumList, number);
nameList[2] = "Sheila Mann";
• The Assignment Operators and Arrays
nameList[3] = "Rohit Sharma";
• The Assignment Operators and Arrays
(continued) nameList[4] = "Mandy Johnson";
• The Assignment Operators and Arrays • Array of String Objects (continued)
(continued)
• Clock[] arrivalTimeEmp = new
• Relational Operators Arrays Clock[100];
• Relational Operators and Arrays • Instantiating Array Objects
(continued)
• Arrays and Variable Length Parameter
• Arrays as Parameter Methods List
• Methods for Array Processing • The syntax to declare a variable length
formal parameter (list) is:
• Methods for Array Processing
(continued) dataType ... identifier
• Methods for Array Processing • Arrays and Variable Length Parameter
(continued) List (continued)
• Methods for Array Processing • Arrays and Variable Length Parameter
(continued) List (continued)
• Arrays and Variable Length Parameter }
List (continued)
• Two-Dimensional Arrays
• Arrays and Variable Length Parameter
• Two-Dimensional Arrays (continued)
List (continued)
• double[][] sales = new double[10][5];
• A method can have at most one
variable length formal parameter • intExp1, intExp2 >= 0
• If a method has both a variable length • indexExp1 = row position
formal parameter and other types of
formal parameters, then the variable • indexExp2 = column position
length formal parameter must be the • Two-Dimensional Arrays and the
last formal parameter of the formal Instance Variable length (continued)
parameter list
• Each row of matrix is a one-dimensional
• foreach loop array; matrix[0], in fact, refers to the
• The syntax to use this for loop to first row
process the elements of an array is: • The value of the expression:
for (dataType identifier : arrayName) matrix[0].length
statements is 15, the number of columns in the first
• identifier is a variable and the data type row
of identifier is the same as the data type • matrix[1].length gives the number of
of the array components columns in the second row, which in
• foreach loop (continued) this case is 15, and so on

sum = 0; • Two-Dimensional Arrays: Special Cases

for (double num : list) • Two-Dimensional Arrays: Special Cases


(continued)
sum = sum + num;
• Two-Dimensional Array Initialization
• The for statement in Line 2 is read: for During Declaration
each num in list
• Two-Dimensional Array Initialization
• The identifier num is initialized to list[0 During Declaration (continued)
• In the next iteration, the value of num is • Two-Dimensional Array Initialization
list[1], and so on During Declaration (continued)
for (double num : numList) • Two-Dimensional Arrays (continued)
{ • Three ways to process 2-D arrays
if (max < num) – Entire array
max = num; – Particular row of array (row
processing)
– Particular column of array • Program: reads given text; outputs the
(column processing) text as is; prints number of lines and
number of times each letter appears in
• Processing algorithms similar to
text
processing algorithms of one-
dimensional arrays • Input: file containing text to be
processed
• Two-Dimensional Arrays: Processing
• Output: file containing text, number of
• Two-Dimensional Arrays: Processing
lines, number of times letter appears in
(continued)
text
• Two-Dimensional Arrays: Processing
• Programming Example Solution:
(continued)
Text Processing
• Two-Dimensional Arrays: Processing
• An array of 26 representing the letters
(continued)
in the alphabet
• Two-Dimensional Arrays: Processing
• Three methods
(continued)
– copyText
• Multidimensional Arrays
– characterCount
• Can define three-dimensional arrays or
n-dimensional array (n can be any – writeTotal
number)
• Value in appropriate index incremented
• Syntax to declare and instantiate array using methods and depending on
character read from text
dataType[][]…[] arrayName = new
• Chapter Summary
dataType[intExp1][intExp2]…[intExpn];
• Arrays
• Syntax to access component
– Definition
arrayName[indexExp1][indexExp2]…
[indexExpn] – Uses

• intExp1, intExp2, ..., • Different Arrays


intExpn = positive
– One-dimensional
integers
– Two-dimensional
• indexExp1,indexExp2, ...
, indexExpn = non- – Multidimensional (n-
negative integers dimensional)
• Loops to Process Multidimensional – Arrays of objects
Arrays
– Parallel arrays
• Programming Example:
Text Processing • Chapter Summary (continued)

• Declaring arrays
• Instantiating arrays

• Processing arrays

– Entire array

– Row processing

– Column processing

• Common operations and methods


performed on arrays

• Manipulating data in arrays

You might also like