Monday, 26 November 2018

Arrays in Java | Java Arrays and their types |Java Arrays method - Online Help


      Guyz in this post we are discussing arrays in java, come on let's see what are all the types available in Java arrays. An array is a group of related data items that share a common name with the same data type. An individual variable in the array is called an array element.

we can define an array name roll no to represent a set of roll no of a group of students. A particular value is indicated by a number called index number. An index number is present in brackets after the array name.
To create an array, you need to perform three steps:
1. Declare the array
 2.Create memory space and
3. Store data into the created memory.


The advantage of java array:

- Code optimization: It makes the code optimize; we can retrieve or sort the data easily.
- Random access: we can get any data located at an index position.

The disadvantage of java array:

- Size limit: we can store the only fixed size of elements in the array. It does not grow its size at runtime. To solve this problem, collection framework is used in java.

One dimensional array:

A list of items-group in a single variable name with only one index is called 1-D array.

1.Declaration of the one-dimensional array:

we can declare an array in java using the subscript operator. The general form is:

                               datatype var_name[];

is equivalent to

                              datatype[] var_name[];

here, the data type is valid java data type and var_name is the name of the array.

If you guys want to know about Java Datatype and Java variables in detail, just go for the link below:

data types in Java:
https://beingcomputeracy.blogspot.com/2018/11/data-types-in-java-java-programming.html

variables in java:
https://beingcomputeracy.blogspot.com/2018/11/variables-in-java.html

Example:
int number[];
int[] number;

2.Create memory space:

After the declaration of an array, we must allocate a memory space for the declared array. This is done with the help of a new operator. The general form is :

                               array_name=new datatype[size];

Example:
number=new int[10];
avg=new float[10];

It is also possible to combine the declaration and creation into one.

The general form is

                              datatype arrayname[]=new datatype[size];

Example:
int number[]=new int[10];
float avg[]=new float[10];

3.Initialization of one-dimensional arrays:

  We can store values at the time of declaration. The compiler allocates the required space depending upon the list of values. The general form is:
                     
                            datatype array_name[]={list of values};

Sample Program:
class arraydemo1
{
publis static void main(String args[])
{
int days[]={31,28,31,30,31};
system.out.println("february has "+days[1]+"days");
}
}

The output of this program is:
february has 28 days

Two Dimensional array:

A list of items group in a single variable name with two indexes (row and column size)is called a 2-D array.

Creation of 2-D array:

We can create two dimensional array as follows:
                   
                    datatype array_name[][]=new datatype[size1][size2];

Here,
size1=number of rows
size2=number of columns

Example:
int arr[][];
arr[][]=new int[2][3];

is equivalent to

int arr[][]=new int[2][3];

Structure of two-dimensional arrays as shown here:

arr[0][0]


arr[0][1]

arr[0][2]
arr[1][0]

arr[1][1]
arr[1][2]


Sample program:
class matrix
{
public static void main(String args[])
{
int x[][]={{1,2,3},{4,5,6}};
for(int i=0;i<2;i++)
{
system.out.prinln()
for(int j=0;j,3;j++)
system.out.print(x[i][j]+"\t");
}
}
}

The output of the above program is shown here:
1 2 3
4 5 6
        

No comments:

Post a Comment