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

วิธีดึงข้อมูลตาม Index ใน CopyOnWriteArrayList บน Android

บทนำ

CopyOnWriteArrayList เป็น List แบบ Thread-safe ที่ Copy Array ใหม่ทุกครั้งที่มีการแก้ไข ตัวอย่างนี้สาธิตการใช้ get(int index) เพื่อดึงข้อมูลตามตำแหน่ง

ขั้นตอนที่ 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);

        // คลิกเพื่อดูข้อมูล Index 1 (ตัวที่ 2)
        actionEvent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // get(index) ดึงข้อมูลตามตำแหน่ง (0-based)
                String element = copyOnWriteArrayList.get(1);
                actionEvent.setText("Index 1: " + element);
            }
        });
    }
}

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

รันแอปบนอุปกรณ์จริง

วิธีดึงข้อมูลตาม Index ใน CopyOnWriteArrayList บน Android

คลิก TextView จะแสดงข้อมูล Index 1 คือ "ram"

วิธีดึงข้อมูลตาม Index ใน CopyOnWriteArrayList บน Android

คำอธิบายเมธอด get(int index)

  • คืนค่าองค์ประกอบที่ตำแหน่ง index (0-based: 0 = ตัวแรก)
  • Thread-safe: การอ่านขณะ Thread อื่นกำลัง Add/Remove ไม่มีปัญหา
  • โยน IndexOutOfBoundsException หาก index < 0 หรือ >= size()
  • ประสิทธิภาพ O(1) เหมือน ArrayList ปกติ

ข้อควรระวัง

  • Iterator ของ CopyOnWriteArrayList เป็น Snapshot - ไม่เห็นการเปลี่ยนแปลงหลังสร้าง Iterator
  • เหมาะสำหรับ: อ่านบ่อย เขียนน้อย, ต้องการ Iterator ที่ไม่โดน ConcurrentModificationException
  • ไม่เหมาะสำหรับ: รายการใหญ่ที่ Add/Remove บ่อย (Copy Array ทุกครั้ง)

สรุป

ใช้ get(index) ดึงข้อมูลตามตำแหน่งใน CopyOnWriteArrayList แบบ Thread-safe และ O(1) เหมือน ArrayList ธรรมดา