Question
StringSorter.java import java.util.Collections ; import java.util.ArrayList ; import java.util.Arrays ; /** Sorting a list of strings alphabetically, using the Collections sort method. The Comparable interface
StringSorter.java
import java.util.Collections ;
import java.util.ArrayList ;
import java.util.Arrays ;
/**
Sorting a list of strings alphabetically, using the Collections sort method.
The Comparable interface is a commonly used interface in Java. Look
up the Comparable interface in the API documentation. It requires
a compareTo method, such that
a.compareTo(b)
returns
1 if a is larger than b
-1 if a is smaller than b
0 if a and b are the same
Since compareTo determines which of two objects is larger, this
interface is used to decouple sorting methods from the classes that
are sorted. The String class implements Comparable, so we
can sort strings alphabetically using a sort method that sorts
Comparable objects.
The Collections.sort method is one such method.
Here we make a list of strings and print the list and then print it
after sorting.
*/
public class StringSorter
{
public static void main(String[] args)
{
String[] array = {"blue", "red", "yellow", "cyan", "orange", "purple", "mauve", "brown"} ;
ArrayList list = new ArrayList() ;
for (int i = 0; i < array.length; i++)
list.add(array[i]);
System.out.println("--------------original array/list:") ;
System.out.println(Arrays.toString(array));
//-----------Start below here. To do: approximate lines of code = 1
//-----------Start below here. To do: approximate lines of code = 1
// 1. sort the array called array using Arrays.sort
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
System.out.println("-------------sorted array:") ; //
System.out.println(Arrays.toString(array));
System.out.println("Expected:[blue, brown, cyan, mauve, orange, purple, red, yellow]");
//-----------Start below here. To do: approximate lines of code = 1
//-----------Start below here. To do: approximate lines of code = 1
// 2. Sort the arrayList called list using Collections.sort()
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
System.out.println("-------------sorted list:") ; //
System.out.println(list);
System.out.println("Expected:[blue, brown, cyan, mauve, orange, purple, red, yellow]");
}
}
P.s: please besides the code, also include a screenshot of code, and the output to confirm code doesn't get edited out and the output satisfies the problem!!
Purpose: practice using an interface (Comparable) and to see how simple it is to use the sort () method of the Collections class on any list of objects where the object implements Comparable. Sorting strings alphabetically (ascending). Difficulty: Easy See the following files: * StringSorter.java (has todo) Approximate total lines of code required: 2
Step by Step Solution
There are 3 Steps involved in it
Step: 1
StringSorterjava import javautilCollections import javautilArrayList import javautilArrays Sorting a ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started