这是我的2维数组:采用2维对象数组
String[][] theMaze = new String[row][column];
如何在这个方法中使用它?
public void ruteToX(String[][] theMaze){
// call theMaze and process in this method
}
这是我的2维数组:采用2维对象数组
String[][] theMaze = new String[row][column];
如何在这个方法中使用它?
public void ruteToX(String[][] theMaze){
// call theMaze and process in this method
}
假设我理解你的问题是正确的。
我会告诉你一个将数组传递给方法ruteToX(...)
的示例。
public class Example
{
String[][] theMaze = new String[5][5];
public void ruteToX(String[][] theMaze)
{
//call theMaze and process in this method
}
public static void main(....)
{
Example ob=new Example();
ob.ruteToX(ob.theMaze);
//passed the value of reference or the pointer to the function ruteToX(...)
}
}
它是如何通过的?
当你传递一个数组时,它在内存中的值是pointer or reference
,这意味着如果你对方法中的参数数组做了任何改变,实际的数组也将面对相同的改变(因为它们是相同的 - 同名参考)。
当阵列中的传递,在以前的方法调用的方法和传递数组中仅使用变量名称(“之前ruteToX”无论正在运行)。
public void previousMethod(){
ruteToX(theMaze);
}
public void ruteToX(String[][] theMaze){
// call theMaze and process in this method
}
编辑: 另外,一旦在该方法既可以使用阵列原样或创建一个新的数组等于原始阵列。
public void ruteToX(String[][] theMaze){
String[][] secondMaze = theMaze;
}
我不同意'创建一个等于原始数组的新数组'。 –
@MathewsMathai同意这不是一个很好的例子,但我想提供多个选项。 – AnthonyGordon
这不是关于决定。 'String [] [] secondMaze = theMaze;'不创建新数组。由于您正在使用'=',因此您创建的新数组对象具有相同的引用。 –
什么应该在里面? –
你打算将数组传递给该方法吗? –
是的,先生。马修。这是数组充满主要价值。而不是如何调用已经填充方法ruteToX的数组? – Wisnu