Skip to main content
23 Jan Java Prog 2
Import Statement
import java.util.Arrays;- Imports the
Arrays class to use its utility methods like sort(), toString(), and copyOf().
Class Declaration
public class ArrayOperations {- Declares the public class
ArrayOperations.
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
numbers with elements {5, 3, 8, 1, 2}.
String[] names = {"Alice", "Bob", "Charlie", "David"};- Declares and initializes a string array
names with elements {Alice, Bob, Charlie, David}.
2. Traversing Arrays
System.out.println("Elements in numbers array:");- Prints a message indicating the start of
numbers array traversal.
for (int number : numbers) { ... }- Enhanced
for loop iterates over each element in numbers and prints them.
System.out.println("Elements in names array:");- Prints a message indicating the start of
names array traversal.
for (String name : names) { ... }- Enhanced
for loop iterates over each element in names and prints them.
3. Finding Length of Arrays
System.out.println("Length of numbers array: " + numbers.length);- Prints the length of
numbers array using the length property.
System.out.println("Length of names array: " + names.length);- Prints the length of
names array.
4. Searching in Arrays
int searchElement = 8;- Declares the element
8 to search for in numbers.
boolean found = false;- Declares a boolean variable
found to indicate whether the element is found.
for (int num : numbers) { ... }- Loops through
numbers to check if any element matches searchElement.
System.out.println("Is " + searchElement + " in numbers array? " + found);- Prints whether
searchElement is found in numbers.
5. Sorting Arrays
Arrays.sort(numbers);- Sorts the
numbers array in ascending order.
System.out.println("Sorted numbers array: " + Arrays.toString(numbers));- Converts
numbers to a string representation and prints it.
Arrays.sort(names);- Sorts the
names array alphabetically.
System.out.println("Sorted names array: " + Arrays.toString(names));- Converts
names to a string representation and prints it.
6. Copying Arrays
int[] copiedNumbers = Arrays.copyOf(numbers, numbers.length);- Creates a copy of the
numbers array.
System.out.println("Copied numbers array: " + Arrays.toString(copiedNumbers));
7. Reversing an Array (Manual Method)
int[] reversedNumbers = new int[numbers.length];- Declares an array
reversedNumbers to store the reversed elements.
for (int i = 0; i < numbers.length; i++) { ... }- Fills
reversedNumbers by iterating backward through numbers.
System.out.println("Reversed numbers array: " + Arrays.toString(reversedNumbers));- Prints the reversed array.
8. Calculating Sum and Average
int sum = 0;- Declares
sum to calculate the total of numbers.
for (int num : numbers) { sum += num; }- Adds each element of
numbers to sum.
double average = sum / (double) numbers.length;- Calculates the average by dividing
sum by the number of elements.
System.out.println("Sum of numbers array: " + 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
matrix with values.
System.out.println("Elements in 2D matrix:");- Prints a message before displaying
matrix elements.
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
rowSum for the current row.
rowSum += matrix[i][j];- Adds each element in the row to
rowSum.
System.out.println("Sum of row " + (i + 1) + ": " + rowSum);- Prints the sum of the current row.
Comments
Post a Comment