java
共 80 篇文章
static blocks in java
这篇 文章可谓极好。 java里面的static code block在jvm装载类文件的时候按照顺序执行,如果其中调用了其他的static方法会去执行那些static方法。
2012-12-21 14:45 · java
Java的annotation
从oracle的官方文档来学习annotations: 地址在: http://docs.oracle.com/javase/tutorial/java/javaOO/annotations.html 总结如下: Annotations我理解就是程序的一些元数据,没有它程序也是ok的。它的主要用途体现在以下几个方面: 替代以前简单的注释; 比如以前的注释可能是: /* author: Benjamin Franklin date: 3/27/2010 */ class My Class {} 用Annotations可以写成: @Author( name = "Benjamin Frankl…
2012-12-20 15:32 · java
Google Protocol Buffers
http://blog.jeoygin.org/2011/09/rpc-framework-protocol-buffers.html 这是我在网上找到的关于PB的比较好的一个文章.其他的博客都只讲了PB作为序列化的功能,在其用作RPC方面鲜有讲到,这篇给出了一个使用第三方基于PB的RPC实现的一个例子。 就像作者说的: protocol buffers并没有实现RPC通信,可以使用第三方的RPC实现protobuf-socket-rpc,下载地址是: http://protobuf-socket-rpc.googlecode.com/files/protobuf-socket-rpc-2.…
2012-11-09 11:01 · java
stack with min operation
How would you design a stack which, in addition to push and pop, also has a function min which returns the minimum element? Push, pop and min should all operate in O(1) time. public class StackWithMin extends Stack<NodeWithMin> { public void push(int value) { int newMin = Math.min(value, min()); su…
Rotation of String
Problem: Assume you have a method isSubstring which checks if one word is a substring of another. Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to isSubstring (i.e., “waterbottle” is a rotation of “erbottlewat”). 这个问题的trick就是将两个原字符串拼起来,然后检测。。。
算法:去除字符串中的重复字母
问题:去除字符串中的重复字符,不能使用额外空间(1或者2个变量除外)。 很简单的练手题,但是发现很难写正确。 public class DuplicateChar { //return length of the final string public static int removeDuplicateChar(char []str){ int len = str.length; if(len < 2) return len; for(int i=0;i<len-1;i++){ char ch = str[i]; int index = i+1; for(int j=i+1;j<=len-…
Print all binary search trees
Problem: Given numbers 1,2,3...N, print all binary search trees that can be constructed with these N numbers. Solution: package alg; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class AllBST { class Node<T extends Comparable<?>> { Node<T> left, right; T da…
find the subtree with max sum
Problem: Given a binary tree, each node has an integer value attached (can be negative), write code to find the subtree with the max sum. My solution: Basically this is a recursive problem, for each subtree result, you should remember its maxSum and corresponding root node. So I use a struct SubTre…
2012-09-18 15:04 · java
Find all valid parentheses
Problem: Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-pairs of parentheses. 给定一个正整数N,打印所有可能的N对括号序列,例子如下。 EXAMPLE: input: 3 (e.g., 3 pairs of parentheses) output: ()()(), ()(()), (())(), ((())) My Code: public class Parenthese { public static void pr…
Find the previous and next nearest number with same 1 bits
Problem: Given an integer, print the next smallest and next largest number that have the same number of 1 bits in their binary representation Solution from CareerCup book: public static boolean GetBit(int n, int index) { return ((n & (1 << index)) > 0); } public static int SetBit(int n, int index, …
can we create an object of an interface?
Definitely, the answer is no! But look at the code below: import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; public class EmployeeSort { static Comparator<Employee> NAMESORT = new Comparator<Employee>(){ public int …
2012-07-25 15:55 · java
Insufficiently Synchronized Java Code
I'm reading "Java Concurrency in Practice". Section 3.1.1 talks about it may cause surprising results when you do not provide enough synchronization to your shared variables. Take a look at the example in the book: public class NoVisibility { private static boolean ready; private static int number;…
2012-07-09 17:01 · java
one java interview question
int f(int n){ return n*f(n-1); } System.out.print(f(100)); what's the output?
2012-07-02 18:03 · java
How to generate permutations recursively
I have posted code to calcuate permutations in order, this is another more short recursive one of generating non-order permutations. package alg_test; public class Permutation2 { void swap(int a[], int i, int j) { int temp = a[i]; a[i]=a[j]; a[j]= temp; } void printArray(int a[]){ for(int i=0;i<a.l…
2011-06-18 17:02 · java
static synchronized methods?
Keep in mind that using synchronized on methods is really just shorthand (assume class is SomeClass): synchronized static void foo() { ... } is the same as static void foo() { synchronized(SomeClass.class) { ... } } and synchronized void foo() { ... } is the same as void foo() { synchronized(this) …
2010-11-16 21:41 · java
java question
1. what is error? what is exception? 2. String vs. StringBuffer vs. StringBuilder 3. final vs. finally vs. finalize 4. synchronized(a){ synchronized(b){ } } any problems? 5. way to avoid GC 6. way to avoid dead lock 7. Collection<?> vs. Collection<Object> 8. Design pattern in java library? pattern …
2010-11-12 11:23 · java
GC 知识
基本回收算法 引用计数(Reference Counting) 比较古老的回收算法。原理是此对象有一个引用,即增加一个计数,删除一个引用则减少一个计数。垃圾回收时,只用收集计数为0的对象。此算法最致命的是无法处理循环引用的问题。 标记-清除(Mark-Sweep) 此算法执行分两阶段。第一阶段从引用根节点开始标记所有被引用的对象,第二阶段遍历整个堆,把未标记的对象清除。此算法需要暂停整个应用,同时,会产生内存碎片。 复制(Copying) 此算法把内存空间划为两个相等的区域,每次只使用其中一个区域。垃圾回收时,遍历当前使用区域,把正在使用中的对象复制到另外一个区域中。此算法每次只处理正在使用…
2010-11-09 09:46 · java