23 Jan Java Prog 2

 

Import Statement

  1. import java.util.Arrays;
    • Imports the Arrays class to use its utility methods like sort(), toString(), and copyOf().

Class Declaration

  1. public class ArrayOperations {
    • Declares the public class ArrayOperations.

Main Method

  1. public static void main(String[] args) {
    • Entry point of the program.

1. Declaring and Initializing Arrays

  1. int[] numbers = {5, 3, 8, 1, 2};
    • Declares and initializes an integer array numbers with elements {5, 3, 8, 1, 2}.
  2. String[] names = {"Alice", "Bob", "Charlie", "David"};
    • Declares and initializes a string array names with elements {Alice, Bob, Charlie, David}.

2. Traversing Arrays

  1. System.out.println("Elements in numbers array:");
    • Prints a message indicating the start of numbers array traversal.
  2. for (int number : numbers) { ... }
    • Enhanced for loop iterates over each element in numbers and prints them.
  3. System.out.println("Elements in names array:");
    • Prints a message indicating the start of names array traversal.
  4. for (String name : names) { ... }
    • Enhanced for loop iterates over each element in names and prints them.

3. Finding Length of Arrays

  1. System.out.println("Length of numbers array: " + numbers.length);
    • Prints the length of numbers array using the length property.
  2. System.out.println("Length of names array: " + names.length);
    • Prints the length of names array.

4. Searching in Arrays

  1. int searchElement = 8;
    • Declares the element 8 to search for in numbers.
  2. boolean found = false;
    • Declares a boolean variable found to indicate whether the element is found.
  3. for (int num : numbers) { ... }
    • Loops through numbers to check if any element matches searchElement.
  4. System.out.println("Is " + searchElement + " in numbers array? " + found);
    • Prints whether searchElement is found in numbers.

5. Sorting Arrays

  1. Arrays.sort(numbers);
    • Sorts the numbers array in ascending order.
  2. System.out.println("Sorted numbers array: " + Arrays.toString(numbers));
    • Converts numbers to a string representation and prints it.
  3. Arrays.sort(names);
    • Sorts the names array alphabetically.
  4. System.out.println("Sorted names array: " + Arrays.toString(names));
    • Converts names to a string representation and prints it.

6. Copying Arrays

  1. int[] copiedNumbers = Arrays.copyOf(numbers, numbers.length);
    • Creates a copy of the numbers array.
  2. System.out.println("Copied numbers array: " + Arrays.toString(copiedNumbers));
    • Prints the copied array.

7. Reversing an Array (Manual Method)

  1. int[] reversedNumbers = new int[numbers.length];
    • Declares an array reversedNumbers to store the reversed elements.
  2. for (int i = 0; i < numbers.length; i++) { ... }
    • Fills reversedNumbers by iterating backward through numbers.
  3. System.out.println("Reversed numbers array: " + Arrays.toString(reversedNumbers));
    • Prints the reversed array.

8. Calculating Sum and Average

  1. int sum = 0;
    • Declares sum to calculate the total of numbers.
  2. for (int num : numbers) { sum += num; }
    • Adds each element of numbers to sum.
  3. double average = sum / (double) numbers.length;
    • Calculates the average by dividing sum by the number of elements.
  4. System.out.println("Sum of numbers array: " + sum);
    • Prints the total sum.
  5. System.out.println("Average of numbers array: " + average);
    • Prints the calculated average.

9. Finding Max and Min Values

  1. int max = numbers[numbers.length - 1];
    • Retrieves the maximum value (last element in sorted array).
  2. int min = numbers[0];
    • Retrieves the minimum value (first element in sorted array).
  3. System.out.println("Max value in numbers array: " + max);
    • Prints the maximum value.
  4. System.out.println("Min value in numbers array: " + min);
    • Prints the minimum value.

10. Multi-Dimensional Arrays

  1. int[][] matrix = { ... };
    • Declares and initializes a 2D array matrix with values.
  2. System.out.println("Elements in 2D matrix:");
    • Prints a message before displaying matrix elements.
  3. for (int i = 0; i < matrix.length; i++) { ... }
    • Loops through each row of the matrix.
  4. for (int j = 0; j < matrix[i].length; j++) { ... }
    • Loops through each element in the current row.

11. Finding Row-Wise Sum in 2D Array

  1. System.out.println("Row-wise sums:");
    • Prints a message before calculating row sums.
  2. for (int i = 0; i < matrix.length; i++) { ... }
    • Iterates through rows of the matrix.
  3. int rowSum = 0;
    • Initializes rowSum for the current row.
  4. rowSum += matrix[i][j];
    • Adds each element in the row to rowSum.
  5. System.out.println("Sum of row " + (i + 1) + ": " + rowSum);
    • Prints the sum of the current row.

Comments

Popular posts from this blog

Programming Notes by Atul Kalukhe