23 Jan Java Prog 2
Import Statement
import java.util.Arrays;- Imports the
Arraysclass to use its utility methods likesort(),toString(), andcopyOf().
- Imports the
Class Declaration
public class ArrayOperations {- Declares the public class
ArrayOperations.
- Declares the public class
Main Method
public static void main(String[] args) {- Entry point of the program.
1. Declaring and Initializing Arrays
int[] numbers = {5, 3, 8, 1, 2};- Declares and initializes an integer array
numberswith elements{5, 3, 8, 1, 2}.
- Declares and initializes an integer array
String[] names = {"Alice", "Bob", "Charlie", "David"};- Declares and initializes a string array
nameswith elements{Alice, Bob, Charlie, David}.
- Declares and initializes a string array
2. Traversing Arrays
System.out.println("Elements in numbers array:");- Prints a message indicating the start of
numbersarray traversal.
- Prints a message indicating the start of
for (int number : numbers) { ... }- Enhanced
forloop iterates over each element innumbersand prints them.
- Enhanced
System.out.println("Elements in names array:");- Prints a message indicating the start of
namesarray traversal.
- Prints a message indicating the start of
for (String name : names) { ... }- Enhanced
forloop iterates over each element innamesand prints them.
- Enhanced
3. Finding Length of Arrays
System.out.println("Length of numbers array: " + numbers.length);- Prints the length of
numbersarray using thelengthproperty.
- Prints the length of
System.out.println("Length of names array: " + names.length);- Prints the length of
namesarray.
- Prints the length of
4. Searching in Arrays
int searchElement = 8;- Declares the element
8to search for innumbers.
- Declares the element
boolean found = false;- Declares a boolean variable
foundto indicate whether the element is found.
- Declares a boolean variable
for (int num : numbers) { ... }- Loops through
numbersto check if any element matchessearchElement.
- Loops through
System.out.println("Is " + searchElement + " in numbers array? " + found);- Prints whether
searchElementis found innumbers.
- Prints whether
5. Sorting Arrays
Arrays.sort(numbers);- Sorts the
numbersarray in ascending order.
- Sorts the
System.out.println("Sorted numbers array: " + Arrays.toString(numbers));- Converts
numbersto a string representation and prints it.
- Converts
Arrays.sort(names);- Sorts the
namesarray alphabetically.
- Sorts the
System.out.println("Sorted names array: " + Arrays.toString(names));- Converts
namesto a string representation and prints it.
- Converts
6. Copying Arrays
int[] copiedNumbers = Arrays.copyOf(numbers, numbers.length);- Creates a copy of the
numbersarray.
- Creates a copy of the
System.out.println("Copied numbers array: " + Arrays.toString(copiedNumbers));- Prints the copied array.
7. Reversing an Array (Manual Method)
int[] reversedNumbers = new int[numbers.length];- Declares an array
reversedNumbersto store the reversed elements.
- Declares an array
for (int i = 0; i < numbers.length; i++) { ... }- Fills
reversedNumbersby iterating backward throughnumbers.
- Fills
System.out.println("Reversed numbers array: " + Arrays.toString(reversedNumbers));- Prints the reversed array.
8. Calculating Sum and Average
int sum = 0;- Declares
sumto calculate the total ofnumbers.
- Declares
for (int num : numbers) { sum += num; }- Adds each element of
numberstosum.
- Adds each element of
double average = sum / (double) numbers.length;- Calculates the average by dividing
sumby the number of elements.
- Calculates the average by dividing
System.out.println("Sum of numbers array: " + sum);- Prints the total sum.
System.out.println("Average of numbers array: " + average);- Prints the calculated average.
9. Finding Max and Min Values
int max = numbers[numbers.length - 1];- Retrieves the maximum value (last element in sorted array).
int min = numbers[0];- Retrieves the minimum value (first element in sorted array).
System.out.println("Max value in numbers array: " + max);- Prints the maximum value.
System.out.println("Min value in numbers array: " + min);- Prints the minimum value.
10. Multi-Dimensional Arrays
int[][] matrix = { ... };- Declares and initializes a 2D array
matrixwith values.
- Declares and initializes a 2D array
System.out.println("Elements in 2D matrix:");- Prints a message before displaying
matrixelements.
- Prints a message before displaying
for (int i = 0; i < matrix.length; i++) { ... }- Loops through each row of the matrix.
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
System.out.println("Row-wise sums:");- Prints a message before calculating row sums.
for (int i = 0; i < matrix.length; i++) { ... }- Iterates through rows of the matrix.
int rowSum = 0;- Initializes
rowSumfor the current row.
- Initializes
rowSum += matrix[i][j];- Adds each element in the row to
rowSum.
- Adds each element in the row to
System.out.println("Sum of row " + (i + 1) + ": " + rowSum);- Prints the sum of the current row.
Comments
Post a Comment