1. 程式人生 > >System.arraycopy()方法詳解

System.arraycopy()方法詳解

System中提供了一個native靜態方法arraycopy(),可以使用這個方法來實現陣列之間的複製

import java.util.Arrays;

import static java.lang.System.arraycopy;

public class Solution4 {
    public static void main(String[] args) {
        int[] arr1 ={1,2,3,4,5};
        int[] arr2= {11,12,13,14,15};

        arraycopy(arr1, 3, arr2, 2, 2);
        System.out.println(Arrays.toString(arr2));
    }
}
//[11, 12, 4, 5, 15]

關於次方法的示例程式碼1:

public class Solution3 {
    private static String method(String s) {
// TODO Auto-generated method stub
        if (s == null || s.length() == 0) {
            return null;
        }
        int count = 0;
        for (int i = 0; i < s.length(); i++) {//計算空格數目
            if (s.charAt(i) == ' ') {
                count++;
            }
        }
        int oldLen = s.length();
//擴充套件陣列的長度
        int newLen = s.length() + 2 * count;
        char[] newc = new char[newLen];
        /**
         * arraycopy(s.toCharArray(), 0, newc, 0, s.length())
         *String.toCharArray 方法,作用:將字串轉換為字元陣列。
         *
         */
        System.arraycopy(s.toCharArray(), 0, newc, 0, s.length());//index=0,相當於陣列copy

//定義兩個遊標,i指向old組中的當前值,j指向newc陣列中初始化的值,然後從後往前初始化newc陣列,直到i指向0或者j追上i。
        int i = oldLen - 1, j = newLen - 1;
        while (i >= 0 && i < j) {
            if (newc[i] == ' ') {
                newc[j--] = '0';
                newc[j--] = '2';
                newc[j--] = '%';


            } else {
                newc[j--] = newc[i];
            }
            i--;
        }
        String newStr = new String(newc);//轉換成字串
        return newStr;
    }
    public static void main(String[] args) {
        String s = "1 2 3 4 5 6 7 8 9 0";
        s = method(s);
        System.out.println(s);

    }

}

2

/*System中提供了一個native方法arraycopy()*/
public class SsytemArrayCopy {
    public static void main(String[] args) {
        User [] users=new User[]{new User(1,"admin","[email protected]"),new User(2,"maco","[email protected],com"),new User(3,"kitty","[email protected],com")};//初始化物件陣列
        User [] target=new User[users.length];//新建一個目標物件陣列
        System.arraycopy(users, 0, target, 0, users.length);//實現複製
        System.out.println("源物件與目標物件的實體地址是否一樣:"+(users[0] == target[0]?"淺複製":"深複製"));
        target[0].setEmail("
[email protected]
"); System.out.println("修改目標物件的屬性值後源物件users:"); for (User user : users){//User user 相當於User user=new User();會呼叫建構函式User{}的方法 System.out.println(user); } } } class User{ private Integer id;//例項變數 private String username; private String email; //無參建構函式 public User() { } //有參的建構函式 public User(Integer id, String username, String email) { super();//呼叫父類的建構函式 this.id = id;//注意this()與this的區別, this.username = username; this.email = email; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() {//此處才為方法 return "User [id=" + id + ", username=" + username + ", email=" + email + "]"; } }