Many of us have to use different type of collections daily. Some interact with an inventory of products in a warehouse or store. Cashiers or tellers interact with group of clients during their job in bank. Teachers interact with students attendance register. Hospital staff interact with patience register. So all these are different type of collections but we refer these by different names.
Programmers also use and work with many different type of collections. A collection at it’s core or most basic level is known as an Array.
An array is basically a fixed size series, collection, group or register of same type of data.
So a string array will only store strings. An numeric array will store only numbers. And an objects array will only store objects of that type. You can also say that an Array is a variable that represent a collection of one type of variables.
Why we need arrays when we already have variables. Here is a simple scenario.
We discussed a problem in previous chapter of printing product labels. And we wrote a function ‘printLabel’ to print product labels in an ascending order. Let’s assume we don’t want to print labels within that same function. We just want to return generated labels from a that function and send those for printing somewhere else.
But, a function can only return one value or variable. This is where arrays will come into play. We can store those labels in an array and return that array from our function. Right!
If we refer to our definition of arrays above, an array is a collection of fixed-size elements. So we will have to create an array equal to size or number of products in an order.
Let’s assume an order of 100 products again. So we will have to create an array of string of size 100 to store our labels.
Each block in an array is known as an element or index. So when we say we will create an array of size 100. It means that array will be able to store 100 elements of that type or it will have 100 indexes of that type.
This process of creating an array is known as array declaration and initialization. Just like variable declaration and initialization.
Declaring an array mean defining or creating an array of size N of required data type. While initializing an array mean putting/storing/writing values in that array.
Here is an array declaration will look like.
{Array of Data Type} {Name} of {size N}
Different programming languages have different ways to declare and initialize arrays. Here are two of those.
// Java Array Declaration
dataType[] arrayName;
// Java Array Initialization
// 'new' is a Java keyword
// that use to initialize objects
arrayName = new dataType[size];
// Java Array Declaration
// and initialization at same time
dataType[] arrayName = new dataType[size];
// PHP Array Declaration
// and initialization
$arrayName = array("one", "two", "three");
// As PHP is a loosely typed language
// That's why we can't just
// declare an array of required size
// We must initialize it with values
// of required data type. It can be
// empty strings, all zero or null
// objects
We can refer to each element or array index by number. Think of it as a house address or PO Box number.
Here is an interesting point and note to remember. Array index always start from Zero. This is kind of confusing for new programmers. But, it will become a norm when you go little deep in programming world. Therefore our array of size 100 will have 0 to 99 index or elements and we can refer each element like this in Java or C++.
// Array index start with 0
arrayName[0] = "Value One";
arrayName[1] = "Value Two";
...
...
...
// Last array index will be
// lastIndex = size - 1;
// i.e. 99
arrayName[99] = "Value Hundred";
So we can use any loop to write or store values in array instead of storing values manually like above. Therefore our loop might look like this.
// Declare an int variable
// to store size
int size = 100;
// Let's declare an array to store
// product numbers
int[] productNumbers = new int[size];
// Remember Array start from?
// Yes -> zero
for (int index = 0;
index < size;
index++) {
productNumbers[index] = index + 1;
// Because our product numbers
// start from 1 not zero.
}
// Also note use of '<' less than
// operator. Because, we can only
// store 100 elements in an array
// That's why our loop should exit
// at 99 considering Array index
// start from 0.
I hope you have much better understanding of Arrays now. If you want to learn more about Arrays in a specific programming language. Please search online or refer to a book.
Let’s move on to next chapter. Please click “Next” to continue.