Question

In: Computer Science

Java basic sorting problem Supposed I have a simple array list storing the telephone numbers. How...

Java basic sorting problem

Supposed I have a simple array list storing the telephone numbers. How can I sort the numbers in descending order with different ways?

Give ArrayList<String> tel = ["11223344", "55442211", "99881122", "99002211", "34446666", "12342353"]

I come up a solution using Collections.sort(tel), but it requires a compare method and I have no idea its contents and also the position of the method.

Would you suggest 2 or 3 ways and write the code to achieve my purpose?

Solutions

Expert Solution

You can use Collections.sort(tel,Collections.reverseOrder()); to sort the list in descending order.

Or you can first sort the list and then reverse.

Collections.sort(tel);
Collections.reverse(tel);

This will sort the list in descending order.

Sample program and output

import java.util.ArrayList;
import java.util.Collections;

public class Test {
    public static void main(String[] args) {
        ArrayList<String> tel = new ArrayList();
        tel.add("11223344");
        tel.add("55442211");
        tel.add("99881122");
        tel.add("99002211");
        tel.add("34446666");
        tel.add("12342353");
        ArrayList<String> copy = (ArrayList<String>) tel.clone();
        System.out.println("List: "+copy);
        Collections.sort(tel,Collections.reverseOrder());
        System.out.println("List after sort in 1st way: "+tel);
      
        System.out.println("\nList: "+copy);
        Collections.sort(copy);
        System.out.println("List after sort: "+copy);
        Collections.reverse(copy);
        System.out.println("List after sort and reverse: "+copy);
      
    }

}


Related Solutions

Programming Language: JAVA In this assignment you will be sorting an array of numbers using the...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the bubble sort algorithm. You must be able to sort both integers and doubles, and to do this you must overload a method. Bubble sort work by repeatedly going over the array, and when 2 numbers are found to be out of order, you swap those two numbers. This can be done by looping until there are no more swaps being made, or using a...
Java Programming I need an application that collects the user input numbers into an array and...
Java Programming I need an application that collects the user input numbers into an array and after that calls a method that sums all the elements of this array. and display the array elements and the total to the user. The user decides when to stop inputting the numbers. Thanks for your help!
Using import java.util.Random, create a simple java code that populates an array with 10 random numbers...
Using import java.util.Random, create a simple java code that populates an array with 10 random numbers and then outputs the largest number.
How do I convert the integers in my 2D array list to output decimals numbers? My...
How do I convert the integers in my 2D array list to output decimals numbers? My program works accepting regular integers, but crashes when decimals are entered into my 2D array list. Can someone improve it so it accepts decimal numbers? Here is my code: import javax.swing.*; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class TwoDimArray { private int numbers[][]; public TwoDimArray() { loadArray(); } public void loadArray() { /* //loadArray() method loads the users defined filename //@return returns the...
Write a simple java program to list roman numeral for a given range of numbers. Roman...
Write a simple java program to list roman numeral for a given range of numbers. Roman numerals are represented by seven different symbols: I, V, X, L, C, D, and M. I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000 Roman numerals are usually written largest to smallest from left to right. But a number like 4 is not written as IIII. It is written as IV. Because...
JAVA Problem 1: [15 marks] Find the average of an array of numbers (filename: FindAverage.java) Write...
JAVA Problem 1: [15 marks] Find the average of an array of numbers (filename: FindAverage.java) Write two overloaded methods with the following headers to find the average of an array of integer values and an array of double values: public static double average(int[] num) public static double average(double[] num) In the main method, first create an array of integer values with the array size of 5 and invoke the first method to get and display the average of the first...
I am in beginners java course so using the most simple/basic JAVA please complete the following...
I am in beginners java course so using the most simple/basic JAVA please complete the following CODE SEGMENTS. (2 parts) FIRST PART: Using ITERATIVE create a METHOD MAX that returns the max element in an ArrayList of ints and prints all of the elements. *you have to use an iterative in the method.* (just need to write the METHOD ONLY not a whole program(will do in 2nd part), assume you are given any numbers/integers. SECOND PART: Now write a class...
Solve this comp architecture problem: Supposed you have an array int* arr = {3,7,6,4,5}. The values...
Solve this comp architecture problem: Supposed you have an array int* arr = {3,7,6,4,5}. The values in arr store in the memory of 0(x21), 8(x21), .... , 32(x21). Transform the following RISC-V code into a C program. ld x5, 16(x21) addi x6, x0, 3 sll x5, x5, x6 sd x5, 8(x21)
I need a basic and simple Java code for this exercise: (Check password) Some websites impose...
I need a basic and simple Java code for this exercise: (Check password) Some websites impose certain rules for passwords. Write a method that checks whether a string is a valid password. Suppose the password rules are as follows: ■ A password must have at least eight characters. A character is alpha or digit. ■ A password consists of only letters and digits. No other symbols. ■ A password must contain at least two digits. Write a program that prompts...
[JAVA] How to do this problem. we have to tell if given list of three number...
[JAVA] How to do this problem. we have to tell if given list of three number is in ascending, descending or no order. Instructions You will be given three integers: ?, ? and ? (read in for you). Then you will be given two boolean values that tell you which way the numbers are going - if the numbers are in increasing order or in decreasing order, respectively. Details Input The program reads in: a sequence of three integers: ?,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT