create a class called Dwarf that has a name (String) and a height (int). In addition, when Dwarfs are placed in a LinkedList and sorted, they will be sorted from tallest to shortest. import java.util.*; public class Main
create a class called Dwarf that has a name (String) and a height (int). In addition, when Dwarfs are placed in a LinkedList and sorted, they will be sorted from tallest to shortest.
import java.util.*;
public class Main
{
public static void main(String[] args)
{
ArrayList<Dwarf> l = new ArrayList<Dwarf>();
l.add(new Dwarf("Sleepy", 14));
l.add(new Dwarf("Dopey", 17));
l.add(new Dwarf("Doc", 9));
l.add(new Dwarf("Happy", 22));
l.add(new Dwarf("Sneezy", 29));
l.add(new Dwarf("Bashful", 11));
l.add(new Dwarf("Grumpy", 4));
Collections.sort(l);
for (Dwarf d : l)
System.out.println(d.getName());
}
}
output
Sneezy
Happy
Dopey
Sleepy
Bashful
Doc
Grumpy
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images