import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Test{ public static void main(String[] args) { List<Student> students = new ArrayList<Student>(); students.add(new Student(10)); students.add(new Student(18)); students.add(new Student(15)); Collections.sort(students); System.out.print(students.get(1).age); } } class Student implements Comparable<Student> { Integer age; public Student(Integer age) { this.age = age; } public int compareTo(Student s) { return s.age.compareTo(this.age); } }A、15
7、现定义如下三个类,
public class A{ Private static A a = new A(); Private A(){}; Public static A getA(){ return a; } } public class B{ private static B b = null; private B(){}; public static B getB(){ b = new B(); return b; } } public class C{ private static C c = null; static{ c = new C(); } private C(){}; public static C getC(){ return c; } }
请问属于单例模式的类是( )
A、classA和classBimport java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); char[] charArray = scanner.nextLine().toCharArray(); int length = charArray.length; // int number[] = new int[length]; int lIndex = 0; int rIndex = length-1; int RCount = 0; int GCount = 0; while(lIndex<1||rIndex>=1) { if(lIndex<1&&charArray[lIndex]=='G') { GCount++; } lIndex++; if(rIndex>=1&&charArray[rIndex]=='R') { RCount++; } rIndex--; } int min = GCount + RCount; for(int i=1;i<length;i++) { if(charArray[i]=='R') { if(RCount>0) RCount--; } else if(charArray[i]=='G') { GCount++; } if(min>GCount+RCount) min = GCount + RCount; } int ToGNumber = 0; for(int i=0;i<length;i++) { if(charArray[i]=='R') ToGNumber++; } if(ToGNumber<min) min = ToGNumber; System.out.println(min); } }2、
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int a = scanner.nextInt(); int b = scanner.nextInt(); int c = scanner.nextInt(); int max = length(a,b,c); int middle = length(a,c,b); if(max<middle) max = middle; middle = length(b,c,a); if(max<middle) max = middle; middle = length(c,b,a); if(max<middle) max = middle; middle = length(c,a,b); if(max<middle) max = middle; middle = length(b,a,c); if(max<middle) max = middle; System.out.println(max); } public static int length(int x,int y,int z) { if(x + y <= z) { z = x + y - 1; } if(x + z <= y) { y = x + z - 1; } if(y + z <= x) { x = y + z - 1; } return x + y + z; } }3、
import java.util.Scanner; import java.util.Stack; public class Main { public static void main(String[] args) { Stack<Character>stack = new Stack<Character>(); Scanner scanner = new Scanner(System.in); char[]chars = scanner.nextLine().toCharArray(); int max = 0; for (char c:chars ) { if(c=='(') { stack.push(c); if(max<stack.size()) max = stack.size(); } else if(c==')') stack.pop(); } System.out.println(max); } }