Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีเพิ่มข้อมูลตาม Index ใน CopyOnWriteArrayList บน Android

บทนำ

CopyOnWriteArrayList เป็นเวอร์ชัน Thread-safe ของ ArrayList ทุกครั้งที่มีการเปลี่ยนแปลง (add, set, remove) จะสร้าง Array ใหม่ (Fresh Copy) ทำให้ Iterator ที่กำลังทำงานไม่ได้รับผลกระทบ เหมาะสำหรับกรณีที่อ่านมากกว่าเขียน (Read-heavy)

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

เปิด Android Studio เลือก File → New Project กรอกข้อมูลที่จำเป็น

ขั้นตอนที่ 2: ออกแบบ Layout (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:gravity="center"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <TextView
        android:id="@+id/actionEvent"
        android:textSize="40sp"
        android:layout_marginTop="30dp"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

</LinearLayout>

ขั้นตอนที่ 3: เขียนโค้ด Java (src/MainActivity.java)

package com.example.myapplication;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import java.util.concurrent.CopyOnWriteArrayList;

public class MainActivity extends AppCompatActivity {

    CopyOnWriteArrayList<String> copyOnWriteArrayList;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        copyOnWriteArrayList = new CopyOnWriteArrayList<>();
        final TextView actionEvent = findViewById(R.id.actionEvent);

        // เพิ่มข้อมูลเริ่มต้น
        copyOnWriteArrayList.add("sai");
        copyOnWriteArrayList.add("ram");
        copyOnWriteArrayList.add("krishna");
        copyOnWriteArrayList.add("prasad");
        copyOnWriteArrayList.add("ram"); // ค่าซ้ำได้

        actionEvent.setText("" + copyOnWriteArrayList);

        // คลิกเพื่อเพิ่ม "first element" ที่ Index 0
        actionEvent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                copyOnWriteArrayList.add(0, "first element");
                actionEvent.setText("" + copyOnWriteArrayList);
            }
        });
    }
}

การทดสอบแอปพลิเคชัน

รันแอป จะเห็นข้อมูลเริ่มต้น 5 รายการ

วิธีเพิ่มข้อมูลตาม Index ใน CopyOnWriteArrayList บน Android

คลิก TextView จะเพิ่ม "first element" ที่ Index 0 (หน้าสุด) ข้อมูลเดิมเลื่อนไปข้างหลัง

วิธีเพิ่มข้อมูลตาม Index ใน CopyOnWriteArrayList บน Android

คำอธิบายเมธอด add(int index, E element)

  • แทรกองค์ประกอบ element ที่ตำแหน่ง index ที่ระบุ
  • องค์ประกอบเดิมที่ Index นั้นและต่อจากนั้นจะเลื่อนไปข้างหลัง (Index เพิ่มขึ้น)
  • Thread-safe: การอ่านขณะกำลังเพิ่มไม่會เกิด ConcurrentModificationException
  • โยน IndexOutOfBoundsException หาก Index ไม่ถูกต้อง

ข้อควรระวัง

  • ประสิทธิภาพ: ทุกครั้งที่ add/set/remove จะ Copy Array ใหม่ทั้งหมด - ใช้หน่วยความจำและ CPU สูง หากมีการเขียนบ่อย
  • เหมาะสำหรับ: รายการที่อ่านบ่อย แต่งานเขียนน้อย (เช่น Event Listeners, Observer Lists)
  • ไม่เหมาะสำหรับ: รายการขนาดใหญ่ที่มีการเพิ่ม/ลบบ่อยครั้ง

สรุป

ใช้ CopyOnWriteArrayList.add(index, element) เพื่อแทรกข้อมูลที่ตำแหน่งเฉพาะแบบ Thread-safe เหมาะสำหรับสถานการณ์ Read-heavy