Introduction
Sorting an array of strings involves arranging the strings in lexicographical (dictionary) order. This task is common in scenarios such as alphabetizing lists, sorting names, or organizing data. This guide will walk you through writing a Java program that sorts an array of strings in ascending order.
Problem Statement
Create a Java program that:
- Prompts the user to enter the size of an array and its string elements.
- Sorts the array of strings in lexicographical order.
- Displays the sorted array.
Example:
-
Input:
["apple", "banana", "cherry", "date"]
-
Output:
["apple", "banana", "cherry", "date"]
-
Input:
["dog", "cat", "elephant", "bear"]
-
Output:
["bear", "cat", "dog", "elephant"]
Solution Steps
- Read the Array Size and Elements: Use the
Scanner
class to take the size and elements of the string array as input from the user. - Sort the Array: Use the
Arrays.sort()
method to sort the array in lexicographical order. - Display the Sorted Array: Print the sorted array.
Java Program
// Java Program to Sort an Array of Strings // Author: https://www.rameshfadatare.com/ import java.util.Arrays; import java.util.Scanner; public class SortStrings { public static void main(String[] args) { // Step 1: Read the size and elements of the array from the user Scanner scanner = new Scanner(System.in); System.out.print("Enter the number of strings: "); int size = scanner.nextInt(); scanner.nextLine(); // Consume the newline String[] array = new String[size]; System.out.println("Enter the strings:"); for (int i = 0; i < size; i++) { array[i] = scanner.nextLine(); } // Step 2: Sort the array of strings Arrays.sort(array); // Step 3: Display the sorted array System.out.println("Sorted array of strings:"); for (String str : array) { System.out.println(str); } } }
Explanation
Step 1: Read the Array Size and Elements
- The
Scanner
class is used to read the size of the string array and its elements. ThenextInt()
method captures the size, and thenextLine()
method is used to capture each string.
Step 2: Sort the Array
- The
Arrays.sort()
method is used to sort the array in lexicographical order. This method sorts the elements in place, meaning the original array is modified to reflect the sorted order.
Step 3: Display the Sorted Array
- The program prints each element of the sorted array using a
for-each
loop.
Output Example
Example 1:
Enter the number of strings: 4 Enter the strings: apple banana cherry date Sorted array of strings: apple banana cherry date
Example 2:
Enter the number of strings: 4 Enter the strings: dog cat elephant bear Sorted array of strings: bear cat dog elephant
Example 3:
Enter the number of strings: 3 Enter the strings: java python c++ Sorted array of strings: c++ java python
Conclusion
This Java program demonstrates how to sort an array of strings in lexicographical order. The program covers essential concepts such as array manipulation, sorting, and input/output operations. This exercise is valuable for understanding how to handle and sort strings in Java programming.