Arrays
Arrays
Arrays
What is an
Array
An array is a data
structure used in
programming to store a
collection of elements,
where each element can
be accessed using an
index or a key. Arrays are
commonly used to
organize and manipulate
large amounts of data in a
structured manner.
Characteristics of Arrays
1. Ordered Collection:
Elements in an array are stored in a specific order, usually starting from index 0 and
increasing sequentially.
2.Fixed Size:
The size of an array is determined when it is created, and it typically cannot be
changed during runtime. Some languages provide dynamic arrays or resizable arrays
that can be extended or shrunk, but the basic idea remains the same.
3.Homogeneous Elements:
Elements within an array are of the same data type. For example, you might have an
array of integers, strings, or other specific data types.
4.Indexed Access:
Elements in an array can be accessed using their index. The index indicates the
position of the element within the array. Indexing often starts from 0 (zero-based
indexing), but some languages might use 1 (one-based indexing) or other
conventions.
Declaring arrays
You typically declare an array the same way you declare any variable but
with[ ]:
variable_type variable name[array_size] = { array, elements } ;
int numbers[5] = { 3 , 6 , 8 , 4 , 9 } ;
OR
variable_type variable name[ ] = { array, elements } ;
int numbers[ ] = { 3 , 6 , 8 , 4 , 9 } ;
!!!NOTE that here we did not specify the size just the elements
OR
variable_type variable name[array_size];
int numbers[7];
!!!NOTE that here we did specify the size, but not the elements. We can give the
elements later in the code.
Array positioning
int numbers[5] = { 3 , 6 , 8 , 4 , 9 };
numbers
0 1 2 3 4 Position
3 6 8 4 9 Element
?
Accessing Array elements (Write)
int numbers[5] = { 3 , 6 , 8 , 4 , 9 };
numbers[0] = 5; //change position 0 to contain 5
numbers[4] = 7; //change position 4 to contain 7
numbers[3] = 8; //change position 3 to contain 8
numbers[1] = 6; //change position 1 to contain 6
numbers[3] = 5; //position 3 will now contain a 5
0 1 2 3 4
5 6 8 5 7
!!!NOTICE THAT POSITION 2 REMAINS UNCHAINGED!!!
Strings
A string is a sequence of characters, typically used to represent textual
data in programming. Characters can include letters, digits, punctuation
marks, whitespace, and special symbols. Strings are one of the most
common and fundamental data types in programming languages, as
they are essential for dealing with text-based information and
communication.
Strings are a data type, but can also be characterized as an array
(character array).
To declare a string:
String variable_name = “value”;
e.g.
String _lion = “Simba”;
String positioning
Just like arrays strings can be used and accessed the same way we
would an array.
String _lion = “Simba”;
_lion
0 1 2 3 4
S i m b a
String Manipulation
Indexing and Slicing:
You can access individual characters within a string by using their
index (position) in the string. You can also extract substrings using
slicing.
Concatenation:
Strings can be concatenated, or joined together, to create new strings.
Length:
You can find the length of a string, which represents the number of
characters it contains.
String Manipulation:
Programming languages provide various built-in functions and
methods for manipulating strings, such as changing case, finding
substrings, replacing portions of the string, and more.
String Manipulation
(indexing)
With indexing, we can access the individual elements of the string
e.g.:
!!!NOTE that the character ‘S’ and ‘s’ are not the same
characters!!!
String Manipulation
(indexing)
With indexing, we can access the individual elements of the string
e.g.:
!!!NOTE that the character ‘S’ and ‘s’ are not the same characters!!!
String Manipulation (indexing)
With indexing, we can access the individual elements of the string e.g.:
!!!NOTE that the character ‘S’ and ‘s’ are not the same characters!!!
String Manipulation
(length)
We can use the length feature to get the length of the
string e.g.: