class TJ4_5{
public static void main(String args[]){
String s="Madam I'm Adam",s2,s3,s4;
char a[]=s.toCharArray(),temp;
char s1[]=new char[s.length()];
char c[]=new char[s.length()];
char d[]=new char[s.length()];
for(int i=0;i<s.length();i++){
if(Character.isLetterOrDigit(a[i])){
s1[i]=a[i];
}
}
s2=new String(s1);
if(s2.length()%2==0){
s2.getChars(0,s2.length()/2,c,0);
s2.getChars(s2.length()/2,s2.length(),d,0);
System.out.println(c);     //交换前的c字符串
System.out.println(d);     //交换前的c字符串
}
else{
s2.getChars(0,s2.length()/2,c,0);
s2.getChars(s2.length()/2+1,s2.length(),d,0);
System.out.println(c);       //交换前的c字符串
System.out.println(d);       //交换前的d字符串
}
for(int i=0;i<d.length/2;i++){
temp=d[i];
d[i]=d[d.length-i-1];
d[d.length-i-1]=temp;
}
s3=new String(c);
s4=new String(d);
System.out.println(s4);
if(s3.equalsIgnoreCase(s4)){
System.out.println("是回文");
}
else{
System.out.println("不是回文");
}
}
}
这个程序在字符串转化为数组之后,数组长度一直是14(字符串的长度),为什么在把a数组赋给s1数组之后长度还是没有变化?请问应该如何更改程序,才能是结果正常?

解决方案 »

  1.   

    char c[]=new char[s.length()]; 
    char d[]=new char[s.length()]; 
    这是长度不变化的原因~你这个方法实在是..不想改..重新写个新的好了class TJ4_5 {
    public static void main(String args[]) {
    String s = "Madam I'm Adam";
    String s1 = new String();
    String s2 = new String();
    char a[] = s.toCharArray();
    for (int i = 0; i < a.length; i++)
    if (Character.isLetterOrDigit(a[i])) {
    s1 = s1 + a[i];
    s2 = a[i] + s2;
    }
    if (s1.equalsIgnoreCase(s2))
    System.out.println("是回文");
    else
    System.out.println("不是回文");
    }
    }
      

  2.   

    不变化的原因:
    char s1[]=new char[s.length()]; 
    晕,就这行没复制上...s1你已经声明了数组长度是14
    那么我只给前3个赋值..长度还是14,只不过后面的都是空的而已..
      

  3.   

    修改了一下`
    [code=Java]import java.awt.*;
    public class HuiWen { /**
     * @langzi
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
         String str = "aabbcc";
         String s1 = new String();
         String s2 = new String();
         char c[] = str.toCharArray();
         for(int i = 0 ; i < str.length();i++){
          if(Character.isLetterOrDigit(c[i])){
          s1 =  s1 + c[i];
          s2 =  c[i] + s2;
          }
          s1 = new String(s1);
          s2 = new String(s2);
         }
         if(s1.equalsIgnoreCase(s2)){
         System.out.println("is huiwen");
         }
         else
         System.out.println("is not huiwen");
         }
    }
    [/java]
      

  4.   

    修改了一下`
    [code=Java]import java.awt.*;
    public class HuiWen { /**
     * @langzi
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
         String str = "aabbcc";
         String s1 = new String();
         String s2 = new String();
         char c[] = str.toCharArray();
         for(int i = 0 ; i < str.length();i++){
          if(Character.isLetterOrDigit(c[i])){
          s1 =  s1 + c[i];
          s2 =  c[i] + s2;
          }
          s1 = new String(s1);
          s2 = new String(s2);
         }
         if(s1.equalsIgnoreCase(s2)){
         System.out.println("is huiwen");
         }
         else
         System.out.println("is not huiwen");
         }
    }
    [/java]