本文共 5672 字,大约阅读时间需要 18 分钟。
public class book { public static void main(String[] args) { Scanner input = new Scanner(System.in); ComparatorcircleComparator = new Comparator () { @Override public int compare(Circle o1, Circle o2) { return (int)(o2.getRadius()-o1.getRadius()); } }; PriorityQueue test = new PriorityQueue<>(1, circleComparator); for(int i=0;i<10;i++){ test.add(new Circle(20*Math.random())); } for(int i=0;i<10;i++){ System.out.println(test.remove().getRadius()); } }}class Circle{ private double radius; private double square; public Circle(double r){ this.radius = r; this.square = Math.PI*r*r; } public double getRadius(){ return this.radius; } public double getSquare(){ return this.square; }}
public class book { public static void main(String[] args) { Scanner input = new Scanner(System.in); PriorityQueueset1 = new PriorityQueue<>(); PriorityQueue set2 = new PriorityQueue<>(); set1.offer("George"); set1.offer("Jim"); set1.offer("John"); set1.offer("Blake"); set1.offer("Kevin"); set1.offer("Michael"); set2.offer("George"); set2.offer("Katie"); set2.offer("Kevin"); set2.offer("Michelle"); set2.offer("Ryan"); PriorityQueue union = new PriorityQueue<>(set1); for( String str: set2){ if(!union.contains(str)){ union.offer(str); } } PriorityQueue intersect = new PriorityQueue<>(); for(String str : set1){ if(set2.contains(str)){ intersect.offer(str); } } PriorityQueue diff = new PriorityQueue<>(); for(String str: set1){ if(!set2.contains(str)){ diff.offer(str); } } System.out.println(Arrays.toString(union.toArray())); System.out.println(Arrays.toString(intersect.toArray())); System.out.println(Arrays.toString(diff.toArray())); }}
public class book { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Please enter the code: "); String code = input.nextLine(); boolean isPair = true; Stacktest = new Stack<>(); for(int i=0;i
public class book { public static void main(String[] args) { Scanner input = new Scanner(System.in); MyPriorityQueuetest = new MyPriorityQueue<>(); test.offer("114"); test.offer("514"); MyPriorityQueue test1 = (MyPriorityQueue ) test.clone(); test1.offer("1919810"); System.out.println(Arrays.toString(test.toArray())); System.out.println(Arrays.toString(test1.toArray())); }}class MyPriorityQueue extends PriorityQueue implements Cloneable{ @Override protected Object clone(){ MyPriorityQueue myPriorityQueue = null; try{ myPriorityQueue = (MyPriorityQueue ) super.clone(); }catch (CloneNotSupportedException e){ e.printStackTrace(); } return myPriorityQueue; }}
🐎
public class book { public static void main(String[] args) { Scanner input = new Scanner(System.in); Stacktest = new Stack<>(); System.out.println("Please enter the RPN"); String rpn = input.nextLine(); rpn = rpn.replace(" ",""); for(int i=0;i = '0' && tmp <='9'){ test.push((char)(tmp-'0')); } else{ int op1 = (int)test.pop(); int op2 = (int)test.pop(); int res; switch (tmp){ case '+': res = op1 + op2; test.push((char)res); break; case '-': res = op1 - op2; test.push((char)res); break; case '*': res = op1 * op2; test.push((char)res); break; case '/': res = op1 / op2; test.push((char)res); break; default: break; } } } System.out.println("The result is "+(int)(test.pop())); }}
🐔
参见
👨🐰🌶
public class book { public static void main(String[] args) { Scanner input = new Scanner(System.in); String[] list2 = { "red","blue","green","yellow","orange","pink"}; Comparatorcomparator = new Comparator () { @Override public int compare(String o1, String o2) { return o1.charAt(o1.length()-1)-o2.charAt(o2.length()-1); } }; selectionSort(list2, comparator); System.out.println(Arrays.toString(list2)); } public static void selectionSort(T[] list, Comparator< ? super T> comparator){ for(int i=0;i
转载地址:http://hczai.baihongyu.com/