0% found this document useful (0 votes)
8 views6 pages

IT5 Chapter 7

The document provides an overview of arrays in programming, defining them as collections of similar data types and explaining how to declare, create, and access them. It includes syntax examples for declaring arrays, creating them, and accessing their elements using indices. Additionally, it presents sample algorithms for printing array elements, summing them, and finding the maximum value in an array.

Uploaded by

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

IT5 Chapter 7

The document provides an overview of arrays in programming, defining them as collections of similar data types and explaining how to declare, create, and access them. It includes syntax examples for declaring arrays, creating them, and accessing their elements using indices. Additionally, it presents sample algorithms for printing array elements, summing them, and finding the maximum value in an array.

Uploaded by

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

POLYTECHNIC COLLEGE OF LA UNION

INFORMATION TECHNOLOGY DEPARTMENT

Array

IT5-Chapter 7

Array Defined:

An array is a collection of similar types of data. It is a container that holds


data (values) of one single type.

For example,

int[] age = new int[5];

Declaring Array Variables

To use an array in a program, you must declare a variable to reference


the array, and you must specify the type of array the variable can reference.
Here is the syntax for declaring an array variable −

Syntax:

dataType[] arrayRefVar; // preferred way. or dataType arrayRefVar[]; //


works but not preferred way.

Example:

double[] myList; // preferred way.

or

double myList[]; // works but not preferred way.

Creating Arrays

Page 1 of 6
POLYTECHNIC COLLEGE OF LA UNION
INFORMATION TECHNOLOGY DEPARTMENT

Syntax:

arrayRefVar = new dataType[arraySize];

The above statement does two things −

It creates an array using new dataType[arraySize].

It assigns the reference of the newly created array to the variable arrayRefVar.

Declaring an array variable, creating an array, and assigning the


reference of the array to the variable can be combined in one statement, as
shown below −

dataType[] arrayRefVar = new dataType[arraySize];

Alternatively you can create arrays as follows −

dataType[] arrayRefVar = {value0, value1, ...};

The array elements are accessed through the index. Array indices are 0-
based; that is, they start from 0 to arrayRefVar.length-1.

Arrays Sample Algorithm #1

For example:

int[] arr= {1,2,3,4,5,6,7,8,9,10};

for(int i=0;i<arr.length;i++) // or

System.out.println(arr[i]);

Test Array (Finding MAX) Arrays Sample Algorithm #2

double[] myList = {1.9, 2.9, 3.4, 3.5};

Part 1// Print all the array elements

for (int i = 0; i < myList.length; i++) {

System.out.println(myList[i] ); }

Part 2// Summing all elements

double total = 0;

Page 2 of 6
POLYTECHNIC COLLEGE OF LA UNION
INFORMATION TECHNOLOGY DEPARTMENT

for (int i = 0; i < myList.length; i++) {

total += myList[i];

System.out.println("Total is " + total);

Part 3// Finding the largest element

double max = myList[0];

for (int i = 1; i < myList.length; i++) {

if (myList[i] > max)

max = myList[i]; }

System.out.println("Max is " + max);

This will produce the following result −

1.9

2.9

3.4

3.5

Total is 11.7

Max is 3.5

Page 3 of 6
POLYTECHNIC COLLEGE OF LA UNION
INFORMATION TECHNOLOGY DEPARTMENT

Array Sample Demo #1

Array Representation:

5 7 12 37 80 3 25 11 92 10

Array lenght = 10
Total Indexes = Array lenght -1 = 9

Simulation Sheet:

i i<10 S.o.pln(arr[i]); i++

0 0<10=t 5 0+1=1
1 t 7 2
2 t 12 3
3 t 37 4
4 t 80 5
5 t 3 6
6 t 25 7
7 t 11 8
8 t 92 9
9 t 10 10
10 10<10=f

Array Sample Demo #2: FINDING MAX PROGRAM

Part 1: Print all the array elements

Array length = 4
Array name = myList
Indices = 0-3

1.9 2.9 3.4 3.5

for (int i = 0; i < myList.length; i++) {


System.out.println(myList[i] ); }

Page 4 of 6
POLYTECHNIC COLLEGE OF LA UNION
INFORMATION TECHNOLOGY DEPARTMENT

i i<4 Sop(myList[i]) i++


0 t 1.9 1
1 t 2.9 2
2 t 3.4 3
3 t 3.5 4
4 4<4=f

1.9
2.9
3.4
3.5

Part 2: Summing all elements

Array length = 4
Array name = myList

1.9 2.9 3.4 3.5

double total = 0;
for (int i = 0; i < myList.length; i++) {
total += myList[i];
}

total i i<4 total+=myList[i] i++ S.o.pln("Total


is " + total);
0 0 t 1.9 1 11.7
1.9 1 t 4.8 2
4.8 2 t 8.2 3
8.2 3 t 11.7 4
11.7 4 4<4=f

1.9
2.9
3.4
3.5
Total is: 11.7

Page 5 of 6
POLYTECHNIC COLLEGE OF LA UNION
INFORMATION TECHNOLOGY DEPARTMENT

Part 3: Finding the largest element

Array length = 4
Array name = myList

1.9 2.9 3.4 3.5

double max = myList[0];


for (int i = 1; i < myList.length; i++) {
if (myList[i] > max)
max = myList[i]; }
System.out.println("Max is " + max);

max=myList[0] i i<4 if(myList[i]>max) max = i++ Sopln(“


myList[i] Max is”
+ max)
1.9 1 t 2.9>1.9=t 2.9 2
2 t 3.4>2.9=t 3.4 3
3 t 3.5>3.4=t 3.5 4
4 4<4=f

1.9
2.9
3.4
3.5
Total is: 11.7
Max is 3.5

Page 6 of 6

You might also like