LEC 10 1. Arrays
LEC 10 1. Arrays
Pilani Campus
Arrays
Introduction to Arrays
• An array is a data structure used to process a
collection of data that is all of the same type
– An array behaves like a numbered list of variables
with a uniform naming mechanism
– It has a part that does not change:
• the name of the array
– It has a part that can change:
• an integer in square brackets
– For example, given five scores:
score[0],score[1],score[2],score[3],score[4
]
• The declaration
double[] score = new double[5];
results in the 5 elements
score[0], score[1], score[2], score[3], score[4]
(continued)
(continued)
(continued)
(continued)
BITS Pilani, Pilani Campus
Array Parameters
• Arrays of double may be defined as follows:
double[] a = new double[10];
double[] b = new double[30];
• Given the arrays above, the method doubleElements
from class SampleClass can be invoked as follows:
SampleClass.doubleElements(a);
SampleClass.doubleElements(b);
– Note that no square brackets are used when an entire
array is given as an argument
– Note also that a method that specifies an array for a
parameter can take an array of any length as an argument
(continued)
– Note that the above code will not make b an exact copy
of a, unless a and b have the same length
(continued)
(continued)