java 如何将整数类型值或对象类型值存储到 char 数组中

how to store an integer type value or an object type value into a char array in java

我试图将整数类型值和对象类型值存储到 char 数组中。我尝试使用 cast,但没有用。编译器抛出一个castexception。我想知道是否有像 Integer.parseInt() 这样的方法来转换它。 感谢您的回答。

您根本无法将其他 class 的对象存储到 char 数组中。 这样行不通。您只能在数组中存储相同 classsubclass 的对象。或者,在同一个数组中存储不同对象(包括数字和字符)的方法是 使用 Object 数组,因为 Object 是 class 的超级 class Java. 中的所有其他 classes 然后将您的对象转换为 Object 类型,或者 autobox 并将 primitives 转换为 Object 类型,然后将它们添加到数组中。

我不确定您的问题到底是什么,但我的理解是您希望将 integer 值转换为字符串,然后切碎每个字符并将它们存储在 char array 中。如果这是您的要求,那么这就是您解决问题的方式。关于object类型值,可以参考Samrat Dutta说的

public static void main(String[] args) {
    int input = 145689; // Taking an integer
    String string = Integer.toString(input); //Converting to string
    for(char c: string.toCharArray()) //Putting them inside a character array
    System.out.println(c);  

}

希望对您有所帮助!