Professional Documents
Culture Documents
Arrays
Arrays
_________________
What is array?
• Collection of variables called elements, each with the same name and
data type. These element are distinguished from one another by a
unique index called a subscript which is usually enclosed in square
brackets immediately after the array name.
• An array is a data structure that can hold multiple values (unlike a
variable, which can only hold 1 value at a time).
• Array index start from 0 (zero).
• E.g nameList[0], represent the first element in the array called
nameList
Why use Arrays?
• Use an array when you need to group values by some common
characteristic (e.g. – Students in a classroom).
• When designing an application that performs the same processing for
multiple sets of data, use an array and a looping structure.
HOW TO DECLARE AN ARRAY?
• Declare Numeric markList[10]
• To declare an array in JavaScript, we use the array constructor and
assign the array to a variable. We give the constructor the size of the
array as an argument:
var markList = new Array(10);
• At times, we may not know the size of the array when declaring. In
such situations, we can declare an empty array:
var markList = new Array();
Declaring an array
• Declare constant SIZE = 10
• Declare Numeric markList [SIZE]
0 1 2 3 4
50 70 20 80 45 markList
Create an array called priceList. At the coding stage load this array with
the following prices: 4.00, 13.50, 20.00, 5.00, 100.00
LOADING ARRAY USING A FOR LOOP
Declare Numeric index
Declare Contant SIZE = 5
Declare Numeric markList[SIZE]
0 1 2 3 4
50 70 20 80 45
Searching Arrays
Searching Arrays
• Example: searching for multiple matches
• Customers whose last name begins with “A”
Searching an array
Solution
Display “ Enter a mark you looking for”
Input searchMark
For index = 0 to SIZE -1
If markList[index] == searchMark then
Display “mark is found at “+index
Else
Display “mark is not found“
End IF
End For
Parallel Arrays
• Parallel arrays gives you the ability to store multiple pieces of
information about a single entity in an application.
• Parallel arrays use a common subscript to relate data fields.
• The indexes of each array should correspond to one another for
individual entities:
• Student ID
• Student Name
• Student Exam Score
Conceptual Example of Parallel Arrays
• Arrays 0 047254 0 John 0 93
2 Elizabeth 2 74
2 024136
3 Omar 3 92
3 057866
4 015543 4 Charles 4 56
5 025769 5 Ravi 5 82
6 025119 6 Katherine 6 89
7 035176 7 Hannah 7 82
8 036585 8 Alexander 8 75
9 028116 9 William 9 77
Creating a Parallel Array
• To create parallel arrays, just create them as you would any other
array:
var SID = new Array();
var n201Class = new Array();
var examScr = new Array();
• The arrays are separate arrays in memory, but your program should
treat them as one …
Assigning Values to Parallel Arrays
• To assign values for a single entity to parallel arrays, use the same
index number for each assignment:
SID[5] = “025769”;
n201Class[5] = “Ravi”;
examScr[5] = 82;
PARALLEL ARRAYS
• Array has a single name and data type
• Example: employee numbers
• Can create parallel array
• To contain other employee information
• Use a common subscript to relate data fields
• Example: arrays empNumber, empFirstName, and empLastName
empNumber[0]= employee number for Joe Smith
empFirstName[0] = “Joe”
empLastName[0] = “Smith”
PARALLEL ARRAYS
• Example: display all employee information
• Works with nonzero employee numbers
d = new Array ( 5 ) ;
document.write( d.length ) ;
The ‘length’ Property of Arrays
x = new Array ( 10 ) ;
for ( x = 0 ; x < 10 ; x = x + 1 ) {
y[ x ] = x * x ;
}
x = new Array ( 10 ) ;
lowest=numList[j];
}
}
Selection Sort continue…….
//Swaping the contents of the Array
temp=numList[i];
numList[i]=lowest;
numList[pos]=temp;
}
//Displaying the contents of the Array
for(var i=0;i<numList.length;i++)
{
document.write (""+numList[i]);
}
The End!!!