23 Jan Java Prog 2
Import Statement
import java.util.Arrays;
- Imports the
Arrays
class 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
numbers
with elements{5, 3, 8, 1, 2}
.
- Declares and initializes an integer array
String[] names = {"Alice", "Bob", "Charlie", "David"};
- Declares and initializes a string array
names
with 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
numbers
array traversal.
- Prints a message indicating the start of
for (int number : numbers) { ... }
- Enhanced
for
loop iterates over each element innumbers
and prints them.
- Enhanced
System.out.println("Elements in names array:");
- Prints a message indicating the start of
names
array traversal.
- Prints a message indicating the start of
for (String name : names) { ... }
- Enhanced
for
loop iterates over each element innames
and prints them.
- Enhanced
3. Finding Length of Arrays
System.out.println("Length of numbers array: " + numbers.length);
- Prints the length of
numbers
array using thelength
property.
- Prints the length of
System.out.println("Length of names array: " + names.length);
- Prints the length of
names
array.
- Prints the length of
4. Searching in Arrays
int searchElement = 8;
- Declares the element
8
to search for innumbers
.
- Declares the element
boolean found = false;
- Declares a boolean variable
found
to indicate whether the element is found.
- Declares a boolean variable
for (int num : numbers) { ... }
- Loops through
numbers
to check if any element matchessearchElement
.
- Loops through
System.out.println("Is " + searchElement + " in numbers array? " + found);
- Prints whether
searchElement
is found innumbers
.
- Prints whether
5. Sorting Arrays
Arrays.sort(numbers);
- Sorts the
numbers
array in ascending order.
- Sorts the
System.out.println("Sorted numbers array: " + Arrays.toString(numbers));
- Converts
numbers
to a string representation and prints it.
- Converts
Arrays.sort(names);
- Sorts the
names
array alphabetically.
- Sorts the
System.out.println("Sorted names array: " + Arrays.toString(names));
- Converts
names
to a string representation and prints it.
- Converts
6. Copying Arrays
int[] copiedNumbers = Arrays.copyOf(numbers, numbers.length);
- Creates a copy of the
numbers
array.
- 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
reversedNumbers
to store the reversed elements.
- Declares an array
for (int i = 0; i < numbers.length; i++) { ... }
- Fills
reversedNumbers
by 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
sum
to calculate the total ofnumbers
.
- Declares
for (int num : numbers) { sum += num; }
- Adds each element of
numbers
tosum
.
- Adds each element of
double average = sum / (double) numbers.length;
- Calculates the average by dividing
sum
by 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
matrix
with values.
- Declares and initializes a 2D array
System.out.println("Elements in 2D matrix:");
- Prints a message before displaying
matrix
elements.
- 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
rowSum
for 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