0

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:

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.

6 Answers 6

4

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.
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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

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

Comments

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

Comments

0

you should return newArr instead of 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;  
    }      
}

Comments

0
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;@

To avoid this use for loop to print your array output

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.