0% found this document useful (0 votes)
13 views12 pages

Java Array

Uploaded by

k2768243
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views12 pages

Java Array

Uploaded by

k2768243
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

JAVA ARRAY

TYPES OF ARRAY
• ONE DIMENSION ARRAY
• TWO DIMENSION ARRAY
• THREE DIMENSION ARRAY

This Photo by Unknown Author is licensed under CC BY-SA


WHAT IS ARRAY?

• Normally, an array is a collection of similar types of


elements which has a contiguous memory location.
• Java array is an object that contains elements of a similar
data type. Additionally, The elements of an array are
stored in a contiguous memory location. It is a data
structure where we store similar elements. We can store
only a fixed set of elements in a Java array.
WHO IT IS USE
• Arrays are used to store multiple values in a single
variable, instead of declaring separate variables for
each value.
• To declare an array, define the variable type
with square brackets:

String[] cars;
We have now declared a variable that holds an array of strings. To
insert values into it, you can place the values in a comma-
separated list, inside curly braces:

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

To create an array of integers, you could write:


int[] myNum = {10, 20, 30, 40};
Access the Elements of an Array
You can access an array element by referring to the index number.
This statement accesses the value of the first element in cars:

Example

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};


System.out.println(cars[0]); // Outputs Volvo
T

Array indexes start with 0: [0] is the first element. [1] is the second element, etc.
Change an Array Element
To change the value of a specific element, refer to the index
number
Example : cars[0] = "Opel";

Example
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; cars[0] = "Opel";
System.out.println(cars[0]); // Now outputs Opel instead of Volvo
Try it Yourself

Array Length
To find out how many elements an array has, use
the length property:
Example
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; System.out.println(cars.
length); // Outputs 4
Loop Through an Array
You can loop through the array elements with the for loop, and use
the length property to specify how many times the loop should run.
The following example outputs all elements in the car array:

ExampleGet your own Java Serve


String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (int i = 0; i < cars.length; i++)
{
System.out.println(cars[i]);
}
One-dimensional array (1-D arrays):
You can imagine a 1D array as a row, where elements are stored one after another.

Syntax for Declaration of Single-Dimensional Array

Below is the syntax to declare the single-dimensional array


data_type array_name[array_size];

For Example :int nums[5];


Two-dimensional (2D) array:

Multidimensional arrays can be considered as an array of arrays or as a


matrix consisting of rows and columns.

Syntax for Declaration of Two-Dimensional Array


Below is the syntax to declare the Two-dimensional array
data_type array_name[sizeof_1st_dimension][sizeof_2nd_dimension];

For Example :int nums[5][10];


Three-dimensional array:
A 3-D Multidimensional array contains three dimensions, so it can be
considered an array of two-dimensional arrays.

Syntax for Declaration of Three-Dimensional Array


Below is the syntax to declare the Three-dimensional array
data_type
array_name[sizeof_1st_dimension][sizeof_2nd_dimension][sizeof_3rd_dimension;

For Example :int nums[5][10][2];


EXAMPLE:
public class Main {
public static void main(String[] args) {
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7}};

for (int i = 0; i < myNumbers.length; ++i) {


for(int j = 0; j < myNumbers[i].length; ++j) {
System.out.println(myNumbers[i][j]); }
}
}
}

You might also like