The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. ― Donald E. Knuth

# Java Tutorial

## Arrays and Vectors

 Module Index ► Introduction to Arrays ► Passing array to method ► Returning array from method ► 2-Dimensional Arrays ( Matrix ) ► Passing 2D array to method ► Returning 2D array from method ► Vectors

Introduction to Arrays
Suppose you want to store the marks of 100 students and then find the average. Would you declare 100 variables for that ? Definitely not. So, here comes a data structure called array which provides an utility to store a collection of elements of same data type in contiguous memory location.
Consider the declaration below :

 int marks[ ] = new int[5]; // declaration and instantiation of the array

In the above statement, we have declared and instantiated an array named marks which can store 5 values of type int. Each of the values can be accessed using an index in the array.
Suppose there are 5 values stored in the array marks as show below :

 75 62 93 49 88

Value stored at index 0 of array marks is 75 i.e marks [ 0 ] = 75. Remember array index starts from 0.
Similarly, marks [ 2 ] = 93. The array marks can be initialized with 5 values in a single statement :

 int marks[ ] = { 75, 62, 93, 49, 88 }; // declaration, instantiation and initialization of the array

Let's look at a very simple program which illustrates the declaration, instantiation and initialization of an array :

Passing array to method
Similar to other data types, we can also pass an array to a method. Following program searches an element in array and returns its position if found.

Returning array from method
Next we illustrate how to return an array from method. In the following program, a method takes an array as argument and returns another array in which every element is multiplied by 10.

2-Dimensional Arrays ( Matrix )
So, far we have seen one dimensional arrays. Now we will see two dimensional array also known as matrix, which is another useful data structure. Consider the declaration below :

 int mat[ ][ ] = new int[3][4];

We have declared and instantiated a two dimensional array or matrix with 3 rows and 4 columns. We can store
3 x 4 = 12 elements in this matrix. Each element is stored in a cell which can be accessed using the combination of row index and column index. Initialization of a 2D array is shown below :

InitializationIndexing
``````int mat[3][4] = { { 17, 23, 15, 19 },
{ 44, 29, 52, 76 },
{ 21, 63, 35, 57 } };``````

 17 [ 0, 0 ] 23 [ 0, 1 ] 15 [ 0, 2 ] 19 [ 0, 3 ] 44 [ 1, 0 ] 29 [ 1, 1 ] 52 [ 1, 2 ] 76 [ 1, 3 ] 21 [ 2, 0 ] 63 [ 2, 1 ] 35 [ 2, 2 ] 57 [ 2, 3 ]

We can see how the above initialization statement in the left builds a matrix shown in the right. Every element is stored in a cell which has a index shown as subscript of each element. For e.g, 52 is stored in a cell with index
[ 1, 2 ] where 1 denotes the row number ( index ) and 2 denotes the column number ( index ). Please note that both row and column index starts with 0.
Following program illustrates declaration, instantiation and initialization of a matrix :

Passing 2D array to method
Following program illustrates how to pass 2D array to methods :

Returning 2D array from method
Following program illustrates how to return 2D array from methods :

Vectors
The main limitation of array is size limit. We can store only fixed number of elements in the array and the size cannot be grown at runtime. Vector introduces the concept of dynamic array in Java. Vector is a class contained in java.util package and it can hold any number and any type of objects. The only limitation is that it can't hold elements of primitive data types like int, float, char, double, long and boolean, we can only store objects. So, the elements need to be converted to objects before storing them in vector. This can be done using wrapper classes contained in java.lang package. For e.g Integer is a wrapper class to convert an int type element to an object. Following set of statements explain how int type element is converted to Integer object and vice versa.

Following table shows the list of wrapper classes which are used to convert primitive data types to objects :

 Primitive Data Type Wrapper Class int Integer char Character long Long float Float double Double boolean Boolean

Let's look at a program demonstrating the use of Vector class.

Vector class has lot of methods. In the above program, we have used addElement( ), size( ) and firstElement( ). Please refer to the documentation of Vector to know about all the methods. Some of the commonly used methods are shown in the table below :

 Method Description addElement(item) Add an item to the end of the vector size() Get the no. of objects present in the vector removeElement(item) Removes the specified item from the vector removeElement(pos) Remove the item stored at specified position insertElementAt(item, pos) Insert item at specified position copyInto(array) Copy the vector elements into array