如何返回不同于java中原始数组类型的新数组
大家好,我正经历着我职业生涯中最艰难的时刻。 在这里,我对代码进行了注释,以便很容易发现问题。 我希望有人能救我,感谢那个救援者。 下面的代码如何返回不同于java中原始数组类型的新数组,java,arrays,types,Java,Arrays,Types,大家好,我正经历着我职业生涯中最艰难的时刻。 在这里,我对代码进行了注释,以便很容易发现问题。 我希望有人能救我,感谢那个救援者。 下面的代码 public class Marathon { /** The whole point here is: how can I return a new arr which is of type Marathon */ static int [][] raceValues; public Marathon(int aValue){
public class Marathon {
/** The whole point here is: how can I return a new arr which is of type Marathon */
static int [][] raceValues;
public Marathon(int aValue){
this.raceValues = [aValue][aValue]; // (Problem no.1) Erroneously accessing a static variable nonstatically
for (int i = 1; i <= aValue; i++) {
for (int j = 1; j <= aValue; j++) {
this.raceValues[i-1][j-1] = (aValue * (i - (aValue - (aValue - 1)))) + j;
}
}
}
Marathon alterAnyValue(int a){
/*
* If I could write something like next line, then my problem would be solved. But eclipse will grizzle
* "Cannot convert from int to Marathon"
* Marathon arr[][] = raceValues;
*/
int arr[][] = raceValues;
for (int i = 0; i < aValue; i++) {
for (int j = 0; j < aValue; j++) {
int c = arr[i][j] % 3; // Divisible by three
int d = arr[i][j] % 5; // Divisible by five
if(c == 0){
arr[i][j] = 3;
}
if(d == 0){
arr[i][j] == 5;
}
}
}
/*
* Here I don't know how to return new arr
* I must return a new arr and it must be of type Maraton
* At this point the whole world just ended. I don't know what to do
*/
//return new Marathon()
//return arr
}
}
公共课马拉松{
/**这里的全部要点是:我如何返回一个马拉松类型的新arr*/
静态int[][]值;
公共马拉松(国际马拉松){
this.raceValues=[aValue][aValue];//(问题1)错误地非静态访问静态变量
对于(int i=1;i对于您的第一个问题:静态访问:您可以执行Marathon.raceValues=new int[aValue][aValue];
这将解决您的静态访问“错误”
对于你的第二个问题,我认为Java中没有这种可能性,因为你不能创建新的“整数”类,因为它们是最终的。你可以创建自己的整数,但我无法想象为什么。因此,另一种可能性可能是你返回一个整数数组
这就是我会尝试做的。如果你想要一些不同的东西,请更好地指定问题:)为什么返回类型应该是Marathon
?回头看看世界是否刚刚结束!请告诉我你看到了什么。是的,返回类型必须是Marathon。