ARRAY in Java

Array is a collection of multiple data of the same data type. for example, we can have an array of int type data or float type data. an array has a static memory allocation i.e memory size once allocated for an array cannot be changed. It’s Index starts from 0 to n number of index

Syntax:
data_type array_name[array_size];
Array in java
Array in java

2.BUBBLE sorting program OR(sort the elements of the array)

import java.util.*;
class sort
{
public static void main (String[] args)
{
int n,temp;
Scanner s=new Scanner(System.in);
System.out.print("Enter the number of element of the array");
n=s.nextInt();
Int a[]=new int[n];
System.out.println("Enter all the elements:");
for(int i=0;i<n;i++)
{
a[i]=s.nextInt();
}
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
System.out.print("Ascending Order:");
for(int i=0;i<n-1;i++)
{
System.out.print(a[i]+",");
}
System.out.print(a[n-1]);
}
} 
OUTPUT:
Enter the number of element of the array:
5
Enter all the elements:
1
4
3
2
5
Ascending Order:
1  2  3  4  5

3.Convert the array into a string

Import java.util.*;
import java.util.Arrays;
class Array
{
public static void main(String args[])
{
int[] arrays={222,444,555};
//call Arrays.toString to display the elements.
System.out.println(Arrays.toString(arrays));
}
}
OUTPUT:
[222,444,555]
Translate »