Lesson Plan - Java Arrays.pdf
Lesson Plan - Java Arrays.pdf
Arrays in Java
Pre-Requisites
Basic Java synta
Java methods
Suppose we have to write a program in which can accept salaries of 50 employees. If we solve this problem by
making use of variables, we need 50 variables to store employees’ salaries. Managing these 50 variables is not
an easy task and will make the program a complex and lengthy one. This problem can be solved by declaring 1
array having 50 elements. Did you notice how convenient the scenario became? If arrays are that useful, why
not learn about them?
type var-name[];
OR
type[] var-name;
Primitive
int intArray[];
or int[] intArray;
byte byteArray[];
short shortsArray[];
boolean booleanArray[];
long longArray[];
float floatArray[];
double doubleArray[];
char charArray[];
Object
MyClass myClassArray[];
Object[] ao;
Collection[] ca
Array initialization
Array Literal
With curly braces, we can initialize the array and add value to it during initialization without defining the size.
You might be wondering that why arrays should be used when we already have the provision of creating as
many variables as we want/need.
Look at the scenarios mentioned below which will clear the air around utility of arrays for our good.
In the example below, we are using five different variables to save our elements one by one.
System.out.println(colour1);// Red
System.out.println(colour2);// Green
System.out.println(colour3);// Blue
System.out.println(colour4);// Yellow
System.out.println(colour5);// Purple
System.out.println(colours [i]);
Output:
Red
Green
Blue
Yellow
Purple
Did you see how arrays made the implementation so convenient and saved a lot of our coding time !
Arrays have an unlimited potential if we use them correctly. Let us look at its types and see how versatile these
can be to handle the most complex scenarios (which will be discussed in the forthcoming lectures)
1.Single-dimensional Array:
When we have elements stored in a single dimension sequentially, they are called single-dimensional arrays.
We can declare and allocate memory to a single-dimensional array using a single variable in Java.
Here is an example,
Output:
elementN};
int[] myArray = { 1, 2, 3, 4 , 5 };
// first element
System.out.println(myArray[0]); // to print 1
// second element
System.out.println(myArray[1]); // to print 2
// Third element
System.out.println(myArray[2]); // to print 3
// fourth element
System.out.println(myArray[3]); // to print 4
// fifth element
Output:
Multi-dimensional Array:
A multidimensional array is simply an array that consists of two or more dimensions and is also commonly
referred to as an array of arrays.
The diagram below will help you visualize the actual implementation structure of a multi- dimensional array.
Look at the example below for clarity. Here we are creating a 2-dimensional array
int[][] items = {
{2, 3},
{5, 6},
{7, 8}
};
1. Length of array
System.out.println(length); // print 4
Important Note: Array index always starts from 0, which means the first element is stored at index 0 and the
last element will be stored at index length-1.
There are many ways to iterate over the Array. The most common ways of looping through arrays in Java are
For Loop
// Creating an Array
System.out.println(length); // print 4
This loop helps in iterating over iterable objects like String, Array and so on.
Syntax:
statement;
Limitation
Its limitation is that it can only be used for traversing the whole array and not part of the array.
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8 };
for (int i : a) {
System.out.println(i);
Output:
Output: 9
Code:
int sum = 0;
sum += arr[i];
System.out.println(sum);
Output : 5
Code:
int mx = arr[0];
System.out.println(mx);
Problem 3: Search if the given element x is present in the array or not and find the index. If not present then
return the index as -1. (Linear Search)
x = 5
Output : 1
Code:
int x=5;
if(arr[i] == x) index = i;
System.out.println(index);
That is all for this class ! See you in the next array lecture !! Keep learning ! Keep Exploring !!