KnowledgeBoat Logo

Arrays

Introduction to Arrays

ICSE Computer Applications


In this section, we will discuss the very important and interesting topic of Arrays. We will start by understanding the need of arrays in programming. Then we will look at some real-world examples to understand the utility of arrays in our day-to-day lives. With this basic understanding in place, we will move on to learn about the syntax and working of single dimensional arrays in Java. After that, we will look at the more advanced concepts of searching, sorting and double dimensional arrays.

Why do we need arrays?

Variables are useful for keeping track of a single piece of information but as we collect more and more information, keeping the variables organized can be complicated. For example, what if you wanted to write a program to keep track of each student’s marks in your class? Assuming there are 40 students in the class, you need to create 40 variables in this program. Suppose with a lot of hard work you created these 40 variables and completed your program.

Your principal was very impressed with your program and asked you to extend it so that it can keep track of the marks of all the students in the school. Now you are in big trouble. Creating hundreds and thousands of individual variables is impractical. Moreover, the total number of students in the school keeps changing as new students join and current students pass out of the school. So, it is impossible to know ahead of time, how many variables you will need. In such situations, we need arrays to solve these problems in a much better and efficient way.

What is an Array?

An array is a way of arranging things in rows and columns. In our daily lives we come across many things that are arranged as an array. Let’s quickly look at a few examples:

Array examples in real-world

This first example is that of a marching contingent. I am sure you would have also done march past or parade in your school as part of the physical training classes. In a marching contingent, soldiers are arranged in rows and columns forming a rectangular or square shaped contingent. It helps in organizing the contingent efficiently. In this next example, we see the popular Crocin tablets lined up in a foil blister pack to create an array. In warehouses, bags are stacked up in rows and columns forming arrays. For a gathering, chairs are usually arranged in rows and columns.

From now on, whenever you will see things arranged in rows and columns, these examples will immediately remind you that they are organized as an array.

Types of Arrays

Arrays are of two types:

  1. Multi-dimensional arrays
  2. Single dimensional arrays

Multi-dimensional arrays

In a multi-dimensional or n-dimensional array, items are arranged in two or more dimensions.

Double Dimensional Arrays

Take the previous examples of arrays we looked at. In each of these examples, the objects are arranged in 2 dimensions i.e. across rows and columns or we can also say that the items are arranged across the x and y dimensions. We call them double dimensional arrays or 2-D arrays.

Three Dimensional Arrays

Rubik's cube as an example of three dimensional array

Now consider a Rubik’s cube. In a Rubik’s cube, the colours are arranged across the 3 dimensions of the cube i.e. its length, breadth and height or x, y and z dimensions. So, this is an example of three dimensional or 3-D arrays.

Theoretically, you can have even higher dimensional 4-D, 5-D, 6-D arrays and so on. But beyond 3-D arrays it is very difficult to visualize them.

Single dimensional arrays

In a single-dimensional array, items are arranged across only the x dimension. So, a single-dimensional array has only a single row and one or more columns.

A queue of people as an example of a single dimensional array

A good example of a single dimensional array is a queue of people. It has just one row and each person in the queue can be considered as a column.

ICSE Computer Applications syllabus includes only Single and Double Dimensional arrays so we will limit our discussion to only these 2 types of arrays in this course.

PrevNext