集合类容器比较是否相等时,比较的是内部的值,而不是这两个容器的句柄(引用):
import java.util.*; public class Main { public static void main(String[] args) throws Exception{ Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.nextLine()); List<String> v = new ArrayList<>(); for(int i = 0; i < n; ++i) v.add(sc.nextLine()); List<String> p = new ArrayList<>(v); Collections.sort(p); boolean a = p.equals(v); //集合类容器比较是否相等 boolean b = true; for(int i = 0; i < v.size() - 1; ++i){ if(v.get(i).length() > v.get(i + 1).length()){ b = false; break; } } if(a && b){ System.out.println("both"); return; } if(a){ System.out.println("lexicographically"); return; } if(b){ System.out.println("lengths"); return; } System.out.println("none"); } }