In: Computer Science
7.2 Lab 7 Sorting with Arrays.java
For more tips, including lecture slides, demonsration code and video hints, please see the Lab7 handout (part A)
For this lab you will explore using four different versions of Arrays.sort()to sort arrays:
Follow these steps:
//Import Arrays
/**
* Lab Test driver for Sorting Arrays
*/
public class SortingArrays
{
public static void main(String [] args)
{
System.out.println("Problem 1:");
//Initialize an array of ints
Integer [] ages = {34,22,29,19,25};
//Initialize an array of Strings
String [] names = {"John","Carrie","Alex","Zuri","Tom"};
//Sort the arrays using Arrays.sort
//Sort a sectoin of the ages array from index 2 to the end of the
array
//Sort a section of the names array from index 0 to index 2
inclusive
//print the arrays
printArray(ages);
printArray(names);
//Now sort the entire arrays using Arrays.sort
//Sort ages completely
//Sort names completely
//print the arrays
printArray(ages);
printArray(names);
}
/**
* Print an array of integers to the screen
*/
private static void printArray(Object[] a)
{
for (int k=0; k<a.length; k++)
{
System.out.print(a[k] + " \n");
}
System.out.println();
}
}
Solution:-
Here is the required code. Comments have been added to properly explain what is being done.
import java.util.Arrays;
public class SortingArrays {
public static void main(String [] args)
{
System.out.println("Problem 1:");
//Initialize an array of ints
Integer [] ages = {34,22,29,19,25};
//Initialize an array of Strings
String [] names = {"John","Carrie","Alex","Zuri","Tom"};
//Sort the arrays using Arrays.sort
// Here we have used the Arrays.sort() function to sort sections of the ages and names array. We will use the overloaded Arrays.sort() function which will take the following inputs:
// First parameter: The function takes in the first input as the array to be sorted.
// Second parameter: The second input to the function is the starting index of the section of the array to be sorted. This is inclusive.
// Third parameter: The third input to the function is the end index of the section of the array to be sorted. This is exclusive.
//Sort a section of the ages array from index 2 to the end of the array
// We have used the ages.length function as the end index because we want to sort from position 2 to the end of the array.
Arrays.sort(ages,2,ages.length);
//Sort a section of the names array from index 0 to index 2 inclusive
// We have used the end position as 3 because this is exclusive. Since we need to sort from positions 0 to 2, hence we need to specify the end position as (2+1) = 3.
Arrays.sort(names,0,3);
//print the arrays
printArray(ages);
printArray(names);
//Now sort the entire arrays using Arrays.sort
// To sort the entire arrays of ages and names, we will use the overloaded Arrays.sort() method which only takes in the array as a parameter.
// This method will sort the entire array by default.
//Sort ages completely
Arrays.sort(ages);
//Sort names completely
Arrays.sort(names);
//print the arrays
printArray(ages);
printArray(names);
}
/**
* Print an array of integers to the screen
*/
private static void printArray(Object[] a)
{
for (int k=0; k<a.length; k++)
{
System.out.print(a[k] + " \n");
}
System.out.println();
}
}