Is it better to use System.arraycopy(…) than a for loop for copying arrays?

前端 未结 7 1649
离开以前
离开以前 2020-11-28 20:28

I want to create a new array of objects putting together two smaller arrays.

They can\'t be null, but size may be 0.

I can\'t chose between these two ways: a

7条回答
  •  我在风中等你
    2020-11-28 21:03

    It depends on the virtual machine, but System.arraycopy should give you the closest you can get to native performance.

    I've worked for 2 years as a java developer for embedded systems (where performance is a huge priority) and everywhere System.arraycopy could be used, I've mostly used it / seen it used in existing code. It's always preferred over loops when performance is an issue. If performance isn't a big issue, I'd go with the loop, though. Much easier to read.

提交回复
热议问题