使用二维对象数组
Using a 2-dimensional object array
这是我的二维数组:
String[][] theMaze = new String[row][column];
这个方法怎么用?
public void ruteToX(String[][] theMaze){
// call theMaze and process in this method
}
传入数组时,在前面的方法中('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;
}
假设我正确理解了你的问题。
我将向您展示一个将数组传递给方法 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
的值,这意味着如果你在方法中对参数数组进行任何更改,实际数组也会面临相同的更改(因为它们是相同的引用)。
这是我的二维数组:
String[][] theMaze = new String[row][column];
这个方法怎么用?
public void ruteToX(String[][] theMaze){
// call theMaze and process in this method
}
传入数组时,在前面的方法中('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;
}
假设我正确理解了你的问题。
我将向您展示一个将数组传递给方法 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
的值,这意味着如果你在方法中对参数数组进行任何更改,实际数组也会面临相同的更改(因为它们是相同的引用)。