You are on page 1of 31

Data structure and algorithm

Arrays One-dimensional and traversal


methods
Array Data Structure
An array is a collection of items stored at contiguous memory locations. The
idea is to store multiple items of the same type together. This makes it
easier to calculate the position of each element by simply adding an offset
to a base value, i.e., the memory location of the first element of the array
(generally denoted by the name of the array).
Array Data Structure

The above image can be looked as a top-level view of a staircase where


you are at the base of the staircase. Each element can be uniquely
identified by their index in the array (in a similar way as you could identify
your friends by the step on which they were on in the above example).
Types of indexing in array
• 0 (zero-based indexing): The first element of the array is indexed by
subscript of 0
• 1 (one-based indexing): The second element of the array is indexed by
subscript of 1
• n (n-based indexing): The base index of an array can be freely chosen.
Usually programming languages allowing n-based indexing also allow
negative index values and other scalar data types like enumerations, or
characters may be used as an array index.
Declaring Array Variables
To use an array in a program, you must declare a variable to reference the
array, and you must specify the type of array the variable can reference. Here
is the syntax for declaring an array variable -
Creating Arrays
You can create an array by using the new operator with the following syntax −
Syntax
arrayRefVar = new dataType[arraySize];
The above statement does two things −
It creates an array using new dataType[arraySize].
It assigns the reference of the newly created array to the variable arrayRefVar.
Declaring an array variable, creating an array, and assigning the reference of the array to the
variable can be combined in one statement, as shown below −
dataType[] arrayRefVar = new dataType[arraySize];
Alternatively you can create arrays as follows −
dataType[] arrayRefVar = {value0, value1, ..., valuek};
The array elements are accessed through the index. Array indices are 0-based; that is, they
start from 0 to arrayRefVar.length-1
Creating Arrays - (One Dimensional)
Example
Following statement declares an array variable, myList, creates an array of
10 elements of double type and assigns its reference to myList −

