赞
踩
目录
数组不是原生类,数组使用基本类型来定义的一个顺序表。
数组的大小不能随意改变,不能自动进行扩容。
数组是对象,对象存储在堆上。
按顺序执行静态代码块。
- // Test.main() 函数执行后的输出是()
- public class Test {
- public static void main(String[] args) {
- System.out.println(new B().getValue());
- }
-
- static class A {
- protected int value;
-
- public A(int v) {
- setValue(v);
- }
-
- public void setValue(int value) {
- this.value = value;
- }
-
- public int getValue() {
- try {
- value++;
- return value;
- } catch (Exception e) {
- System.out.println(e.toString());
- } finally {
- this.setValue(value);
- System.out.println(value);
- }
- return value;
- }
- }
-
- static class B extends A {
- public B() {
- super(5);
- setValue(getValue() - 3);
- }
-
- public void setValue(int value) {
- super.setValue(2 * value);
- }
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
子类重写父类方法,默认优先调用子类重写后的方法。
按照题目的要求写即可,就是遍历数组,判断是否按长度排序,以及字典序,判断字典序可以使用 Object 类自带的 compareTo 方法来比较。
代码实现:
- import java.util.Scanner;
-
- // 注意类名必须为 Main, 不要有任何 package xxx 信息
- public class Main {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- // 注意 hasNext 和 hasNextLine 的区别
- while (in.hasNextInt()) { // 注意 while 处理多个 case
- int n = in.nextInt();
- String[] arr = new String[n];
- for (int i = 0; i < n; i++) {
- arr[i] = in.next();
- }
- print(arr, n);
- }
- }
-
- public static void print(String[] arr, int n) {
- // 长度
- boolean flg1 = true;
- // 字典序
- boolean flg2 = true;
- for (int i = 0; i < n - 1; i++) {
- if (arr[i].length() >= arr[i + 1].length()) {
- flg1 = false;
- }
- if (arr[i].compareTo(arr[i + 1]) >= 0) {
- flg2 = false;
- }
- }
- if (flg1 && flg2) {
- System.out.println("both");
- } else if (flg1) {
- System.out.println("lengths");
- } else if (flg2) {
- System.out.println("lexicographically");
- } else {
- System.out.println("none");
- }
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。