Category Archive : Array Examples

1. Write a program to sort numeric numbers.

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] numbers={14,12,9,15,17,21,23,5,8,25};
        Arrays.sort(numbers);
        System.out.println(Arrays.toString(numbers));

    }
}
Output:
[5, 8, 9, 12, 14, 15, 17, 21, 23, 25]

2. Write a

Read more
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] numbers={14,12,9,15,17,21,23,5,8,25};
        System.out.println("Given array: "+Arrays.toString(numbers));
        Arrays.sort(numbers);
        System.out.println("Second largest number: "+numbers[numbers.length-2]);
        System.out.println("Second smallest number: "+numbers[1]);
    }

}
Output:
Given array: [14, 12, 9, 15, 17, 21, 23, 5, 8, 
Read more
public class Main {
    public static void main(String[] args) {
        int[] numbers={14,12,9,15,17,21,23,8,5,25};
        int max=findMax(numbers);
        int min=findMin(numbers);
        System.out.println("Max number is: "+max);
        System.out.println("Min number is: "+min);
    }
    public static int findMax(int arr[]){
        int max=0;
        for(int i=0;i<arr.length;i++){
            if(arr[i]arr[0]){
                max=arr[i];
            }
        }
        return 
Read more
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] numbers={14,12,9,15,17,21,23,5,8,25};
        System.out.println("Before inserting:\n"+Arrays.toString(numbers));
        String newArr=insertValue(numbers,2,6); //inserting 6 in array 2nd index
        System.out.println("After inserting:");
        System.out.println(newArr);

    }
    public static String insertValue(int[] arr, int arrayIndex, int value){
        for(int i=arr.length-1;iarrayIndex;i--){
            
Read more
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        System.out.println("[14,12,9,15,17,21,23,5,8,25]");
        System.out.println("Enter a number that contains in the given array:\n");
        Scanner scanner=new Scanner(System.in);
        int value=scanner.nextInt();
        int[] numbers={14,12,9,15,17,21,23,5,8,25};
        int index=findArrayIndex(numbers,value);
        System.out.println("Array index is: "+index);

    }
    public static int findArrayIndex(int[] 
Read more

What is array:

Array is a special type of variable that is used for storing similar types of data. Suppose, 10 students are in a class. you can store name of the students in an array and retrieve any data.… Read more