-3
package test;
import java.util.*;


public class test {
    public int[][] left (int matrix[][]) {

        for(int i=0; i<=3;i++)
            for (int j=2;j>=0;j--)
            {
                if (matrix[i][j] ==0) 
                {
                    for (int k=j+1; k<=3;k++)
                    {
                        matrix[i][k-1]=matrix[i][k];    // chuyen
                    }
                    for (int l=3; l>=0;l--)
                    {
                        if (matrix[i][l] == 0) continue;
                        else matrix[i][l] =0;
                        if (matrix[i][l] != 0) break;
                    }
                }
            }
        return matrix;
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int i,j;
        int[][] a = new int[3][3];
        for (i=0;i<=2;i++)
             for (j=0;j<=2;j++)
             {
                 System.out.print("input a" +i + j);
                 a[i][j]= in.nextInt();
             }
        left(a);
        for (i=0;i<=2;i++)
        {
             for (j=0;j<=2;j++)
             {
                 System.out.print(a[i][j]);
             }
             System.out.println("");
        }

    }

}

This method insert takes as input int[][] array, and I want a method to return a new array. In example, I got an array.

a[][]= 
{  
0 0 2 0
0 2 0 0 
0 2 2 2 
2 0 0 0 
}

And I try to use method like that : left(a); to return the new array a but it not work .
The new array i want to return like:

{ 
2 0 0 0  
2 0 0 0  
2 2 2 0 
2 0 0 0 
} 

(incremental element to the left, except 0)

Multiple markers at this line
- Return type for the method is missing
- Syntax error, insert "... VariableDeclaratorId" to complete
FormalParameterList

I don't know how to fix that. Can someone help?

Shirishkumar Bari
  • 2,692
  • 1
  • 28
  • 36
Phoenix
  • 113
  • 1
  • 1
  • 6
  • 4
    `but it not work` Please, be specific. What exactly doesn't work? Post compilation errors, exception message, stack trace, invalid or unexpected results. – default locale Apr 09 '15 at 11:25
  • New arrays are created using the **new** keyword. And stuff is returned from methods by using the **return** statement. So, where exactly is your problem? – GhostCat Apr 09 '15 at 11:35
  • Are you looking for a `deep copy` of your array? Possibly answered here: http://stackoverflow.com/questions/1564832/how-do-i-do-a-deep-copy-of-a-2d-array-in-java – vikingsteve Apr 09 '15 at 11:45
  • I'm sorry about that. I edited my post – Phoenix Apr 09 '15 at 11:48
  • I change method to static and its worked. Thank you – Phoenix Apr 09 '15 at 11:56

2 Answers2

1

Well I am really sorry but your code has many issues .

  1. left() method is non static and you are trying to access it via Static method . It should give you compilation error.
  2. the sample data you have shown that you need matrix of 4X4 but in main you are declaring array as int[][] a = new int[3][3]
  3. you are looping in left() method as for(int i=0; i<=3;i++) which should throw an arrayIndexOutBoundException.

And I can see your comment that it worked by just declaring left() method as static .Could you please post the working code then?

I would recommend you to resolve issues mentioned above first to be able to make your code atleast in working condition.

Shirishkumar Bari
  • 2,692
  • 1
  • 28
  • 36
0

TODO:
1. Mind all the issues said by @Shirish
2. Don't use Primitive array, as Collections work with Class type. ex : Integer[][] a=new Integer[3][3];
3. After input run the below code and display code.(do not call left(a) method)

incremental element to the left, except 0

What I applied here is - taken each row from the array and sorted in descending order.

    for (Integer[] item : a) {
        Arrays.sort(item,Collections.reverseOrder());
    }

Hope it will solve the issue.

Final Code:

public class MyArray {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int i, j;
        Integer[][] a = new Integer[3][3];
        for (i = 0; i <= 2; i++)
            for (j = 0; j <= 2; j++) {
                System.out.print("input a" + i + j);
                a[i][j] = in.nextInt();
            }
        // left(a);
        for (Integer[] item : a) {
            Arrays.sort(item, Collections.reverseOrder());
        }

        for (i = 0; i <= 2; i++) {
            for (j = 0; j <= 2; j++) {
                System.out.print(a[i][j]);
            }
            System.out.println("");
        }
    }
}
Bharatesh
  • 8,943
  • 3
  • 38
  • 67