array in java

Array in java-: Arrays are  the collection of same data type in a large value called the array.  Array are the part of data structure in Java. Data structures are tools that allow us to store and access sequences of information rather than using individual variables. Variables are great when we have one specific piece of information that we need in our local programming space but data structures are used when we want to store large or complicated sets or series of information

Declaring and initializing an array in java-: Like any other programming language it must be declaring the array. Let’s begin by taking a look at the syntax behind declaring and initializing an array in Java. The syntax of  an array to come into being with enough space to hold eight characters.

char[] arrayVar = new char[8]; 

To the left of our assignment operator (=), the syntax looks pretty familiar, not unlike the syntax we would use when declaring any other primitive or object. We begin by telling Java what type of element we are going to declare here. In this condition we are declaring a character array. The empty square brackets lets Java know that rather than creating a single character variable, we would  like to declare an array type variable because our array is a variable just like any other c programming,c++ programming,etc.

 

Assigning values to an array in java-: The location of our array is accessed by our program when we call the arrayVar variable. This allows us to run lines of code such as the following:

arrayVar[2] = 'c'; 

Our arrayVar variable gives us access, essentially, to eight different character variables. When we do not want to assign a new array to our arrayVar variable, we are  probably going to be accessing these character variables as individuals. We do this simply using the variable name of arrayVar, following it with square brackets, which include the index of the individual character we would like to access. when our computers count indexes, they almost always begin with 0. So, in Java, our seven-character array has these indexes: 0, 1, 2, 3, 4

Types of array in java -: As we know that the same data type in large volume called the array. According the volume the array has some type.

  • Single dimensional array in java
  • Multidimensional array in java

 

Single Dimensional array in java -: In the java all the data are store in one variable it is called the single dimensional array in java.

Syntax of  single dimensional array  we can declare three type as like

datatype [] array; or
datatype[] array;or
datatype array [];

Example of one  dimensional array- in this example we take a example in this variable name is “a” and class name is “netnic”

class netnic{
public static void main(String args[]){

int a[]=new int[8];//declaration and instantiation
a[0]=15;
a[1]=28;
a[2]=62;
a[3]=48;
a[4]=55;
a[5]=68;
a[6]=89;
a[7]=72;

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

}}


Output of this programme

15
28
62
40
55
68
89
72

Multidimensional array in java-: arrays are unwieldy and we want to use more strongly organised data structures so that they can be easier for humans to understand and write programs around. “Multidimensional array” is a pretty scary-sounding name but in fact the concept behind it is very basic.

The basic syntax are same as like

datatype [][] array; or
datatype[][] array;or
datatype array [][];

Example of multidimensional array

class netnic{
public static void main(String args[]){

//declaring and initializing array
int arr[][]={{1,2,3},{2,4,5},{4,4,5},{5,6,7}};

//printing array
for(int i=0;i<3;i++){
 for(int j=0;j<3;j++){
   System.out.print(arr[i][j]+" ");
 }
 System.out.println();
}

output of this programme

1 2 3 
2 4 5 
4 4 5

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *