fvdcx's blog


  • 首页

  • 分类

  • 关于

  • 归档

  • 标签

Java this和super关键字

发表于 2017-02-02   |  

this关键字

参考了:https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html

  Java中this关键字通常有以下几个用法:

  1. 作为构造器中的指示隐式参数,如:

    1
    2
    3
    4
    5
    6
    public class Foo {
    private String a;
    public Foo(String a) {
    this.a = a;
    }
    }
  2. 引用同一个类中的其它构造器

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public class Foo {
    private String a;
    public Foo() {
    this("");
    }
    public Foo(String a) {
    this.a = a;
    }
    }

    这里默认构造起通过this关键字调用了另外一个构造器。

super关键字

  Java中super关键字通常有以下几种用途:

  1. 调用父类的方法

    1
    2
    3
    4
    5
    6
    public class Manager extends Employee {
    ....
    public double getSalary() {
    double b = super.getSalary(); // 调用父类的getSalary方法
    }
    }
  2. 在构造器中调用父类构造器方法

    1
    2
    3
    4
    5
    6
    7
    public class Manager extends Employee {
    private int bonus;
    public Manager(String n) {
    super(n); // 调用父类构造器方法
    bonus = 0;
    }
    }

    需要注意的是,如果在构造器中利用super调用超类的构造器,super语句要位于方法的第一条。而且如果子类的构造器没有显示地调用超类构造器,会自动调用超类的默认构造器。如果超类没有默认构造器,子类构造器中又没有显示调用超类的构造器,那么编译器会报错。

Java finally关键字以及finalize方法

发表于 2017-02-02   |   分类于 Java   |  

finally代码块

参考了 https://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html

  当try代码块退出以后,finally代码块总是执行,即使是unexpected exception发生,但是finally用处不仅仅是在异常处理方面,你可以在finally程序块里写一些cleanup代码,把cleanup代码写在finally块中是个好习惯。

但是如果JVM在执行try-catch代码中推出,finally块可能不执行。同样,如果执行try-catch代码的线程中断或者被杀死,finally块也可能不被执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
private List<Integer> list;
private static final int SIZE = 10;
public void writeList() {
PrintWriter out = null;
try {
System.out.println("Entered try statement");
out = new PrintWriter(new FileWriter("OutFile.txt"));
for (int i = 0; i < SIZE; i++) {
out.println("Value at: " + i + " = " + list.get(i));
}
} catch (IndexOutOfBoundsException e) {
System.err.println("IndexOutOfBoundsException: " + e.getMessage());
} catch (IOException e) {
System.err.println("Caught IOException: " + e.getMessage());
} finally {
if (out != null) {
System.out.println("Closing PrintWriter");
out.close();
} else {
System.out.println("PrintWriter not open");
}
}
}

  因此在finally块中写合适的代码能防止内存泄漏。

finalize方法

  Java有自动的垃圾回收器,不需要人工回收内存。但是一些文件或者系统资源的句柄有时候需要人工管理,finalize方法将在垃圾回收器清除对象之前调用。但是要注意,最好不要依赖finalize方法做清理工作,因为有可能在程序的执行生命周期中没有发生垃圾回收,垃圾回收发生的时刻是不确定的,所以通常不要指望finalize方法做清理工作。也可以stackoverflow的回答when is the finalize() method called in Java?

Java和C++的右移运算符

发表于 2017-02-02   |   分类于 C++   |  

  最近在看Core Java 第三章讲右移运算符>>中,强调了c/c++和java的不同,对于java右移运算,高位是用符号位进行填充,那么对于负数的右移是用1来填充,因此

1
2
int a = -3;
System.out.println(a >> 1); //输出-2

  书中说对于c/c++,是不确定的,如果对于非负数是没有歧义的,但是对于负数,是用0还是1来填充会选择效率高的一种执行,也就是说结果是未定义的,也可以理解成c/c++中>>只是为非负数定义的
  查阅了cpp reference

For negative a, the behavior of a << b is undefined.

For unsigned a and for signed a with nonnegative values, the value of a >> b is the integer part of a/2b
. For negative a, the value of a >> b is implementation-defined (in most implementations, this performs arithmetic right shift, so that the result remains negative).

In any case, if the value of the right operand is negative or is greater or equal to the number of bits in the promoted left operand, the behavior is undefined.

  也得到了印证,所以在c/c++中要对负数右移操作慎用。

leetcode-sliding-window-maximum

发表于 2017-02-02   |   分类于 算法 , leetcode   |  

题目大意

  https://leetcode.com/problems/sliding-window-maximum/

  给你一个数组nums和窗口大小k,窗口最开始左边界位于0的位置,之后每次右移一位,输出每个窗口内元素的最大值。

题目分析

  暴力方法O(n*k)可解,如果利用堆可以O(n * logk),但题目要求了复杂度是O(n),也就是和k无关的,我参考了discuss区的这篇解法:https://discuss.leetcode.com/topic/19067/clean-c-o-n-solution-using-a-deque 思路还是比较容易理解,利用双端队列(模拟窗口)维护一个递减序列,序列的头部就是窗口最大值,当然要注意如果元素个数超出窗口大小k,就要移除头部元素。

阅读全文 »
1…567…38
fvdcx

fvdcx

149 日志
14 分类
20 标签
GitHub
© 2015 - 2017 fvdcx
由 Hexo 强力驱动
主题 - NexT.Pisces