可能重复:
Java是"传递引用"吗?
我想知道如何通过java中的referance将对象作为参数传递给方法.我试过这段代码
public class Class1
{
public Class1()
{
String x = null;
F(x);
//x = null
System.out.println("x = " + x);
}
void F(String x)
{
x = "new String";
}
public static void main(String[] args)
{
new Class1();
}
}
Run Code Online (Sandbox Code Playgroud)
您可以看到我将一个String函数F 传递给它,并且我在其中更改了String的值,但我无法看到我在函数F之外对其进行的更改.当我执行此代码时,我得到//{x = null}的不是我预期的//{x = new String}.
我试图理解传递的论点,我遇到了一个问题.
假设我有以下代码:
我将值60传递给方法someMethod.从那里我希望用用户输入修改该值.一旦它被修改,我希望它将该值传递给另一个名为getValue的方法.然后getValue方法返回该值.
这是问题所在:
1)如果我打电话给someMethod,它也会再次调用我不想要的用户输入.
2)将getValue方法的值打印出来的正确方法是:
New.getValue(int returnedValue); 调用错误"意外类型,必需值,找到类"
public class New {
Scanner sc = new Scanner(System.in)
private int static num1 = 60;
someMethod(num1);
public static int someMethod(int myValue)
{
//modify the integer
System.out.println("Enter in the value to modify");
myValue = sc.nextInt();
//output the value to a getter method
getValue(myValue);
return myValue;
}
public static int getValue(int returnedValue)
{
return returnedValue;
}
}
Run Code Online (Sandbox Code Playgroud)
一如既往,如果您需要澄清,请问!谢谢.
好吧,我需要字符串"H"更改为"KeyEvent.VK_ +(输入的字符串H)"由于某种原因,我无法将变量从H更改为主类中的新字符串.但它在其他课程中确实发生了变化.任何帮助都会很棒.
主类
Convert ConvertObject = new Convert();
String word = "H"
ConvertObject.Convert(word);
System.out.println(word); // this keeps printing out H but it needs to print out
"KeyEvent.VK_H"
Run Code Online (Sandbox Code Playgroud)
转换类
public static String Convert(String x) {
x = "KeyEvent.VK_" + x;
System.out.println(x);
return x;
Run Code Online (Sandbox Code Playgroud) 我得到:'
需要意外类型:变量
找到:值'在标记的行(*)
for (int i = 0; i < boardSize; i++) {
for (int j = 0; j < boardSize; j++) {
rows[i].getSquare(j) = matrix[i][j]; // * points to the ( in (j)
columns[j].getSquare(i) = matrix[i][j]; // * points to the ( in
int[] b = getBox(i, j);
int[] boxCord = getBoxCoordinates(i + 1, j + 1);
boxes[b[0]][b[1]].getSquare(boxCord[0], boxCord[1]);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的Row类:
private Square[] row;
Row(int rowCount) {
this.row = new Square[rowCount];
}
public Square getSquare(int index) {
return …Run Code Online (Sandbox Code Playgroud)