这2个代码有什么区别:
代码A:
Foo myFoo;
myFoo = createfoo();
Run Code Online (Sandbox Code Playgroud)
哪里
public Foo createFoo()
{
Foo foo = new Foo();
return foo;
}
Run Code Online (Sandbox Code Playgroud)
比.代码B:
Foo myFoo;
createFoo(myFoo);
public void createFoo(Foo foo)
{
Foo f = new Foo();
foo = f;
}
Run Code Online (Sandbox Code Playgroud)
这2个代码之间有什么区别吗?
java terminology parameter-passing pass-by-reference pass-by-value
我的变量范围有问题.
public static void main(String[] args){
int[] test={1,2,3};
test(test);
System.out.println(test[0]+" "+test[1]+" "+test[2]);
}
static void test(int[] test){
test[0]=5;
}
Run Code Online (Sandbox Code Playgroud)
我期望输出到1 2 3,但结果是5 2 3.为什么我在方法中更改了数组中的值,但原始数组发生了变化?
如果需要,我可以发布更多代码,但在此之前我想问一下关于下面传递数组的方法的一般问题,然后设置为另一个数组,但由于某种原因原始数组,一个被传入,也正在改变,这怎么可能/我该怎么办?谢谢
tempBoard是一个与currentState大小相同的数组,temp [k]包含在movePiece中进行的更改,当前状态在方法中声明,而不是全局变量
private int[][] MiniMaxBaseCase(int[][] currentState, int currentColor)
{
tempBoard = movePiece(currentState,temp[k]);
}
private int[][] movePiece(int[][] currentState, int[] move)
{
if(move[0] == -1)
return currentState;
//if the piece is just moving
if(move[4] == -1)
{
currentState[move[2]][move[3]] = currentState[move[0]][move[1]];
currentState[move[0]][move[1]] = 0;
return currentState;
}
//if the piece is jumping another
if(move[4] != -1)
{
currentState[move[4]][move[5]] = currentState[move[0]][move[1]];
currentState[move[2]][move[3]] = 0;
currentState[move[0]][move[1]] = 0;
return currentState;
}
return currentState;
}
Run Code Online (Sandbox Code Playgroud) 我写这样的时候:
public class test {
void mainx()
{
int fyeah[] = {2, 3, 4};
smth(fyeah);
System.out.println("x"+fyeah[0]);
}
void smth(int[] fyeah)
{
fyeah[0] = 22;
}
}
Run Code Online (Sandbox Code Playgroud)
它打印x22;
我写这样的时候:
public class test {
void mainx()
{
int fyeah = 5;
smth(fyeah);
System.out.println("x"+fyeah);
}
void smth(int fyeah)
{
fyeah = 22;
}
}
Run Code Online (Sandbox Code Playgroud)
它不打印x22,但打印x5.
为什么在第二个版本函数中,值不会改变?它是否仅为数组元素更改值?
我正在尝试理解java中的数组设置.在创建数组后,为什么必须为数组中的每个对象初始化空间.它是如何存储在内存中的:
[object][object]
Run Code Online (Sandbox Code Playgroud)
或者像这样:
[*class]->[object]
[*class]->[object]
Run Code Online (Sandbox Code Playgroud)
换句话说,实际上在内存中做了什么.难道array[0] = new class()仅仅返回一个参考给预留位置在内存中,与class[] array = new class[10]声明一起创造的10个球线的东西,这是后来由新的报表分配到?
我对数组语法有些不理解。例如我可以这样做:
int[] tab = {1,2,3};
假设我有一个接受数组作为参数的方法,我可以这样做:
myMethod(tab);
但为什么我不能这样做:
myMethod({1,2,3})
为什么我必须添加一个额外的“new int[]”,如下所示:
Method(new int[] {1,2,3})
我有一个很奇怪的问题。在活动中,我声明了两个数组
private String original[] = new String[100];
private String changed[] = new String[100];
Run Code Online (Sandbox Code Playgroud)
然后,我将值分配给这两个数组OnCreate:
Bundle extras = getIntent().getExtras();
if (extras != null) {
original = extras.getStringArray("sentArray");
changed = original;
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我更改changed数组成员的值,则original数组也将更改该成员。
例如,我做完之后
changed[0] = "New value";
Run Code Online (Sandbox Code Playgroud)
的值original[0]也是“新值”。
这样怎么可能?这是错误吗?
我很困惑为什么我不能使用该语句来显示数组中的值?
System.out.println(intarray);
这是输出“[I@7852e922”
如果 java 是按值传递的,那么该语句不应该返回数组的值而不是对象引用。这是我使用的修复方法。
System.out.println(Arrays.toString(intarray));
Run Code Online (Sandbox Code Playgroud)