Table of Contents
Print All Elements of an Array
Given an array of integers, write a Java program to display all the elements stored in the array.
// Java program to print all elements
// of an array
public class PrintArray
{
public static void main(String[] args)
{
int[] arr = {10, 20, 30, 40, 50};
for(int i = 0; i arr.length; i++)
{
System.out.print(arr[i] + " ");
}
}
}
Output:
Explanation:10 20 30 40 50
The program traverses the array using a loop and prints each element.
Find the Sum of All Elements
Given an array of integers, write a Java program to calculate the sum of all elements in the array.
// Java program to find sum of
// all elements of array
public class SumArray
{
public static void main(String[] args)
{
int[] arr = {5, 10, 15, 20};
int sum = 0;
for(int num : arr)
{
sum += num;
}
System.out.println("Sum = " + sum);
}
}
Output:
Explanation:Sum = 50
Each element is added to the sum variable until the entire array is processed.
Find the Largest Element
Given an array of integers, write a Java program to find the largest element present in the array.
// Java program to find the largest element
public class LargestElement
{
public static void main(String[] args)
{
int[] arr = {12, 45, 78, 23, 56};
int largest = arr[0];
for(int i = 1; i arr.length; i++)
{
if(arr[i] largest)
{
largest = arr[i];
}
}
System.out.println("Largest Element = " + largest);
}
}
Output:
Largest Element = 78
Find the Smallest Element
Given an array of integers, write a Java program to find the smallest element present in the array.
// Java program to find the smallest element
public class SmallestElement
{
public static void main(String[] args)
{
int[] arr = {12, 45, 78, 23, 56};
int smallest = arr[0];
for(int i = 1; i arr.length; i++)
{
if(arr[i] smallest)
{
smallest = arr[i];
}
}
System.out.println("Smallest Element = " + smallest);
}
}
Output:
Smallest Element = 12
Calculate the Average of Array Elements
Given an array of integers, write a Java program to calculate the average value of all elements.
// Java program to calculate the average
// of array elements
public class AverageArray
{
public static void main(String[] args)
{
int[] arr = {10, 20, 30, 40, 50};
int sum = 0;
for(int num : arr)
{
sum += num;
}
double average = (double) sum / arr.length;
System.out.println("Average = " + average);
}
}
Output:
Average = 30.0
Search for an Element
Given an array and a target value, write a Java program to search for the target element and determine whether it exists in the array.
// Java program to search for
// an element in an array
public class SearchElement
{
public static void main(String[] args)
{
int[] arr = {10, 20, 30, 40, 50};
int target = 30;
boolean found = false;
for(int num : arr)
{
if(num == target)
{
found = true;
break;
}
}
if(found)
{
System.out.println("Element Found");
}
else
{
System.out.println("Element Not Found");
}
}
}
Output:
Element Found
Count Even and Odd Numbers
Given an array of integers, write a Java program to count the number of even and odd elements in the array.
// Java program to count even
// and odd numbers
public class EvenOddCount
{
public static void main(String[] args)
{
int[] arr = {1, 2, 3, 4, 5, 6};
int even = 0;
int odd = 0;
for(int num : arr)
{
if(num % 2 == 0)
even++;
else
odd++;
}
System.out.println("Even Count = " + even);
System.out.println("Odd Count = " + odd);
}
}
Output:
Even Count = 3
Odd Count = 3
Reverse an Array
Given an array of integers, write a Java program to print the elements in reverse order.
// Java program to reverse an array
public class ReverseArray
{
public static void main(String[] args)
{
int[] arr = {10, 20, 30, 40, 50};
for(int i = arr.length - 1; i = 0; i--)
{
System.out.print(arr[i] + " ");
}
}
}
Output:
50 40 30 20 10
Find the Second Largest Element
Given an array of integers, write a Java program to find the second largest element in the array.
// Java program to find the second
// largest element
public class SecondLargest
{
public static void main(String[] args)
{
int[] arr = {10, 50, 30, 70, 60};
int largest = Integer.MIN_VALUE;
int secondLargest = Integer.MIN_VALUE;
for(int num : arr)
{
if(num largest)
{
secondLargest = largest;
largest = num;
}
else if(num secondLargest && num != largest)
{
secondLargest = num;
}
}
System.out.println("Second Largest = " + secondLargest);
}
}
Output:
Second Largest = 60
Sort an Array in Ascending Order
Given an array of integers, write a Java program to sort the elements in ascending order.
// Java program to sort an array
// in ascending order
import java.util.Arrays;
public class SortArray
{
public static void main(String[] args)
{
int[] arr = {50, 20, 10, 40, 30};
Arrays.sort(arr);
for(int num : arr)
{
System.out.print(num + " ");
}
}
}
Output:
10 20 30 40 50
Conclusion
One-dimensional array problems are essential for mastering Java programming fundamentals. By practicing tasks such as finding sums, searching elements, reversing arrays, counting values, and sorting data, you can develop strong problem-solving skills and gain confidence in working with arrays. These concepts serve as a foundation for more advanced programming topics and interview preparation.Frequently Asked Questions
1. What is a one-dimensional array in Java?2. How do you find the length of an array in Java?A one-dimensional array is a collection of elements of the same data type stored in contiguous memory locations and accessed using a single index.
3. What is array traversal?You can use the length property of the array, such as arr.length.
4. How can an element be searched in an array?Array traversal is the process of visiting and processing each element of an array.
5. Why are array problems important?An element can be searched using linear search or binary search techniques.
Array problems help improve logical thinking, coding skills, and understanding of data structures.
0 Comments