简体   繁体   English

Java - 如何在没有别名的情况下返回方法多维数组

[英]Java - how to return in a method multidimensional array without aliasing

I'm aware that arrays are objects and in java objects are transfered by reference which could cause aliasing so objects should be returned with in this form to not cause aliasing:我知道数组是对象,在 java 中对象是通过引用传输的,这可能会导致别名,因此应该以这种形式返回对象,以免导致别名:

return new (object(parameters));返回新的(对象(参数));

So this is what I'm trying to do with multidimensional arrays, however for some reason compiler says I have an error : "array dimension missing".所以这就是我试图对多维数组做的事情,但是由于某种原因,编译器说我有一个错误:“缺少数组维度”。

public int[][] Testing(int[][]arr)
    {
        int[][]newArr=new int[arr.length][arr[0].length];
        for(int i=0;i<arr.length;i++)
        {
            for(int j=0;j<arr[0].length;j++)
            {
                newArr[i][j]=arr[i][arr[0].length-1-j];
            }
            return new int[][]newArr;  
        }      
    }

Could anyone tell me how to return in a method an multidimensional array without aliasing?谁能告诉我如何在方法中返回一个没有别名的多维数组?

Thank you.谢谢。

return newArr; is the one you should use.是你应该使用的。 Change your code as follows更改您的代码如下

  public int[][] Testing(int[][]arr){
    int[][]newArr=new int[arr.length][arr[0].length];
    for(int i=0;i<arr.length;i++)
    {
        for(int j=0;j<arr[0].length;j++)
        {
            newArr[i][j]=arr[i][arr[0].length-1-j];
        }

    }
    return newArr; // rerunning the array witch created inside this method.
}

Since you are creating your array inside your method, there is no risk for aliasing in this scenario.由于您是在方法中创建数组,因此在这种情况下不存在别名风险。 Noone else can get a reference to your array.没有其他人可以获得对您的数组的引用。

A simple一个简单的

return newArr;

will work just fine.会工作得很好。

Do you want to return a copy of newArr ?你想返回newArr的副本吗? see the following: copy a 2d array in java请参阅以下内容: 在java中复制一个二维数组

If you don't need a copy - just return newArr .如果您不需要副本 - 只需返回newArr

You can't return newArr as:您不能将newArr返回为:

return newArr;

Because it tells the compiler that it is an int but was declared as a 2D array.因为它告诉编译器它是一个int但被声明为一个二维数组。

import java.util.Scanner;
public class First {
    int a[][]=new int[3][3];
    Scanner s=new Scanner(System.in);
    int[][] Arr()
    {
        for(int i = 0;i < a.length;i++)
        {
            for(int j = 0;j < a.length;j++)
            {
                a[i][j] = s.nextInt();
            }
        }
        return(a);
    }
    public static void main(String[] args) {
        First f = new First();
        int c[][]=new int[3][3];
        c=f.Arr();
        for(int i=0;i<c.length;i++)
        {
            for(int j=0;j<c.length;j++)
            {
                System.out.println(c[i][j]);
            }
        }
    }
}

you should return newArr instead of int[][]newArr.你应该返回 newArr 而不是 int[][]newArr。

public int[][] Testing(int[][]arr)
{
    int[][]newArr=new int[arr.length][arr[0].length];
    for(int i=0;i<arr.length;i++)
    {
        for(int j=0;j<arr[0].length;j++)
        {
            newArr[i][j]=arr[i][arr[0].length-1-j];
        }
        return new newArr;  
    }      
}
public int[][] Testing(int[][] arr){
    int[][] newArr = new int[arr.length][arr[0].length];
    for(int i = 0; i < arr.length; i++){
        for(int j = 0; j < arr[0].length; j++){       
            newArr[i][j] = arr[i][arr[0].length-1-j];
        }            
    }    
    return newArr; 
}

Note: If you directly print the newArr in your main method you will get an error [[Ljava.lang.String;@注意:如果你直接在你的 main 方法中打印 newArr 你会得到一个错误 [[Ljava.lang.String;@

To avoid this use for loop to print your array output为避免使用 for 循环打印数组输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM