ภาพรวม
ตัวอย่างนี้สาธิตวิธีตรวจสอบว่าข้อความในสอง EditText เป็น Anagram (คำที่ประกอบด้วยตัวอักษรเดียวกันแต่เรียงต่างกัน) หรือไม่ ใน Android แสดงผลด้วย Toast
ขั้นตอนที่ 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">
<EditText
android:id="@+id/edit_query"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter first word" />
<EditText
android:id="@+id/edit_query1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter second word" />
<Button
android:id="@+id/buttonPanel"
android:text="Check Anagram"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</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.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText editQuery, editQuery1;
@RequiresApi(api = Build.VERSION_CODES.P)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editQuery = findViewById(R.id.edit_query);
editQuery1 = findViewById(R.id.edit_query1);
findViewById(R.id.buttonPanel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String first = editQuery.getText().toString().trim();
String second = editQuery1.getText().toString().trim();
if (first.isEmpty() || second.isEmpty()) {
Toast.makeText(MainActivity.this, "Please enter both words", Toast.LENGTH_SHORT).show();
return;
}
if (areAnagrams(first, second)) {
Toast.makeText(MainActivity.this, "It is an anagram!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "It is NOT an anagram", Toast.LENGTH_LONG).show();
}
}
});
}
// เมธอดตรวจสอบ Anagram แบบปรับปรุง (ใช้ Array นับความถี่)
private boolean areAnagrams(String first, String second) {
if (first.length() != second.length()) {
return false;
}
// แปลงเป็น lowercase และลบช่องว่าง
first = first.toLowerCase().replaceAll("\\s+", "");
second = second.toLowerCase().replaceAll("\\s+", "");
int[] charCount = new int[26]; // สำหรับ a-z
for (char c : first.toCharArray()) {
if (c >= 'a' && c <= 'z') {
charCount[c - 'a']++;
}
}
for (char c : second.toCharArray()) {
if (c >= 'a' && c <= 'z') {
charCount[c - 'a']--;
}
}
for (int count : charCount) {
if (count != 0) {
return false;
}
}
return true;
}
}
การทดสอบ
รันแอป ป้อนคำในทั้งสองช่อง เช่น "listen" และ "silent" คลิกปุ่ม จะแสดง "It is an anagram!"
ตัวอย่าง Anagram
- listen ↔ silent
- triangle ↔ integral
- heart ↔ earth
การปรับปรุงจากโค้ดต้นฉบับ
- ใช้ Array นับความถี่ตัวอักษร (O(n)) แทน Nested Loop (O(n²))
- จัดการตัวพิมพ์เล็ก/ใหญ่และช่องว่าง
- ตรวจสอบความยาวก่อนเพื่อประสิทธิภาพ
- โค้ดอ่านง่ายและบำรุงรักษาง่าย