0% found this document useful (0 votes)
2 views

Java Arrays

Java arrays are data structures that store multiple values of the same data type in a single variable, with elements accessed via their index. Arrays can be declared and initialized, and their elements can be printed using standard output or a for-each loop. The document provides examples of array declaration, initialization, and traversal in Java.

Uploaded by

fahathaat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java Arrays

Java arrays are data structures that store multiple values of the same data type in a single variable, with elements accessed via their index. Arrays can be declared and initialized, and their elements can be printed using standard output or a for-each loop. The document provides examples of array declaration, initialization, and traversal in Java.

Uploaded by

fahathaat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Java Arrays

Arrays are used to store multiple values in a single variable, instead of declaring
separate variables for each value.

Array is a very common type of data structure which contains all the data values of
the same data type. The data items put in the array are called elements and the first
element in the array starts with index zero.

Declaring Arrays

Let's declare an array of 10 integer values.

This declaration declares an array named num that contains 10 integers. When the
compiler encounters this declaration, it immediately sets aside enough memory to
hold all 10 elements.

A program can access each of the array elements (the individual cells) by referring
to the name of the array followed by the subscript denoting the element (cell). For
example, the third element is denoted num[2].

int num [ ] = { 33, 3, 4 ,5 }; //declaration, and initialization

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


Initializing Arrays
Int [ ] num = new int [ 5 ];
num [ 0 ] = 100;
num [ 1 ] = 400;
num [ 2 ] = 300;
num [ 3 ] = 700;
num [ 4 ] = 600;
Printing Arrays
System.out.println (num [ 2 ] ) ;
System.out.println (num [ x ] ) ;
System.out.println (“Testing String “ + num [ 3 ] ) ;

Example:

class Testarray{
public static void main(String args[]){
int a[]=new int[5];//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;

//traversing array
for(int i=0; i<a.length; i++)//length is the property of array
System.out.println(a [ i ] );
}
}
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:

for (data_type variable : array){


//body of the loop
}

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

You might also like