小编Hri*_*dre的帖子

在 ArrayList#remove() 中转换 char->Character - 为什么?

import java.util.*;

public class rearrange_palindrome {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String st=sc.nextLine();

        ArrayList<Character> ar = new ArrayList<Character>();

        for (int i=0;i<st.length();i++){
            if (ar.contains(st.charAt(i)))
            ar.remove((Character)st.charAt(i));//why did they use type casting?
            else
            ar.add(st.charAt(i));
        }
        if (st.length()%2==0 && ar.isEmpty()==true || st.length()%2==1 && ar.size()==1)
        System.out.println("Palindrome");
        else
        System.out.println("Not a palindrome");
    }
}
Run Code Online (Sandbox Code Playgroud)

请告诉我他们为什么使用类型转换的原因,我已经评论了该行。

java typecasting-operator

2
推荐指数
1
解决办法
69
查看次数

标签 统计

java ×1

typecasting-operator ×1