Java version, use array implement same as C++ version