Java programs-Array

 Sort an Array using inbuilt Methods:


package JavaPra;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collections;

public class SortingArray {

public static void main(String[] args) {
int years[]= {1988,2021,1991,2025};
//approach 1
Arrays.parallelSort(years);
System.out.println(Arrays.toString(years));

int yearsolderst[]= {1988,2021,1991,2025};
System.out.println("Before Sorting---"+Arrays.toString(yearsolderst));
//approach 2--Ascending order
Arrays.sort(yearsolderst);
System.out.println("After Sorting---"+Arrays.toString(yearsolderst));


////approach 3- decending order
Integer Tdates[]= {1988,2021,1991,2025};
Arrays.sort(Tdates, Collections.reverseOrder());
System.out.println(Arrays.toString(Tdates));
}
}

Output
[1988, 1991, 2021, 2025] Before Sorting---[1988, 2021, 1991, 2025] After Sorting---[1988, 1991, 2021, 2025] [2025, 2021, 1991, 1988]


EvenandOddNumber



package JavaPra;

public class EvenandOddNumber {

public static void main(String[] args) {
int []numbers ={11,12,13,14,15,16,17,18};

for(int x:numbers){
if (x%2==0){
System.out.println("Even ="+ x);


} else {
System.out.println("ODD =" + x);
}
}
}
}

Search number in an Array :


package JavaPra;

import java.util.Arrays;

public class SearchNumberArray {

public static void main(String[] args) {
int a[]= {10,25,30,12,50};
for (int i=0;i<a.length;i++){
// if (a[i]==30) {
System.out.println("Element is present in an Array index of " + i +" is "+ a[i]);

//}
}

for (int x:a){
if (x==30){
System.out.println("Element is present " + x);
}
}

}
}

Comments

Popular posts from this blog

Implicit and Explicit Waits,FluentWait,PageLoadTimeOut

A Interview Questions- selenium