Java 将整型数组转换为整型数组

Java 将整型数组转换为整型数组,java,arrays,int,Java,Arrays,Int,我的应用程序中有一个类,其中存储int值: Characters.class: public int charPunch(int q) { int[] charPunch = { 15, 10, 20, 25, 20, 20, 15, 20, 20, 25 }; return charPunch

我的应用程序中有一个类,其中存储int值:

Characters.class:

public int charPunch(int q) {

    int[] charPunch = {
        15, 
        10, 
        20, 
        25, 
        20, 
        20, 
        15, 
        20, 
        20, 
        25
    };
    return charPunch(q);
}
q
值由用户字符选择决定。我正在努力理解代码,所以只发布了当前的代码

在同一个类文件中,我有一个字符串数组,然后可以用.toString()转换(在另一个.class文件中)

Game.class:

oneName = Ch.charName(q).toString();
这将为playerne的
oneName
提供数组值,并将字符串数组结果转换为单个字符串并工作

我的问题是:我是否能够对int值数组执行完全相同的操作

  • 将int数组更改为String数组,将String数组转换为单个字符串,然后将字符串转换为int,这会是糟糕的编程吗?但这是我最好的解决方案

    String onePunch = charPunch(q).toString();
    int charPunchInt = Integer.parseInt(charPunch);
    
当前,我在Characters.class数组的返回行上获得
stackOverflowerError
,直到进程放弃

我目前在Characters.class上获得StackOverflower错误

这是因为您在不停地反复调用同一个方法。基本上,这就是您的代码的外观(除了它的其余代码):

因此,它将使用相同的参数调用自己,并且只会填充堆栈内存,直到您得到指示的错误

一个可能的解决方案可能是在方法中添加一些逻辑来停止。或者,您可能希望访问数组的一个元素:

public int charPunch(int q) {
    int[] charPunch = {
        15, 
        10, 
        20, 
        25, 
        20, 
        20, 
        15, 
        20, 
        20, 
        25
    };
    return charPunch[q]; //<- using brackets [] instead of parenthesis ()
}
由于您从
charPunch
返回
int
,因此它不会编译。
int
是一种基本类型,根本没有任何方法。因此,您可以将方法改为返回一个
整数
,这样您就可以访问
toString
方法,但通过这样做,上面的代码将把一个整数转换成一个字符串,从而将字符串转换成一个整数(再次),这似乎毫无意义


我能对int值数组执行完全相同的操作吗


定义您真正想要做的事情,然后您将获得预期的帮助。

需要解决的几个问题,以完全理解您的问题

函数charPunch(intq)中的整数值是否总是相同的

您是在尝试将整个int数组转换为字符串还是仅转换通过函数传递的值?赋值后,您将如何处理该值

无论哪种方式,您都可能希望查看数组列表和增强的for循环语法(针对每个循环)

范例

// if values are immutable (meaning they cannot be changed at run time)
static final int charPunches [] = {15,10,20,25};

// function to return string value
protected String getCharPunchTextValue(int q){
    String x = null;
    for(int i: charPunches){ // iterate through the array
        if(q == i){// test equality
            x = Integer.toString(i);// assign string value of integer
        }
    }
    return x; // Return value, what ever needs this value may check for null to test if value exists
}    

如果将int数组转换为单个整数。这个整数是什么?我不理解这个问题,但是你得到了StackOverflowerError,因为在你的方法中,你递归地调用charPunch,而没有一个退出条件,这非常简单。非常感谢。我曾尝试使用[q],但它产生了其他错误。现在它可以工作了。@Patty P:1)方法中的整数值始终保持不变。2) 我真的不想将任何整数值转换成字符串,我希望有另一种解决方案。3) 我使用初始值设置变量,从中减去,直到得到结果,然后重用这些值重置变量并重新开始。我把它们放在另一个教室里,只是为了减少混乱。我喜欢你的答案。谢谢,我想你会想要一些常量类,其中包含你的值的静态最终副本,一旦你完成了你的值重置。
String onePunch = charPunch(q).toString();
int charPunchInt = Integer.parseInt(charPunch);
// if values are immutable (meaning they cannot be changed at run time)
static final int charPunches [] = {15,10,20,25};

// function to return string value
protected String getCharPunchTextValue(int q){
    String x = null;
    for(int i: charPunches){ // iterate through the array
        if(q == i){// test equality
            x = Integer.toString(i);// assign string value of integer
        }
    }
    return x; // Return value, what ever needs this value may check for null to test if value exists
}