0% found this document useful (0 votes)
3 views8 pages

Java Arrays: Declaration, Access, and Types

The document provides an overview of Java arrays, explaining how to declare, instantiate, and initialize both single-dimensional and multi-dimensional arrays. It covers accessing and modifying array elements, finding the length of an array, and using loops to traverse arrays. Additionally, it discusses passing arrays to methods and returning arrays from methods.

Uploaded by

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

Java Arrays: Declaration, Access, and Types

The document provides an overview of Java arrays, explaining how to declare, instantiate, and initialize both single-dimensional and multi-dimensional arrays. It covers accessing and modifying array elements, finding the length of an array, and using loops to traverse arrays. Additionally, it discusses passing arrays to methods and returning arrays from methods.

Uploaded by

faizanhameed690
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Arrays

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 to 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"};

[Link](cars[0]);

// Outputs Volvo

Try it Yourself »
Note: 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";

[Link](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"};

[Link]([Link]);

// Outputs 4

Types of Array in java


There are two types of array.
o Single Dimensional Array
o Multidimensional Array

Single Dimensional Array in Java


Syntax to Declare an Array in Java

1. dataType[] arr; (or)


2. dataType []arr; (or)
3. dataType arr[];

Instantiation of an Array in Java

1. arrayRefVar=new datatype[size];

Example of Java Array


Let's see the simple example of java array, where we are going to
declare, instantiate, initialize and traverse an array.

1. //Java Program to illustrate how to declare, instantiate, initialize


2. //and traverse the Java array.
3. class Testarray{
4. public static void main(String args[]){
5. int a[]=new int[5];//declaration and instantiation
6. a[0]=10;//initialization
7. a[1]=20;
8. a[2]=70;
9. a[3]=40;
10. a[4]=50;
11. //traversing array
12. for(int i=0;i<[Link];i++)//length is the property of array
13. [Link](a[i]);
14. }}

Declaration, Instantiation and Initialization of Java


Array
We can declare, instantiate and initialize the java array together by:

1. int a[]={33,3,4,5};//declaration, instantiation and initialization

Let's see the simple example to print this array.

1. //Java Program to illustrate the use of declaration, instantiation


2. //and initialization of Java array in a single line
3. class Testarray1{
4. public static void main(String args[]){
5. int a[]={33,3,4,5};//declaration, instantiation and initialization
6. //printing array
7. for(int i=0;i<[Link];i++)//length is the property of array
8. [Link](a[i]);
9. }}
Test it Now

Output:
33
3
4
5

For-each Loop for Java Array


We can also print the Java array using for-each loop. The Java for-
each loop prints the array elements one by one. It holds an array
element in a variable, then executes the body of the loop.
The syntax of the for-each loop is given below:

1. for(data_type variable:array){
2. //body of the loop
3. }

Let us see the example of print the elements of Java array using the
for-each loop.

1. //Java Program to print the array elements using for-each loop


2. class Testarray1{
3. public static void main(String args[]){
4. int arr[]={33,3,4,5};
5. //printing array using for-each loop
6. for(int i:arr)
7. [Link](i);
8. }}

Output:
33
3
4
5

Passing Array to a Method in Java


We can pass the java array to method so that we can reuse the
same logic on any array.

Let's see the simple example to get the minimum number of an


array using a method.

1. //Java Program to demonstrate the way of passing an array


2. //to method.
3. class Testarray2{
4. //creating a method which receives an array as a parameter
5. static void min(int arr[]){
6. int min=arr[0];
7. for(int i=1;i<[Link];i++)
8. if(min>arr[i])
9. min=arr[i];
10.
11. [Link](min);
12. }
13.
14. public static void main(String args[]){
15. int a[]={33,3,4,5};//declaring and initializing an array
16. min(a);//passing array to method
17. }}
Test it Now

Output:
3

Returning Array from the Method


We can also return an array from the method in Java.

1. //Java Program to return an array from the method


2. class TestReturnArray{
3. //creating method which returns an array
4. static int[] get(){
5. return new int[]{10,30,50,90,60};
6. }
7.
8. public static void main(String args[]){
9. //calling method which returns an array
10. int arr[]=get();
11. //printing the values of an array
12. for(int i=0;i<[Link];i++)
13. [Link](arr[i]);
14. }}
Test it Now

Output:
10
30
50
90
60

Multidimensional Array in Java


In such case, data is stored in row and column based index (also
known as matrix form).

Syntax to Declare Multidimensional Array in Java

1. dataType[][] arrayRefVar; (or)


2. dataType [][]arrayRefVar; (or)
3. dataType arrayRefVar[][]; (or)
4. dataType []arrayRefVar[];

Example to instantiate Multidimensional Array in Java

1. int[][] arr=new int[3][3];//3 row and 3 column

Example to initialize Multidimensional Array in Java

1. arr[0][0]=1;
2. arr[0][1]=2;
3. arr[0][2]=3;
4. arr[1][0]=4;
5. arr[1][1]=5;
6. arr[1][2]=6;
7. arr[2][0]=7;
8. arr[2][1]=8;
9. arr[2][2]=9;

Example of Multidimensional Java Array


Let's see the simple example to declare, instantiate, initialize and
print the 2Dimensional array.

1. //Java Program to illustrate the use of multidimensional array


2. class Testarray3{
3. public static void main(String args[]){
4. //declaring and initializing 2D array
5. int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
6. //printing 2D array
7. for(int i=0;i<3;i++){
8. for(int j=0;j<3;j++){
9. [Link](arr[i][j]+" ");
10. }
11. [Link]();
12. }
13. }}
Test it Now

Output:
1 2 3
2 4 5
4 4 5

You might also like