What is array? How arrays in java are different? Explain different types of arrays with example.

Array

An array is a collection of similar type of elements which has contiguous memory location.

 arrays in java are different are described below:-

Normally, an array is a collection of similar type of elements which has contiguous memory location.

Java array is an object which 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.

Array in Java is index-based, the first element of the array is stored at the 0th index, 2nd element is stored on the 1st index, and so on.


Types of Array in java

There are two types of array.

  • Single Dimensional Array
  • Multidimensional Array

1. Single Dimensional Array in java
Syntax to Declare an Array in java
dataType[] arr; or
dataType []arr; or
dataType arr[]; 

Instantiation of an Array in java
arr_ref_var-new datatype [size];
Let's see the simple example of a java array, where we are going to declare instantiate, initialize and traverse an array.

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;

//printing array
for (int i=0;i<a.length; i++)//length is the property of array 
System.out.printIn(a[i]);
}
}

Output
10 
20
30
40
50

We can declare, instantiate and initialize the java array together by:
int a[]=[33,3,4,5);//declaration, instantiation and initialization
class Testarray
{
public static void main(String args[])
 {
int a[]=(3,7,9);//declaration, instantiation and initialization
//printing array
for(int i=0;i<a.length; i++)//length is the property of array 
System.out.printIn(a[i]);
}
}
output
3
7
9

2. Multidimensional array in java

In such cases, data is stored in row and column-based index which is also known as matrix form.

Syntax to Declare Multidimensional Array in java

dataType[][] ar;or

dataType [] []ar;or

dataType ar[][];or

dataType []ar[];

Example to instantiate Multidimensional Array in java

int[][] ar=new int [3] [3];//3 row and 3 column

Example of Multidimensional java array

Let's see the simple example to Declare instantiate, initialize and print the two-dimensional array.

import java.util.*;

class TestArray

{

public static void main(String args[])

{

int a[][]=new int[3][3];//declaration and instantiation

Scanner sc=new Scanner(System.in);

System.out.printIn("Enter elements");

for(int i=0;i<3;i++)

for(int j=0;j<3;j++)

a[i][j]=sc.nextint();

System.out.printIn("------Enter Elements Are----------\n");

for(int i=0;i<3;i++) ; 

{

for(int j=0;j<3;j++) ·

{

System.out.print(a[i][j]+" "); .. .

}

System.out.printIn();

}

}

}




Comments

Popular posts from this blog

What are different steps used in JDBC? Write down a small program showing all steps.

Explain Parallel Efficiency of MapReduce.

Suppose that a data warehouse for Big-University consists of the following four dimensions: student, course, semester, and instructor, and two measures count and avg_grade. When at the lowest conceptual level (e.g., for a given student, course, semester, and instructor combination), the avg_grade measure stores the actual course grade of the student. At higher conceptual levels, avg_grade stores the average grade for the given combination. a) Draw a snowflake schema diagram for the data warehouse. b) Starting with the base cuboid [student, course, semester, instructor], what specific OLAP operations (e.g., roll-up from semester to year) should one perform in order to list the average grade of CS courses for each BigUniversity student. c) If each dimension has five levels (including all), such as “student < major < status < university < all”, how many cuboids will this cube contain (including the base and apex cuboids)?