Array is a data structure in java, which enable you to put more than one
value of like type into a single variable. These are data structures that are
used to store one or more than one item of data in a single variable that
has various index positions. In each array there are some indexes and
each elements in an array has an index that is equal to 0, the second
index is equal to 1, and so on.
Example 1: Basic Array Operations
Problem Statement: Declare an array of strings with as many elements as
the number of fruits that exists in the world and assign it the following
values five. Now print each fruit name using a loop.
Explanation: You will form a string array containing the fruit names, and
you will fill in the array and then print out the name using a loop.
public class FruitArray {
public static void main(String[] args) {
String[] fruits = {"Apple", "Banana", "Cherry", "Date", "Elderberry"};
System.out.println("Fruits in the array:");
for (String fruit : fruits) {
System.out.println(fruit);
}
}
}
Example 2: The main objective of this work is to identify the minimum
element in an array.
Problem Statement: Creating a method that would accept an array of
integers and then returns the minimum value of that array.
Explanation: You will construct an algorithm that traverses the map and
remembers the data at the lowest index.
public class MinInArray {
public static void main(String[] args) {
int[] numbers = {12, 5, 8, 20, 3};
int min = findMin(numbers);
System.out.println("Minimum value: " + min);
}
public static int findMin(int[] array) {
int min = array[0];
for (int num : array) {
if (num < min) {
min = num;
}
}
return min;
}
}
Example 3: Merging Two Arrays
Problem Statement: A method which enters two arrays of integers as
parameters and returns the merged single array.
Explanation: You will introduce a new array with a number of elements
that is equal with the sum between the two input arrays and, then you will
put the elements for each array in the new array that you created.
public class MergeArrays {
public static void main(String[] args) {
int[] array1 = {1, 2, 3};
int[] array2 = {4, 5, 6};
int[] mergedArray = mergeArrays(array1, array2);
System.out.println("Merged array:");
for (int num : mergedArray) {
System.out.print(num + " ");
}
}
public static int[] mergeArrays(int[] array1, int[] array2) {
int[] merged = new int[array1.length + array2.length];
int index = 0;
for (int num : array1) {
merged[index++] = num;
}
for (int num : array2) {
merged[index++] = num;
}
return merged;
}
}
Example 4: Let’s count how many of them are there time needed.
Problem Statement: Describe a function which should accept an array of
integer numbers and a single integer number, and should return the
number of times this number was found in the array.
Explanation: In this question, you are going to start the pointer from the
beginning of the array and whenever the pointer reaches your given
value, you need to put an increment on that pointer.
public class CountOccurrences {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 2, 4, 2, 5};
int valueToCount = 2;
int count = countOccurrences(numbers, valueToCount);
System.out.println("Value " + valueToCount + " occurs " + count + "
times.");
}
public static int countOccurrences(int[] array, int value) {
int count = 0;
for (int num : array) {
if (num == value) {
count++;
}
}
return count;
}
}
Example 5: Sorting an Array
Problem Statement: Develop a function that accept an array of integer
and sort that array using the Bubble Sort technique.
Explanation: To sort the elements of the array you will use Bubble Sort
algorithm.
public class BubbleSort {
public static void main(String[] args) {
int[] numbers = {64, 34, 25, 12, 22, 11, 90};
bubbleSort(numbers);
System.out.println("Sorted array:");
for (int num : numbers) {
System.out.print(num + " ");
}
}
public static void bubbleSort(int[] array) {
int n = array.length;
boolean swapped;
for (int i = 0; i < n - 1; i++) {
swapped = false;
for (int j = 0; j < n - i - 1; j++) {
if (array[j] > array[j + 1]) {
// Swap array[j] and array[j + 1]
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
swapped = true;
}
}
if (!swapped) {
break; // If no swaps were made, the array is already sorted
}
}
}
}