1. ARRAYS
An array refers to a data structure that contains
homogeneous elements. This means that all the
elements in the array are of the same data type.
2. ARRAYS
There are three main features of an array:
Dynamic allocation:
In arrays, the memory is created dynamically,
which reduces the amount of storage required for the
code.
Elements stored under a single name:
All the elements are stored under one name.
This name is used any time we use an array.
Occupies contiguous location:
The elements in the arrays are stored at
adjacent positions. This makes it easy for the user to
find the locations of its elements.
3. Advantages of Arrays in Java
• Java arrays enable you to access any element
randomly with the help of indexes
• It is easy to store and manipulate large data sets
Disadvantages of Arrays in Java
• The size of the array cannot be increased or
decreased once it is declared—arrays have a fixed
size
• Java cannot store heterogeneous data. It can only
store a single type of primitives
• Now that we understand what Java arrays are- let
us look at how arrays in Java are declared and
defined.
4. Define an Array in Java
Arrays in Java are easy to define and declare.
First, we have to define the array. The syntax
for it is:
Here, the type is int, String, double, or long.
Var-name is the variable name of the array.
5. byte a[];
short b[];
boolean booleanArray[];
long longArray[];
float floatArray[];
double doubleArray[];
char charArray[];
6. Instantiating an Array in Java
When an array is declared, only a reference of
an array is created. To create or give memory
to the array, you create an array like this:
The general form of new as it applies to one-
dimensional arrays appears as follows:
type var-name[];//array declaration
var-name = new type [size];
7. Example:
int intArray[]; //declaring array
intArray = new int[20]; // allocating memory to
array
OR
int[] intArray = new int[20]; // combining both
statements in one
8. Array Literal
The elements in the array allocated by new will
automatically be initialized to zero (for numeric
types), false (for boolean), or null (for reference
types).
In a situation where the size of the array and
variables of the array are already known, array
literals can be used.
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 };
// Declaring array literalThe length of this array
determines the length of the created array.
9. Types of Arrays:
There are three types of arrays.
We use these types of arrays as per the
requirement of the program.
1.One-dimensional Array
Also known as a linear array, the elements are
stored in a single row.
For example:
11. class GFG {
public static void main(String[] args)
{
// declares an Array of integers.
int[] arr;
// allocating memory for 5 integers.
arr = new int[5];
// initialize the first elements of the array
arr[0] = 10;
// initialize the second elements of the array
arr[1] = 20;
// so on...
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
// accessing the elements of the specified array
for (int i = 0; i < arr.length; i++)
System.out.println("Element at index " + i
+ " : " + arr[i]);
}
}
12. 3. Multi-dimensional Array
This is a combination of two or more arrays
or nested arrays.
We can even use more than two rows and
columns using the following code:
13. //Java Program to illustrate the use of multidimensional array
class Testarray3
{
public static void main(String args[])
{
//declaring and initializing 2D array
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
//printing 2D array
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}