ARRAY IN JAVA

Maha

ARRAY IN JAVA 

  • An  array in java is an object that includes components of the same data type. The items of an array are kept in a single memory address.
  • In the most cases, an array is a collection of similar type elements stored in a single memory address. 
  • In a java array, we can only store a fixed number of elements.
  • The first element of an array in java is saved at the 0th index, the second element is stored at the 1st index, and so on.
  • In java, an array is a dynamically produced class object. 

Types of Array in java:

There are two types of array;

             ðŸ‘‰ Single Dimensional Array
             ðŸ‘‰ Multidimensional Array

Single Dimensional Array:


A Single dimensional array is a a group of elements with same data type which stored in a linear arrangement  under a single variable name.

syntax:

dataType[] arr;
       (or)
dataType []arr;
      (or)
dataType arr[];

Instantiation of an Array:

arrayRefVar = new datatype[size];
 

Example:

class demotarray
{
public static void main(String args[])
{
int a[]=new int[5];
a[0]=100;
a[1]=200;
a[2]=300;
a[3]=400;
a[4]=500;
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}
}

Output:

100
200
300
400
500


Multi dimensional Array:

Multidimensional Array can be defined in simple words as arrays.Data in multidimensional arrays are stored in tabular from in row major order.

Syntax:

datatype [1st  dimentional][2nd  dimension][]..[Nth imension]array_name = new data_[size1][size2]....[sizeN];

Example:

class demoarray3
{  
public static void main(String args[])
{  
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};  
for(int i=0;i<3;i++)
{  
for(int j=0;j<3;j++)
{  
System.out.print(arr[i][j]+" ");  
}  
System.out.println();  
}  
}
  

Output:

1 2 3 
2 4 5 
4 4 5 




Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send