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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20519100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:30:24  来源:igfitidea点击:

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

javamultidimensional-arrayreturn

提问by JaVaPG

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.

谢谢你。

采纳答案by Keppil

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.

会工作得很好。

回答by Ruchira Gayan Ranaweera

return newArr;is the one you should use. Change your code as follows

return 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; // rerunning the array witch created inside this method.
}

回答by Lital Kolog

Do you want to return a copy of newArr? see the following: copy a 2d array in java

你想退回一份newArr吗?请参阅以下内容: 在java中复制一个二维数组

If you don't need a copy - just return newArr.

如果您不需要副本 - 只需 return newArr

回答by tmols007

You can't return newArras:

您不能返回newArr为:

return newArr;

Because it tells the compiler that it is an intbut was declared as a 2D array.

因为它告诉编译器它是一个int但被声明为一个二维数组。

回答by Ajay kumar

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]);
            }
        }
    }
}

回答by Suraj Adhikary

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;  
    }      
}