Array is a collection of similar data items. Genarally,operations on array includes finding a maximum number in array, reversing the array elements, sorting and searching.
This blog includes two java programs to illustrate array
operations.
1.Java Program to find biggest number in the array:
This
program includes the below steps.
- · Include the built in package java.util.Scanner.
- · A public class MaxElement is created with main() function.
- · Read the number of elements in an array as input.
- · Using a loop(for loop), get all the elements to array.
- · Set a variable max to first element of array.
- · Check each element with max value to find the biggest value.
- · If the array element is greater than max, set the max as array element.
- · otherwise, repeat the process.
- · Finally, print the maximum value of the array.
#Java program to find the biggest array element.
import java.util.Scanner;
public class MaxElement {
public static void
main(String[] args) {
Scanner
scanner = new Scanner(System.in);
System.out.print("Enter the number of elements");
int no =
scanner.nextInt(); // Read the number of elements
int[] a = new
int[no]; // Array declaration
int j=0;
System.out.println("Enter " + no + " elements: ");
for (j = 0;
j< no; j++) {
a[j] =
scanner.nextInt(); // get the array elements
}
for (int num :
a) {
if (num
> max) {
max =
num;
}
}
System.out.println("The Biggest array element is:"+max);
}
}
The output is as follows….
C:\raji\blog>javac MaxElement.java
C:\raji\blog>java MaxElement
Enter the number of elements 5
Enter 5 elements:
11
23
76
45
18
The Biggest array element is:76
Next program is to reverse the elements in array.
2.Java Program to reverse the elements in the array:
This
program is to reverse the elements from the beginning to last.
Steps:👇
- As a beginning, import the built in package java.util.scanner.
- A class is created with main() function.
- First,get the number of elements of the array.
- Read the array elements.
- Declare the two variables as Sta and end. Using this variables, repeat the process from starting element to last element to reverse the array.
- Finally,print the array reversed.
#Java Program to reverse the elements in the array:
import java.util.Scanner;
public class ReverseElement {
public static void
main(String[] args) {
Scanner
scanner = new Scanner(System.in);
System.out.print("Enter the number of elements");
int no =
scanner.nextInt(); // Read the number of elements
int[] a = new
int[no]; // Array declaration
int j=0;
System.out.println("Enter " + no + " elements: ");
for (j = 0;
j< no; j++) {
a[j] =
scanner.nextInt(); // get the array elements
}
int sta = 0;
int end =
a.length - 1;
while (sta
< end) {
int temp =
a[sta];
a[sta] =
a[end];
a[end] =
temp;
sta++;
end--;
}
System.out.print("The Reversed array is:");
System.out.println("");
for(j=0;j<no;j++)
{
System.out.println(a[j]);
}
}
}
Output:
C:\raji\blog>javac ReverseElement.java
C:\raji\blog>java ReverseElement
Enter the number of elements5
Enter 5 elements:
54
34
23
67
87
The Reversed array is:
87
67
23
34
54
These are the java programs to perform array operations.
Keep coding!!!!
No comments:
Post a Comment