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

วิธีแปลง ConcurrentLinkedQueue เป็น Array ใน Android

บทนำ

ConcurrentLinkedQueue เป็น Queue แบบ Thread-safe, Unbounded ตัวอย่างนี้สาธิตการใช้ toArray() แปลง Queue เป็น Array

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

public class MainActivity extends AppCompatActivity {

    ConcurrentLinkedQueue<String> concurrentLinkedQueue;

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

        concurrentLinkedQueue = new ConcurrentLinkedQueue<>();
        final TextView actionEvent = findViewById(R.id.actionEvent);

        // เพิ่มข้อมูลตัวอย่าง
        concurrentLinkedQueue.add("sai");
        concurrentLinkedQueue.add("ram");
        concurrentLinkedQueue.add("krishna");
        concurrentLinkedQueue.add("prasad");
        concurrentLinkedQueue.add("ram");

        actionEvent.setText("" + concurrentLinkedQueue);

        // คลิกเพื่อแปลงเป็น Array
        actionEvent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // toArray() คืน Object[] - ต้อง Cast หากต้องการ Type เฉพาะ
                Object[] array = concurrentLinkedQueue.toArray();
                
                // แปลงเป็น String[] (แนะนำใช้ toArray(new String[0]))
                String[] stringArray = concurrentLinkedQueue.toArray(new String[0]);
                
                actionEvent.setText("Array length: " + stringArray.length + "\n" + 
                        java.util.Arrays.toString(stringArray));
            }
        });
    }
}

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

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

วิธีแปลง ConcurrentLinkedQueue เป็น Array ใน Android

คลิก TextView จะแสดง Array length และเนื้อหา

เมธอด toArray() ทั้งสองรูปแบบ

  • ต้อง Cast เอง, ไม่ Type-safe
  • Type-safe, ใช้ new String[0] เพื่อให้ JVM สร้าง Array ขนาดพอดี
  • เมธอด คืนค่า หมายเหตุ
    toArray() Object[]
    toArray(T[] a) T[]

    แนะนำ: ใช้ toArray(new String[0])

    String[] array = queue.toArray(new String[0]);
    • ประสิทธิภาพดีกว่า (JVM optimize)
    • Type-safe ไม่ต้อง Cast
    • หลีกเลี่ยง ArrayStoreException

    สรุป

    ใช้ toArray(new String[0]) แปลง ConcurrentLinkedQueue เป็น String[] แบบ Type-safe และมีประสิทธิภาพ เหมาะสำหรับส่งให้ API ที่ต้องการ Array