Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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]");

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Database Programming With Visual Basic.NET

Authors: Ying Bai

1st Edition

0521712351, 978-0521712354

More Books

Students also viewed these Databases questions

Question

18. If you have power, then people will dislike and fear you.

Answered: 1 week ago