0% found this document useful (0 votes)
328 views10 pages

Programming 1A: (PROG5121)

The document discusses arrays in Java programming, including declaring and initializing arrays, using subscripts to access array elements, searching arrays, and passing/returning arrays. It provides examples of declaring various types of arrays, initializing arrays with values, accessing elements with subscripts, searching an array, and a method that passes and returns an array. It also presents a class activity to write a program that reverses the order of elements in an array.

Uploaded by

Dzudzi Manyuha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
328 views10 pages

Programming 1A: (PROG5121)

The document discusses arrays in Java programming, including declaring and initializing arrays, using subscripts to access array elements, searching arrays, and passing/returning arrays. It provides examples of declaring various types of arrays, initializing arrays with values, accessing elements with subscripts, searching an array, and a method that passes and returns an array. It also presents a class activity to write a program that reverses the order of elements in an array.

Uploaded by

Dzudzi Manyuha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

PROGRAMMING 1A

(PROG5121)
INTRODUCTION TO ARRAYS

BASED ON CHAPTER 8, FARRELL, J. 2019. JAVA PROGRAMMING.


9TH ED. 2019. COURSE TECHNOLOGY, CENGAGE LEARNING.
DECLARING AND CREATING ARRAYS

• An array is a named list of data items that all have the same data type.
• Each data item is an element of the array.
• To declare an array of double values to hold sales figures for sales people you can write
the following:
double[] salesFigures;
• To create an array of integers to hold student ID numbers, you can write the following:

int[] idNums;
• You can declare and create an array in one statement with the following:

double[] salesFigures = new double[20];


MEMORY ALLOCATION AND SUBSCRIPTS

• A subscript is an integer contained within square brackets that specifies one of an array’s
elements.
• In Java, any array’s elements are numbered beginning with 0, so you can legally use any
subscript from 0 through 19 when working with an array that has 20 elements.
INITIALIZING AN ARRAY

• To initialize an array when you declare it, you use an


initialization list of values separated by commas and
enclosed within curly braces.
• Providing values for all the elements in an array also is
called populating the array.
• Example of array initialization:
int[] num = {20, 30, 40, 60, 80, 100, 120, 150, 200, 300};
USING SUBSCRIPTS WITH AN ARRAY

Suppose you declare an array and a named constant as follows:

int[] scoreArray = {2, 14, 35, 67, 85};


final int INCREASE = 3;
final int NUMBER_OF_SCORES = 5;
Then, the following two loops are identical:
for(int sub = 0; sub < 5; ++sub)
scoreArray[sub] += INCREASE;
for(int sub = 0; sub < NUMBER_OF_SCORES; ++sub)

scoreArray[sub] += INCREASE;
ARRAYS OF OBJECTS

Employee[] emps = new Employee[7];

final int START_NUM = 101;

final double STARTING_SALARY = 15000;

for (int x = 0; x < emps.length; ++x)

emps[x] = new Employee(START_NUM + x,

STARTING_SALARY);

for (int x = 0; x < emps.length; ++x)

System.out.println (emps[x].getEmpNum() +

""+

emps[x].getSalary());
You can also use the enhanced for loop
alternative to iterate through an array of objects:
for (Employee worker : emps)
System.out.println(worker.getEmpNum() +
""+
worker.getSalary());
SEARCHING AN ARRAY

int[] validValues = {101, 108, 201, 213, 266,


304, 311, 409, 411, 412};
int itemOrdered = 304;
boolean validItem = false;

for(int x = 0; x < validValues.length; ++x) {


if (itemOrdered == validValues[x])
validItem = true;
} Comparing a variable to a list of
values in an array, is a process
called searching an array.
PASSING AND RETURNING ARRAYS

This method receives an integer


array through its parameter list.

This method returns an integer array


to its caller.
CLASS ACTIVITY 8

Write a Java program that reverses the order of the elements in an array. Call your class
containing the main() method ReverseArray.
Your code should produce the following output:

Array before reverse:


1 2 3 4 5 6 7 8 9 10
Array after reverse:
10 9 8 7 6 5 4 3 2 1

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy