This is the 21st day of my participation in the August Text Challenge.More challenges in August

One practical tip every day: array copy

To be honest, in actual business development, it’s rarely, rarely, rarely… We’re going to see arrays copied, even though we don’t usually use arrays, List doesn’t smell good, why do we use arrays

Now the question is, how do I copy an array?

1. Basic writing

The simplest and most straightforward way to write this is to create an array, copy it in one by one, and you’re done

String[] data = new String[]{"1"."2"."3"};
String[] ans = new String[data.length];
for (int index = 0; index < data.length; index ++) {
    ans[index] = data[index];
}
Copy the code

2. Borrow containers for transfer

Arrays are a little bit cumbersome to use, so it’s nice to use a container, and use a List to copy arrays, just a few lines of code

String[] data = new String[]{"1"."2"."3"};
List<String> list = Arrays.asList(data);
String[] out = new String[data.length];
list.toArray(out);
Copy the code

3. Array.copy

This one is going a little too far, so just use array.copy

String[] data = new String[]{"1"."2"."3"};
String[] out = Arrays.copyOf(data, data.length);
Copy the code

4. System.arraycopy

In addition to the above, more basic usages can be used

String[] data = new String[]{"1"."2"."3"};
String[] out = new String[data.length];
System.arraycopy(data, 0, out, 0, data.length);
Copy the code

If you have read the JDK source code, the above usage should be familiar, especially in the container class, this array copy method is visible

Parameter Description:

public static native void arraycopy(Object src,  int  srcPos,
        Object dest, int destPos,
        int length);
Copy the code
  • SRC: original array
  • SrcPos: The starting index of the original array used for copying
  • Dest: the copied array
  • DestPos: indicates the byte of the destination array
  • Length: the length of the array copied from the original array

As you can see from the above description, this method can copy not only the array, but also the specified segment of the array

Series of blog posts:

  • Practical tip 1: String placeholder replacement -JDK version
  • Practice Tip 2: Array and list are rotated
  • Practice Tip 3: String and container transfer
  • Practical tip 4: Elegant string concatenation implementation
  • Practice tip 5: Hump and underline turn each other
  • Practice Tip 6: Special use of enumeration
  • Practice Tip 7: Sort carefully
  • Practice tip 8: Specify the initial size of the container
  • List. SubList is used incorrectly StackOverflowError
  • Practice Tip # 10: Immutable containers

II. The other

1. A gray Blog:liuyueyi.github.io/hexblog

A gray personal blog, recording all the study and work in the blog, welcome everyone to go to stroll

2. Statement

As far as the letter is not as good, the above content is purely one’s opinion, due to the limited personal ability, it is inevitable that there are omissions and mistakes, if you find bugs or have better suggestions, welcome criticism and correction, don’t hesitate to appreciate

  • Micro Blog address: Small Gray Blog
  • QQ: a gray /3302797840
  • Wechat official account: One Grey Blog