Java – Arrays

Java – Introduction to Arrays

Array: Array creation:                         int[]  arr = {3, 7, 1, 9, 5, 2, 7, 6}; Memory representation: Find the Length of Array: length: class Code{      public static void main(String[] args)      {                  int[] arr = {4, 2, 8, 9, 1, 6, 7, 4};                  System.out.println(“Length is : ” + arr.length);      }} Array Object: Creating empty array with …

Java – Introduction to Arrays Read More »

Java – Arrays – Basic Programs

Java – Display first element of array class Code{      public static void main(String[] args)      {                  int[] arr = {4, 2, 8, 9, 1, 6, 7, 4};                  int first = arr[0];                  System.out.println(“First element is : ” + first);      }} It is recommended to check the array contains elements or not before accessing elements: class Code{      public …

Java – Arrays – Basic Programs Read More »

Java – Display Array Elements

Java – Program to display array elements class Code{      public static void main(String[] args)      {                  int[] arr = {4, 2, 8, 9, 1, 6, 7, 5};                  System.out.println(“Array elements are : “);                  for (int i=0 ; i<=arr.length-1 ; i++)                  {                              System.out.println(arr[i]);                  }      }} Java – Program to display default values of array class Code{      public static …

Java – Display Array Elements Read More »

Java – For Each Loop

Java – Program to display array elements using for each loop class Code{      public static void main(String[] args)      {                  int[] arr = {4, 2, 8, 9, 1, 6, 7, 5};                  System.out.println(“Array elements using for-each loop : “);                  for (int x : arr)                  {                              System.out.println(x);                  }      }} Java – Replace the first element and last element …

Java – For Each Loop Read More »

Java – Array Programs

Java – Check the first element Even or not class Code{      public static void main(String[] args)      {                  int[] arr = {4, 2, 8, 9, 1, 6, 7, 5};                  int first = arr[0];                  if(first%2==0)                              System.out.println(“First element is Even”);                  else                              System.out.println(“First element is not Even”);      }} Java – Check the sum of first and last elements is …

Java – Array Programs Read More »

Java – Array Median and Swap Elements

Display the median value of array class Code{            public static void main(String[] args)            {                        int[] arr = {6, 2, 8, 9, 1, 6, 7, 5};                        int n = arr.length;                        if(n%2!=0)                        {                                    System.out.println(“Mean : ” + arr[n/2]);                        }                        else{                                    int x = arr[n/2-1];                                    int y = arr[n/2];                                    System.out.println(“Mean : ” + ((x+y)/2));                        }            }} Java – …

Java – Array Median and Swap Elements Read More »

Java – Sum of Array elements

Java – Program to find the sum of array elements class Code{            public static void main(String[] args)            {                        int[] arr = {6, 3, 9, 1, 2, 8, 4, 5};                        int sum=0;                        for (int i=0 ; i<arr.length ; i++)                        {                                    sum = sum + arr[i];                        }                        System.out.println(“Sum of elements : ” + sum);            }} Java – …

Java – Sum of Array elements Read More »

Java – Print Even number in Array

Java – Program to print even numbers of array using for each loop class Code{            public static void main(String[] args)            {                        int[] arr = {6, 3, 9, 1, 2, 8, 4, 5};                                               System.out.println(“Even numbers of array : “);                        for (int x : arr)                        {                                    if(x%2==0)                                                System.out.println(x);                        }            }} Java – Program to display odd numbers …

Java – Print Even number in Array Read More »

Java – Print Prime Numbers in the Array

Java – Print only prime numbers in the given array class Code{            public static void main(String[] args)            {                        int[] arr = {6, 3, 9, 1, 2, 8, 4, 5};                        for (int i=0 ; i<arr.length ; i++){                                    int n = arr[i];                                    int count=0;                                    for (int j=1 ; j<=n ; j++)                                    {                                                if(n%j==0)                                                            count++;                                    }                                    if(count==2)                                                System.out.println(n …

Java – Print Prime Numbers in the Array Read More »

Java – Reverse Array Elements

class Code{            public static void main(String[] args) {                        int[] arr = {10, 20, 30, 40, 50};                        int i=0;                        int j=arr.length-1;                        while(i<j){                                    int temp = arr[i];                                    arr[i] = arr[j];                                    arr[j] = temp;                                    i++;                                    j–;                        }                        System.out.println(“Reverse array is : “);                        for (int x : arr){                                    System.out.println(x);                        }            }}

Scroll to Top