在许多情况下,我们需要删除StringBuilder/StringBuffer的最后一个字符.例如,给定a int[]{1,2,3}
,实现String toString(int[] a)
方法,用逗号分隔符联系每个元素.输出应该是1,2,3
,没有拖尾逗号.
我们可以轻松编写一个循环:
int[] nums = new int[]{1,2,3,4,5};
StringBuilder sb = new StringBuilder();
for (int i = 0; i < nums.length; i++) {
sb.append(nums[i]);
sb.append(",");
}
//here we need to remove the tailing ','
Run Code Online (Sandbox Code Playgroud)
但我们总是需要去除拖尾','
.有两种方法可以实现它:
sb.deleteCharAt(sb.length() - 1);
Run Code Online (Sandbox Code Playgroud)
和
sb.setLength(sb.length() - 1);
Run Code Online (Sandbox Code Playgroud)
推荐哪一个?为什么?
注意:我知道做了Arrays.toString
什么.这只是一个描述我的问题的例子,可能不太合适.
这不是关于字符串连接的讨论,而是StringBuffer/StringBuilder的最佳实践.
我想在apache phoenix中导出现有表的模式.是否有一些命令或工具可以像show create table TABLE_NAME
在mysql中那样做?
谢谢
根据4.0中的文档New和值得注意的,netty4提供了一个新的bootstrap API,doc提供了以下代码示例:
public static void main(String[] args) throws Exception {
// Configure the server.
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 100)
.localAddress(8080)
.childOption(ChannelOption.TCP_NODELAY, true)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(handler1, handler2, ...);
}
});
// Start the server.
ChannelFuture f = b.bind().sync();
// Wait until the server socket is closed.
f.channel().closeFuture().sync();
} finally {
// …
Run Code Online (Sandbox Code Playgroud) 我使用的是netty 4.1.0CR,官方代码示例建议我使用NioEventLoopGroup
启动服务器和客户端,如下:
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup);
Run Code Online (Sandbox Code Playgroud)
但是我在 Linux(CentOS 6)上同时运行服务器和客户端,我应该使用它EpollEventLoopGroup
来获得更好的性能吗?或者我如何决定使用哪一个?
我只是注意到当我发出git pull
命令时,我得到了一个Could not chdir to home directory
. 我在 Mac OSX 上,所以我的主目录不在/home/xxxxxx
. 为什么会发生这种情况?为什么gitchdir
在进行pull过程时需要回家?
$ git pull
Could not chdir to home directory /home/xxxxx: No such file or directory
Current branch mainline is up to date.
Run Code Online (Sandbox Code Playgroud) 正如Netty New中所介绍的那样,4.0中值得注意:
channelOpen,channelBound和channelConnected已合并到channelActive.channelDisconnected,channelUnbound和channelClosed已合并到channelInactive.同样,Channel.isBound()和isConnected()已合并到isActive().
请注意,channelRegistered和channelUnregistered不等同于channelOpen和channelClosed.它们是为支持频道的动态注册,注销和重新注册而引入的新州,如下所示:
恕我直言,在TCP/IP的情况下,当一个频道未注册时,相应的套接字被关闭,怎么可能re-register
再次?
众所周知,在Sun(Oracle)JDK中,HashSet
实现了一个支持HashMap
,以重用复杂的算法和数据结构.但是,有可能实现一个MyHashMap
使用java.util.HashSet
作为它的背面?如果可能,怎么样?如果没有,为什么?
请注意,此问题仅是对编码技巧的讨论,不适用于生产场景.
假设在Oracle中有一个名为DISTANCES的表,该表的浮动类型列名为distance。距离范围是[0,1000]。我想知道距离的分布,例如,在以下每个范围中有多少行:(0,10],(10,50],(50,100],(100,500],.. 。(5000,10000]。
如何建立这个SQL查询?