U -1 Array in java
U -1 Array in java
There are some basic operations we can start with as mentioned below:
1. Array Declaration
To declare an array in Java, use the following syntax:
type[] arrayName;
type: The data type of the array elements (e.g., int, String).
2. Create an Array
To create an array, you need to allocate memory for it using the new keyword:
This statement initializes the numbers array to hold 5 integers. The default value for each
element is 0.
The first line sets the value of the first element to 10. The second line retrieves the value of the
first element.
5. Array Length
We can get the length of an array using the length property:
Array Properties
In Java, all arrays are dynamically allocated.
Arrays may be stored in contiguous memory [consecutive memory locations].
Since arrays are objects in Java, we can find their length using the object property length. This is different
from C/C++, where we find length using size of.
A Java array variable can also be declared like other variables with [] after the data type.
The variables in the array are ordered, and each has an index beginning with 0.
Java array can also be used as a static field, a local variable, or a method parameter.
An array can contain primitives (int, char, etc.) and object (or non-primitive) references of a class,
depending on the definition of the array. In the case of primitive data types, the actual values might be
stored in contiguous memory locations (JVM does not guarantee this behavior). In the case of class
objects, the actual objects are stored in a heap segment.
Note: This storage of arrays helps us randomly access the elements of an array [Support
Random Access].
Declare
Initialize
Access
i. Declaring an Array
The general form of array declaration is
Method 1:
type var-name[];
Method 2:
type[] var-name;
The element type determines the data type of each element that comprises the array. Like an
array of integers, we can also create an array of other primitive data types like char, float,
double, etc., or user-defined data types (objects of a class).
Note: It is just how we can create is an array variable, no actual array exists. It merely tells
the compiler that this variable (int Array) will hold an array of the integer type.
Here, type specifies the type of data being allocated, size determines the number of elements
in the array, and var-name is the name of the array variable that is linked to the array. To
use new to allocate an array, you must specify the type and number of elements to
allocate.
Example:
// declaring array
int intArray[];
Note: 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). Do refer to default array
values in Java.
Obtaining an array is a two-step process. First, you must declare a variable of the desired array
type. Second, you must allocate the memory to hold the array, using new, and assign it to the
array variable. Thus, in Java, all arrays are dynamically allocated.
Array Literal in Java
In a situation where the size of the array and variables of the array are already known, array
literals can be used.
The length of this array determines the length of the created array.
There is no need to write the new int[] part in the latest versions of Java.
All the elements of array can be accessed using Java for Loop.
class GFG {
public static void main(String[] args)
{
// declares an Array of integers.
int[] arr;
Output
Element at index 0 : 10
Element at index 1 : 20
Element at index 2 : 30
Element at index 3 : 40
Element at index 4 : 50
A single-dimensional array in Java is a linear collection of elements of the same data type.
arrayRefVar=new datatype[size];
// A single-dimensional array
int[] singleDimArray = {1, 2, 3, 4, 5};
Multidimensional Array in Java
A multidimensional array in Java is an array of arrays where each element can be an array itself.
Multidimensional arrays are arrays of arrays with each element of the array holding the reference of other
arrays.
Multidimensional arrays are useful when you want to store data as a tabular form, like a table with rows
and columns.
A multidimensional array is created by appending one set of square brackets ([]) per dimension.
Syntax:
There are 2 methods to declare Java Multidimensional Arrays as mentioned below:
// Method 1
datatype [][] arrayrefvariable;
// Method 2
datatype arrayrefvariable[][];
Declaration:
// 2D array or matrix
int[][] intArray = new int[10][20];
// 3D array
int[][][] intArray = new int[10][20][10];
Example
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
Access Elements
To access the elements of the myNumbers array, specify two indexes: one for the array, and one
for the element inside that array. This example accesses the third element (2) in the second array
(1) of myNumbers:
Example
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
System.out.println(myNumbers[1][2]); // Outputs 7
Change Element Values
You can also change the value of an element:
Example
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
myNumbers[1][2] = 9;
Example
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
System.out.println(myNumbers[i][j]);
}
}
Or you could just use a for-each loop, which is considered easier to read and write:
Example
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
System.out.println(i);