public class ArrayLinear
public static void main(String[] args) {
// TODO Auto-generated method stub
int [] arr = {10,202,303,404,50,60,70,80,90,100};
int key =4;
int i=0;
while (i<arr.length) {
if (key ==arr[i])
{
System.out.println(key +"is prenseted poision ");
break;
}
i=i+1;
}
if(i==arr.length)
{
System.out.println(key +"is not prsented ");
}
}
}
Reverse Array:
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr [] = { 10,20,30,40,50,60,70,80,90,100};
int i=0;
int j = arr.length-1;
System.out.println("Before reversing");
for (int k=0;k<arr.length;k++)
{
System.out.println(arr[k]+" ");
}
while(i<j)
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
i=i+1;
j=j-1;
}
System.out.println(" ");
System.out.println("after reversing");
for (int k=0;k<arr.length;k++)
{
System.out.println(arr[k]+" ");
}
}
}
How many times element present:
public class HowManyEleInArr {
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr [] = {10,202,303,505,202,606,202,707,202,909};
int key =202;
int i=1; int j = 0;
while (i<arr.length)
{
if(key==arr[i])
{
j++;
}
i=i++;
}
if (j>0)
{
System.out.println(key +"is present "+j +"times");
}
else
{
System.out.println("not presented");
}
}
}
Array left shift:
public class LeftShiftArr {
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[]= {10,20,30,40,50};
System.out.println("before left shift");
for (int j=0;j<arr.length;j++)
{
System.out.println(arr[j]+" ");
}
int temp= arr[0];
int i=0;
while (i<arr.length-1)
{
arr[i]= arr[i+1];
i=i+1;
}
arr[i]=temp;
System.out.println (" ");
System.out.println (" after left shift");
for (int j=0; j<arr.length;j++)
{
System.out.println(arr[j]+" ");
}
}
}
Array Right shif:
public class RightShiftArr {
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[]= {10,20,30,40,50};
System.out.println("before right shift");
for (int j=0;j<arr.length;j++)
{
System.out.println(arr[j]+" ");
}
int temp= arr[arr.length-1];
int i = arr.length-1;
while (i>0)
{
arr[i]=arr [i-1];
i=i-1;
}
arr[0]=temp;
System.out.println (" ");
System.out.println (" after right shift");
for (int j=0; j<arr.length;j++)
{
System.out.println(arr[j]+" ");
}
}
}
Array left shit twice:
public class LeftTwiceArr {
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[]= {10,20,30,40,50};
System.out.println("before right shift");
for (int j=0;j<arr.length;j++)
{
System.out.println(arr[j]+" ");
}
int temp1= arr[0];
int temp2= arr[1];
int i=0;
while (i <arr.length-2)
{
arr[i]= arr[i +2];
i=i+1;
}
arr[i]= temp1;
arr[i+1]=temp2;
System.out.println (" ");
System.out.println (" after left shift twice");
for (int j=0; j<arr.length;j++)
{
System.out.println(arr[j]+" ");
}
}
Smallest number in array:
public class SmallestArr {
public static void main (String[] args )
{
int[] arr= {10,20,30,40,50,-5};
int small =Integer.MAX_VALUE;
int i=0;
while (i<arr.length)
{
if (arr[i]<small)
{
small =arr[i];
}
i++;
}
System.out.println("smallest num in arr "+ small);
}
}
Storing negative Number:
public class negativeArr {
public static void main (String[] args )
{
int[] arr1 = {10,20,-30,-40,50};
int count =0;
for (int i=0; i<arr1.length;i++)
{
if (arr1[i]<0)
{
count++;
}
int [] arr2= new int [count];
int j=0;
System.out.println("arr2 elements are:");
for (int j1=0;j1<arr1.length;j1++)
{
if (arr1[j1]<0)
{
arr2[j1]= arr1[i];
System.out.println(arr2[j1]+" ");
if (j1==count)
break;
j1++;
}
}
}
}
}
Find negative number in Array:
public class FindNegative {
public static void main(String[] args) {
// TODO Auto-generated method stub
int [] arr = {-10,20,-30,40,-50};
for (int i=0;i<arr.length;i++)
{
if (arr[i]<0)
{
System.out.println(arr[i]);
}
}
}
}
Biggest element in Array:
public class FindBiggest {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr = {10,20,30,400,50};
int biggest =Integer.MIN_VALUE;
int i=0;
while (i<arr.length)
{
if (arr[i]>biggest)
{
biggest = arr[i];
}
i++;
}
System.out.println("Biggest number is " +biggest);
}
}
Copy another Array:
public class copyAnotherArr
public static void main(String[] args) {
// TODO Auto-generated method stub
int [] a= {10,20,30,40,50};
int [] b= new int [a.length];
System.out.println("before copied");
for (int k=0;k<b.length;k++)
{
System.out.println (b[k]+" ");
}
int i=0; int j = b.length-1;
while (i<b.length)
{
b[i]=a[j];
i++;
j--;
}
System.out.println(" ");
System.out.println(" after copied");
for (int k=0; k<b.length;k++) {
System.out.println(b[k]+ " ");
}
}
Copy only odd index Output:
public class oddIndexvalue {
public static void main (String[] args)
{
int[] arr1 = {10,20,30,40,50};
int oddIndex=arr1.length/2;
int [] arr2=new int [oddIndex];
int j=0;
for (int i=1;i<arr1.length;i+=2);
{
int i = 0;
arr2[j]=arr1[i];
j++;
}
System.out.println ("Printing arry 2:");
for (int i=0;i<arr2.length;i++)
{
System.out.println (arr2[i]+" ");
}
}
}