我想写一个泛型方法,它接受一个整数数组并计算其中有多少是回文。
这是我的程序:
public class pal {
public static void main(String[] args) {
Integer[] a = {12131,121,324,123,33,43,5654};
System.out.println("Total palindromes : "+checkPalindrome(a));
}
public static <T> int checkPalindrome(T[] b){
int n=0,temp=0,sum=0,r=0;
for(T a:b){
temp = (int)a;
while((int)a>0){
r = (int)a%10;
sum = (sum*10)+r;
}
if(temp == sum){
n++;
}
}
return n;
}
}
代码不会抛出任何异常,也不会做任何事情。
public class pali{公共静态无效主(String[]args){
Integer[] a = {12131,121,324,123,33,43,5654,45654};
System.out.println("Total palindromes : "+checkPalindrome(a));
}
public static <T> int checkPalindrome(T[] b){
int n=0;
for(T a:b){
int sum=0,r=0;
int temp = (int)a;
int k = (int)a;
while(k!=0){
r = k%10;
sum = (sum*10)+r;
k/=10;
}
if(temp == sum)
n++;
}
return n;
}
}