Java Palindrome Tutorials
Java Palindrome tutorial shows how to work with palindromes in Java.
Java Palindrome教程展示了如何在Java中使用回文
Palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward, such as madam or racecar.
回文报是一个单词,数字,短语或其它字符序列,它们的向前和向后读取相同,例如madam或racecar
In this tutorial we show several ways to check if a string is a palidrome in Java.
在本教程中,展示了几种检查字符串是否是Java回文的方法
Java Palindrome with StringBuilder
The StringBuilder's
reverse
method causes this character sequence to be replaced by the reverse of the sequence.
package com.zetcode.palindrome;
public class Palindrome1 {
public static void main(String[] args) {
System.out.println(isPalindrome("radar"));;
System.out.println(isPalindrome("kayak"));
System.out.println(isPalindrome("forest"));;
}
private static boolean isPalindrome(String original) {
String reversed = new StringBuilder(original).reverse().toString();
return original.equals(reversed);
}
}
In the example, we pass the original string to the StringBuilder
and reverse it. Then we compare the reversed string to the original with equals
.
在示例中,我们将原始字符串传递给StringBuilder并将其反转。然后,我们将颠倒的字符串与原始字符串进行比较
Java Palindrome with for loop and charAt
具有for循环和charAt的Java回文
The String's
charAt
method returns the char value at the specified index. An index ranges from 0 to length() - 1
.
字符串的charAt方法返回指定索引处的char值。索引范围是0到length()-1
package com.zetcode.palindrome;
public class Palindrome2 {
public static void main(String[] args) {
System.out.println(isPalindrome("radar"));
System.out.println(isPalindrome("kayak"));
System.out.println(isPalindrome("forest"));
}
private static boolean isPalindrome(String original) {
String reversed = "";
int len = original.length();
for (int i = len - 1; i >= 0; i--) {
reversed = reversed + original.charAt(i);
}
return original.equals(reversed);
}
}
In the example, we build the reversed string with a for loop that goes from the end of the original string. We get the current character with charAt
.
在示例中,我们使用从原始字符串末尾开始的for循环构建反向字符串。通过charAt获得当前字符
int len = original.length();
We get the number of characters in the original string with length
.
我们获得原始字符串中具有的字符数长度
for (int i = len - 1; i >= 0; i--) {
reversed = reversed + original.charAt(i);
}
We build the reversed string by taking the characters from the end of the original string with charAt
.
我们通过使用charAt从原始字符串的末尾获取字符来构建反向字符串
Java Palindrome with toCharArray
Java回文与toCharArray
The String's
toCharArray
converts the string to a new character array.
toCharArray将字符串转换为新的字符数组
package com.zetcode.palindrome;
public class Palindrome3 {
public static void main(String[] args) {
System.out.println(isPalindrome("radar"));
System.out.println(isPalindrome("kayak"));
System.out.println(isPalindrome("foreat"));
}
private static boolean isPalindrome(String original) {
char[] data = original.toCharArray();
int i = 0;
int j = data.length - 1;
while (j > i) {
if (data[i] != data[j]) {
return false;
}
++i;
--j;
}
return true;
}
}
In the example, we turn the original string to an array of characters. In a while loop, we start to compare the characters from the both sides of the string; starting with the leftmost and rightmost characters. We go until the middle of the string.
在示例中,我们将原始字符串转换为字符数组。在while循环中,我们开始比较字符串两侧的字符;从最左边和最右边的字符开始, 一直走到字符串的中间
Java Palindrome with Stack
The Stack
is a last-in-first-out (LIFO) collection.
package com.zetcode.palindrome;
import java.util.Stack;
public class Palindrome4 {
public static void main(String[] args) {
System.out.println(isPalindrome("radar"));
System.out.println(isPalindrome("kayak"));
System.out.println(isPalindrome("foreat"));
}
private static boolean isPalindrome(String original) {
char[] data = original.toCharArray();
Stack<Character> stack = new Stack<>();
for (char c : data) {
stack.push(c);
}
char[] data2 = new char[data.length];
int len = stack.size();
for (int i = 0; i < len; i++) {
data2[i] = stack.pop();
}
String reversed = new String(data2);
return original.equals(reversed);
}
}
The example uses the Java Stack
container to build a reversed string.
该示例使用Java Stack容器构建反向字符串
char[] data = original.toCharArray();
First, we turn the string to an array of characters with toCharArray
.
首先,使用toCharArray将字符串转换为字符数组
Stack<Character> stack = new Stack<>();
for (char c: data) {
stack.push(c);
}
In the second step, we push the characters to the stack.
第二步,将字符推入堆栈
char[] data2 = new char[data.length];
This array will hold the reversed characters.
该数组将包含反向字符
for (int i = 0; i < len; i++) {
data2[i] = stack.pop();
}
We get the reversed string now by popping the characters from the stack.
现在通过从堆栈中弹出字符来获得反向字符串
String reversed = new String(data2);
return original.equals(reversed);
We create the reversed string from the array and compare it with the original string using equals
.
从数组创建反向字符串,然后使用equals将其与原始字符串进行比较
In this tutorial, we have check if a string is a palindrome. We have created various algorithms to check a palindrome.
在本教程中,我们检查字符串是否是回文。并创建了各种算法来检查回文