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

จะลบรายการที่ซ้ำกันออกจากรายการลิงก์ที่ไม่ได้เรียงลำดับใน Android ได้อย่างไร


ตัวอย่างนี้สาธิตเกี่ยวกับวิธีการลบรายการที่ซ้ำกันออกจากรายการที่เชื่อมโยงที่ไม่เรียงลำดับใน Android

ขั้นตอนที่ 1 − สร้างโครงการใหม่ใน Android Studio ไปที่ไฟล์ ⇒ โครงการใหม่และกรอกรายละเอียดที่จำเป็นทั้งหมดเพื่อสร้างโครงการใหม่

ขั้นตอนที่ 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">
   <TextView
      android:id="@+id/text"
      android:textSize="30sp"
      android:layout_width="match_parent"
      android:layout_height="match_parent" />
</LinearLayout>

ในโค้ดด้านบนนี้ เราได้นำมุมมองข้อความมาแสดงโดยไม่มีค่าที่ซ้ำกัน

ขั้นตอนที่ 3 − เพิ่มรหัสต่อไปนี้ใน 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.widget.TextView;

import java.util.HashSet;
import java.util.LinkedList;

public class MainActivity extends AppCompatActivity {
   @RequiresApi(api = Build.VERSION_CODES.P)
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      LinkedList<Integer> linkedList = new LinkedList<>();
      linkedList.add(2);
      linkedList.add(8);
      linkedList.add(2);
      linkedList.add(5);
      linkedList.add(7);
      linkedList.add(5);
      linkedList.add(9);

      HashSet<Integer> hashSet = new HashSet<>(linkedList);
      TextView textView = findViewById(R.id.text);
      textView.setText(hashSet.toString());
   }
}

มาลองเรียกใช้แอปพลิเคชันของคุณกัน ฉันคิดว่าคุณได้เชื่อมต่ออุปกรณ์มือถือ Android จริงกับคอมพิวเตอร์ของคุณ ในการรันแอพจาก android studio ให้เปิดไฟล์กิจกรรมของโปรเจ็กต์แล้วคลิกไอคอน Run จากแถบเครื่องมือ เลือกอุปกรณ์มือถือของคุณเป็นตัวเลือก แล้วตรวจสอบอุปกรณ์มือถือของคุณซึ่งจะแสดงหน้าจอเริ่มต้นของคุณ -

จะลบรายการที่ซ้ำกันออกจากรายการลิงก์ที่ไม่ได้เรียงลำดับใน Android ได้อย่างไร