CodeGym /จาวาบล็อก /สุ่ม /วิธีการ Java System.arraycopy()
John Squirrels
ระดับ
San Francisco

วิธีการ Java System.arraycopy()

เผยแพร่ในกลุ่ม

วิธีการ Java System.arraycopy() คืออะไร

เมธอด java.lang.System.arraycopy() คัดลอกอาร์เรย์ต้นทางที่ตำแหน่งเริ่มต้นที่ระบุไปยังปลายทางที่ดัชนีที่กำหนด
เมธอดนี้เป็นของคลาสjava.lang.System มันคัดลอกเนื้อหาของ subarray ที่มีความยาว ตามที่กำหนด จาก อาร์เรย์ ต้นทาง ที่กำหนด ไปยังอาร์เรย์อื่นที่เรียกว่าอาร์เรย์ปลายทาง อาร์เรย์ต้นทางเรียกว่า src และอาร์เรย์ปลายทางเรียกว่าdest จำนวนองค์ประกอบถูกกำหนดโดยอาร์กิวเมนต์ len

ประกาศวิธีการ Java System.arraycopy()

การประกาศเมธอดสำหรับ เมธอด java.lang.System.arraycopy()ในคลาสjava.langเป็นดังนี้:

public static void arraycopy(Object src, int srcIndex, Object dest, int destIndex, int len)

พารามิเตอร์

ต่อไปนี้เป็นพารามิเตอร์ของ วิธี arraycopy :
  1. src : เป็นอาร์เรย์ต้นทาง

  2. srcIndex : เป็นดัชนีเริ่มต้นของอาร์เรย์ต้นทาง

  3. dest : เป็นอาร์เรย์ปลายทาง

  4. destIndex : เป็นดัชนีเริ่มต้นของอาร์เรย์ปลายทาง

  5. len : เป็นจำนวนองค์ประกอบที่ต้องคัดลอกจากอาร์เรย์ต้นทางไปยังอาร์เรย์ปลายทาง

ฟังก์ชั่น

วิธีarraycopyคัดลอกข้อมูลจากsrcเริ่มจาก องค์ประกอบ srcIndexจนถึงsrcIndex +( len - 1) ไปยังอาร์เรย์ปลายทางที่destIndexจนถึงdestIndex + ( len - 1)

ค่าส่งคืน

วิธี การ arraycopyมี ประเภทการส่งคืน เป็นโมฆะซึ่งหมายความว่าจะไม่ส่งคืนอะไรเลย

ตัวอย่าง


public class Example {

    public static void main(String[] args) {

        String[] src = { "Monday","Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
        String[] dest = { "January", "February", "March", "April", "May", "June", "July", "August"};
  
        int srcIndex = 2;
        int destIndex = 3;
        int len = 2;
        
        //print number of elements that need to be copied 
        //from the source to the destination array  
        System.out.println("len : " + len);
        
        //print source index
        System.out.println("srcIndex : " + srcIndex);
  
        //print elements of the source array
        System.out.print("src : ");
        for (int i = 0; i < src.length; i++)
            System.out.print(src[i] + " ");
        System.out.println("");
  
        //print destination index
        System.out.println("destIndex : " + destIndex); 
        
        //print elements of the destination array
        System.out.print("dest : ");
        for (int i = 0; i < dest.length; i++)
            System.out.print(dest[i] + " ");
        System.out.println("");
                
        // Use of arraycopy() method
        System.arraycopy(src, srcIndex, dest, destIndex, len); 
        // this method copies the 'len' no of elements 
        // from the src array to the dest array using the srcIndex
        // and destIndex as reference points in both the arrays
        
                        
        // Print elements of destination after  
        System.out.print("final destination array : ");
        for (int i = 0; i < dest.length; i++)
            System.out.print(dest[i] + " ");
    }
}

เอาต์พุต

len : 2 srcIndex : 2 src : จันทร์ อังคาร พุธ พฤหัสบดี ศุกร์ เสาร์ อาทิตย์ destIndex : 3 dest : มกราคม กุมภาพันธ์ มีนาคม เมษายน พฤษภาคม มิถุนายน กรกฎาคม สิงหาคม อาร์เรย์ปลายทางสุดท้าย : มกราคม กุมภาพันธ์ มีนาคม วันพุธ พฤหัสบดี มิถุนายน กรกฎาคม สิงหาคม

ตัวอย่างการเปลี่ยนแปลงพารามิเตอร์


// Example of changing parameters
         srcIndex = 4;
         destIndex = 5;
         len = 1;

เอาต์พุต

len : 1 srcIndex : 4 src : จันทร์ อังคาร พุธ พฤหัสบดี ศุกร์ เสาร์ อาทิตย์ destIndex : 5 dest : มกราคม กุมภาพันธ์ มีนาคม เมษายน พฤษภาคม มิถุนายน กรกฎาคม สิงหาคม อาร์เรย์ปลายทางสุดท้าย : มกราคม กุมภาพันธ์ มีนาคม เมษายน พฤษภาคม วันศุกร์ กรกฎาคม สิงหาคม

คำอธิบาย

ในโค้ดข้างต้น เราได้กำหนดสองอาร์เรย์src[]และdest[ ] หลังจากนั้นเราได้กำหนดsrcIndexและdestIndexเพื่อกำหนดตำแหน่งอ้างอิงสำหรับอาร์เรย์ทั้งสอง ในตัวอย่างแรก เมธอด System.arraycopy คัดลอกองค์ประกอบจากอาร์เรย์ต้นทางsrc[]เริ่มต้นที่ดัชนี 2 และสิ้นสุดที่ดัชนี 3 (เนื่องจากlen = 2) ไปยังอาร์เรย์ปลายทาง โดยเริ่มจากดัชนี 3 จากนั้นจึงส่งออก อาร์เรย์ปลายทางสุดท้ายหลังจากคัดลอกองค์ประกอบ ในตัวอย่างที่สอง เมธอด System.arraycopy คัดลอกองค์ประกอบ 1 รายการ (เนื่องจากlen = 1) จากอาร์เรย์ต้นทางsrc[]เริ่มต้นที่ดัชนี 4 ไปยังอาร์เรย์ปลายทาง โดยเริ่มจากดัชนี 5 และยังส่งออกอาร์เรย์ปลายทางสุดท้ายหลังจากคัดลอกองค์ประกอบ

บทสรุป

ในตอนท้ายของบทความนี้ เราหวังว่าคุณจะคุ้นเคยกับ เมธอด java.lang.System.arraycopyใน Java แล้ว ฝึกฝนอย่างต่อเนื่องเพื่อสั่งการแนวคิดที่ลึกซึ้งยิ่งขึ้น จนกว่าจะถึงตอนนั้น จงเติบโตและเปล่งประกายต่อไป!
ความคิดเห็น
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION