Computer >> 컴퓨터 >  >> 프로그래밍 >> Android

Android ConcurrentLinkedDeque에 첫 번째 요소를 추가하는 방법

ConcurrentLinkedDeque란 무엇인가?

예제를 살펴보기 전에 ConcurrentLinkedDeque에 대해 먼저 알아보겠습니다. ConcurrentLinkedDeque는 링크드 노드(linked nodes) 기반의 크기 제한이 없는(unbounded) 데크(deque) 자료구조입니다. 가장 큰 특징은 여러 스레드가 동시에 접근해도 안전하게 데크의 요소를 조작할 수 있다는 점입니다.

이번 예제에서는 Android에서 ConcurrentLinkedDeque의 맨 앞에 첫 번째 요소를 추가하는 방법을 단계별로 알아보겠습니다.

1단계 — 새 프로젝트 생성

Android Studio에서 새 프로젝트를 생성합니다. 메뉴에서 File ⇒ New Project로 이동한 후, 새 프로젝트 생성에 필요한 모든 세부 정보를 입력합니다.

2단계 — 레이아웃 파일 수정

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>

위 코드에서는 ConcurrentLinkedDeque의 요소들을 화면에 표시하기 위해 TextView를 사용했습니다.

3단계 — MainActivity 작성

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.ConcurrentLinkedDeque;
public class MainActivity extends AppCompatActivity {
    ConcurrentLinkedDeque concurrentLinkedDeque;
    String head;
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        concurrentLinkedDeque = new ConcurrentLinkedDeque<String>();
        final TextView actionEvent = findViewById(R.id.actionEvent);
        concurrentLinkedDeque.add("sai");
        concurrentLinkedDeque.add("ram");
        concurrentLinkedDeque.add("krishna");
        concurrentLinkedDeque.add("prasad");
        actionEvent.setText("" + concurrentLinkedDeque);
        actionEvent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                concurrentLinkedDeque.addFirst("first element");
                actionEvent.setText("" + concurrentLinkedDeque);
            }
        });
    }
}

핵심은 addFirst() 메서드입니다. TextView를 클릭하면 addFirst("first element")가 호출되어 새로운 요소가 데크의 맨 앞에 삽입되고, 변경된 데크의 내용이 화면에 갱신되어 표시됩니다.

앱 실행 및 결과 확인

이제 애플리케이션을 실행해 보겠습니다. 실제 Android 모바일 기기가 컴퓨터에 연결되어 있다고 가정합니다. Android Studio에서 앱을 실행하려면 프로젝트의 액티비티 파일 중 하나를 연 후, 툴바에서 Run 아이콘을 클릭합니다. 옵션 목록에서 자신의 모바일 기기를 선택하면, 모바일 기기에 아래와 같은 기본 화면이 표시됩니다.

Android ConcurrentLinkedDeque에 첫 번째 요소를 추가하는 방법

이제 화면의 TextView를 클릭하면 "first element"가 데크의 맨 앞에 추가된 결과를 아래와 같이 확인할 수 있습니다.

Android ConcurrentLinkedDeque에 첫 번째 요소를 추가하는 방법