Following picture represents array myList. Here, myList holds ten double
values and the indices are from 0 to 9.
Types of indexing in array
• 0 (zero-based indexing): The first element of the array is indexed by
subscript of 0
• 1 (one-based indexing): The second element of the array is indexed by
subscript of 1
• n (n-based indexing): The base index of an array can be freely chosen.
Usually programming languages allowing n-based indexing also allow
negative index values and other scalar data types like enumerations, or
characters may be used as an array index.
Creating Arrays - (One Dimensional)
Processing an Array (One Dimensional)
When processing array elements, we often use either for loop
or foreach loop because all of the elements in an array are of the same type
and the size of the array is known.
Example
Here is a complete example showing how to create, initialize, and process
arrays
Processing Arrays
public class TestArray
{
public static void main(String[] args)
{
double[] myList = {1.9, 2.9, 3.4, 3.5};
// Print all the array elements
for (int i = 0; i < myList.length; i++)
{ System.out.println(myList[i] + " "); }
// Summing all elements
double total = 0;
for (int i = 0; i < myList.length; i++)
{ total += myList[i]; } System.out.println("Total is " + total);
// Finding the largest element
double max = myList[0];
for (int i = 1; i < myList.length; i++)
{
if (myList[i] > max) max = myList[i]; }
System.out.println("Max is " + max); }
Processing Arrays
Output
1.9
2.9
3.4
3.5
Total is 11.7
Max is 3.5
The foreach Loops
JDK 1.5 introduced a new for loop known as foreach loop or enhanced for
loop, which enables you to traverse the complete array sequentially without
using an index variable.
• Example
• The following code displays all the elements in the array myList −
The foreach Loops
public class TestArray
{
double[] myList = {1.9, 2.9, 3.4, 3.5};
// Print all the array elements
for (double element: myList)
{ System.out.println(element);
The foreach Loops
• Output
• 1.9
• 2.9
• 3.4
• 3.5
Passing Arrays to Methods
Just as you can pass primitive type values to methods, you can also pass arrays to method
For example, the following method displays the elements in an int array −
Example
public static void printArray(int[] array)
{
for (int i = 0; i < array.length; i++)
{
}
}
You can invoke it by passing an array. For example, the following statement invokes the printArray method
to display 3, 1, 2, 6, 4, and 2 −

Example
printArray(new int[]{3, 1, 2, 6, 4, 2});
Returning an Array from a Method
• A method may also return an array.
• For example, the following method returns an array that is the
reversal of another array −
• Example
• public static int[] reverse(int[] list)
•{
• int[] result = new int[list.length];
• for (int i = 0, j = result.length - 1; i < list.length; i++, j--)
• { result[j] = list[i]; } return result;
The Arrays Class
The java.util.Arrays class contains various static methods for sorting and
searching arrays, comparing arrays, and filling array elements. These
methods are overloaded for all primitive types.
The Arrays Class
•Sr.No.
•Method & Description

•1
•public static int binarySearch(Object[] a, Object key)
•Searches the specified array of Object ( Byte, Int , double, etc.) for the specified value using the binary
search algorithm. The array must be sorted prior to making this call. This returns index of the search key,
if it is contained in the list; otherwise, it returns ( – (insertion point + 1)).
•2
•public static boolean equals(long[] a, long[] a2)
•Returns true if the two specified arrays of longs are equal to one another. Two arrays are considered
equal if both arrays contain the same number of elements, and all corresponding pairs of elements in
the two arrays are equal. This returns true if the two arrays are equal. Same method could be used by all
other primitive data types (Byte, short, Int, etc.)
•3
•public static void fill(int[] a, int val)
•Assigns the specified int value to each element of the specified array of ints. The same method could be
used by all other primitive data types (Byte, short, Int, etc.)
•4
•public static void sort(Object[] a)
•Sorts the specified array of objects into an ascending order, according to the natural ordering of its
elements. The same method could be used by all other primitive data types ( Byte, short, Int, etc.)
Traversing an Array – One Dimensional
In general, arrays are the containers that store multiple variables of the
same datatype. These are of fixed size and the size is determined at the
time of creation. Each element in an array is positioned by a number starting
from 0.

You can access the elements of an array using name and position
as −

System.out.println(myArray[3]);
//Which is 1457
Creating an array in Java
• In Java, arrays are treated as referenced types you can create an array using the new keyword
similar to objects and populate it using the indices as −
• int myArray[] = new int[7];
• myArray[0] = 1254;
• myArray[1] = 1458;
• myArray[2] = 5687;
• myArray[3] = 1457;
• myArray[4] = 4554;
• myArray[5] = 5445;
• myArray[6] = 7524;
• Or, you can directly assign values with in flower braces separating them with commas (,) as −
• int myArray = { 1254, 1458, 5687, 1457, 4554, 5445, 7524};
Traversing through an array
You can traverse through an array using for loop or forEach loop.
Using the for loop − Instead on printing element by element, you can iterate the index using for loop starting
from 0 to length of the array (ArrayName.length) and access elements at each index.
Example
public class IteratingArray {
public static void main(String args[]) {
//Creating an array
int myArray[] = new int[7];
//Populating the array
myArray[0] = 1254;
myArray[1] = 1458;
myArray[2] = 5687;
myArray[3] = 1457;
Traversing through an array
• myArray[4] = 4554;
• myArray[5] = 5445;
• myArray[6] = 7524;
• //Printing Contents using for loop
• System.out.println("Contents of the array: ");
• for(int i=0; i<myArray.length; i++) {
• System.out.println(myArray[i]);
• }
• }
•}
Traversing through an array
• Output
• Contents of the array:
• 1254
• 1458
• 5687
• 1457
• 4554
• 5445
• 7524
Traversing through an array
Using the for each loop − Since JDK 1.5, Java introduced a new for loop
known as foreach loop or enhanced for loop, which enables you to traverse
the complete array sequentially without using an index variable. You can
traverse through the array with less effort using this
Traversing through an array
Example
import java.util.Arrays;
public class IteratingArray {
public static void main(String args[]) {
//Creating an array
int myArray[] = new int[7];
//Populating the array
myArray[0] = 1254;
myArray[1] = 1458;
myArray[2] = 5687;
Traversing through an array
myArray[3] = 1457;
• myArray[4] = 4554;
• myArray[5] = 5445;
• myArray[6] = 7524;
• //Printing Contents using for each loop
• System.out.println("Contents of the array: ");
• for (int element: myArray) {
• System.out.println(element);
• }
• }
• }
• Output
• Contents of the array:
• [1254, 1458, 5687, 1457, 4554, 5445, 7524]
Advantages and disadvantages of Arrays

Advantages
1. Reading an array element is simple and efficient. As shown in the above table,
the read time of array is O(1) in both best and worst cases. This is because any
element can be instantly read using indexes (base address calculation behind
the scene) without traversing the whole array.
2. Array is a foundation of other data structures. For example other data
structures such as LinkedList, Stack, Queue etc. are implemented using array.
3. All the elements of an array can be accessed using a single name (array
name) along with the index, which is readable, user-friendly and efficient rather
than storing those elements in different-2 variables.
Advantages and disadvantages of Arrays

Disadvantages
1. While using array, we must need to make the decision of the size
of the array in the beginning, so if we are not aware how many
elements we are going to store in array, it would make the task
difficult.
2. The size of the array is fixed so if at later point, if we need to store
more elements in it then it can’t be done. On the other hand, if we
store less number of elements than the declared size, the remaining
allocated memory is wasted.
References
javatpoint.com. Java - Arrays retrieved from
https://www.tutorialspoint.com/java/java_arrays.htm

geeksforgeeks Introduction to arrays retrieved from


https://www.geeksforgeeks.org/introduction-to-arrays/

You might also like