1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
emmasim [6.3K]
2 years ago
10

Write the following generic method that sorts an ArrayList of Comparable items. The sort method must use the compareTo method.

Computers and Technology
1 answer:
Inessa05 [86]2 years ago
6 0

Answer:

Explanation:

The following code is written in Java, it prompts the user to enter 10 integers and saves them to an ArrayList. Then it prompts for 5 strings and saves them to another ArrayList. Then, it calls the sort method and adds the lists as parameters. Finally, it prints out both lists completely sorted in ascending order.

import java.util.ArrayList;

import java.util.Scanner;

class Division{

   public static double division(double a, double b) throws Exception {

       if(b == 0)

           //throw new Exception("Invalid number.");

           return (a / b);

       return a / b;

   }

   public static void main(String[] args) {

       Scanner in = new Scanner(System.in);

       ArrayList<Integer> mylist2 = new ArrayList<>();

       for (int x = 0; x < 10; x++) {

           System.out.println("Please Enter a Number: ");

           int number = in.nextInt();

           mylist2.add(number);

       }

       ArrayList<String> mylist = new ArrayList<>();

       for (int x = 0; x < 5; x++) {

           System.out.println("Please Enter a Word: ");

           String word = in.nextLine();

           mylist.add(word);

       }

       sort(mylist);

       sort(mylist2);

       for (String x: mylist) {

           System.out.print(x + ", ");

       }

       System.out.println("");

       for (int x: mylist2) {

           System.out.print(x + ", ");

       }

   }

   public static <E extends Comparable<E>> ArrayList<E> sort(ArrayList<E> list) {

       E temp;

       if (list.size()>1) // check if the number of orders is larger than 1

       {

           for (int x=0; x<list.size(); x++) // bubble sort outer loop

           {

               for (int i=0; i < list.size() - x - 1; i++) {

                   if (list.get(i).compareTo(list.get(i+1)) > 0)

                   {

                       temp = list.get(i);

                       list.set(i,list.get(i+1) );

                       list.set(i+1, temp);

                   }

               }

           }

       }

       return list;

}}

You might be interested in
Researching the history of computers is a good troubleshooting technique.<br> True or False?
Verdich [7]

Yes very much so! You could learn previous mistakes/bugs/etc and find solutions to fixing them and avoiding getting them. As well as much more. History is always a fun subject for anything, really in my opinion.

4 0
2 years ago
A mobile device you are troubleshooting is experiencing a sharp decrease in performance after an hour of operation. The user pow
Anna71 [15]

Answer:

Close or disable all unused running apps.

Explanation:

Closing programs running in the background on your computer or mobile device helps to free up system resources for your other programs. These problems can be resolved where your system is running slowly or two programs are trying to use the same device hence causing the device or system to drag.

5 0
3 years ago
Which security scanner runs in a web browser and doesn't require that you install a product before scanning
balandron [24]

Answer:

It’s OSI. also your question cut off lol

4 0
3 years ago
Suppose you have two tables: officer and gentleman. what question(s) can be asked by using the union operator?
Alik [6]
The union of two tables is basically the total of the two tables, so "or" questions would be my guess.
8 0
2 years ago
Answer to this if you have apex legends on either pc xbox one or PS4 for the one that answers and does gets brainliest please do
weqwewe [10]

Answer:

I got it on Ps4

3 0
2 years ago
Read 2 more answers
Other questions:
  • Pete Jones, a bait shop owner, incorporates ______ within a webpage to entice customers to buy a new lure.
    7·1 answer
  • With a(n) ____ data table you can vary the value in one cell.
    5·1 answer
  • How is a cell named?
    9·1 answer
  • Hy i am new anybody here​
    10·2 answers
  • A device (or a software program on a computer) that can monitor data traveling on a network is known as a socket sniffer. ______
    15·1 answer
  • Computer with a domain name is called a
    7·1 answer
  • Technician A says that it's a good idea to perform a test drive before attempting repairs. Technician B says that it's a good id
    9·2 answers
  • Optimizing a PC’s performance often involves utilities that can defragment disks, clean a system’s Registry, scan for malware, r
    14·1 answer
  • The _______ within a story are the people and/or objects that the story is about
    5·2 answers
  • Describe accessibility in Windows 7 ​
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!