繁体   English   中英

Java用特定方法填充二维数组

[英]java fill 2D array with specific method

我有一个二维数组(矩阵),一个整数nn是全平方

例如,如果n等于16,则是这样的填充矩阵。

1  2  3  4
12 13 14 5
11 16 15 6
10 9  8  7

我能做什么?

这是一个小例子,看起来像什么。 实际发生的情况写为评论。

public class TestArray {

    // A simple enum for each direction
    public enum Mode {
        right, down, left, up;
    }

    public static void main(String[] args) {
        final int size = 4; // We set a fixed size for the square
        int[][] arr = new int[size][size]; // create the array from the size
        // Running vallues 
        // i and j to refer to the array.
        // val holds the current value to be inserted
        // circle holds how often we are going up. Each time we go up it´s increased by one
        // In the end this should reduce the amount of steps we do to not override 
        // already assigned values.
        int i = 0,j = 0, val = 1, circle = 0; 
        Mode runningMode = Mode.right; // We start by going to the right.
        while(size*size >= val) { // loop until we reached the last value
            arr[j][i] = val++; // Assign the value and increase the value by one afterwards.
            // We go right.
            if(runningMode == Mode.right) { 
                // we reached the last assignable item.
                // subtract one to not get an index out of bound,
                // subract the variable trips that is used to get the last item for the inner circle
                if(i==arr.length-1-circle) { 
                    // We are going down now and increase j 
                    runningMode = Mode.down;
                    ++j;
                } else {
                    // go on going right.
                    ++i;
                }
            } else if(runningMode == Mode.down){
                // we reached the last assignable item.
                // subtract one to not get an index out of bound,
                // subract the variable trips that is used to get the last item for the inner circle
                if(j==arr.length-1-circle) {
                    // We are going left now and decrease i 
                    runningMode = Mode.left;
                    --i;
                } else {
                    // go on going down.
                    ++j;
                }
            } else if(runningMode == Mode.left){
                // we reached the last assignable item.
                // add the variable trips that is used to get the last item for the inner circle
                if(i==0+circle) {
                    // We are going up now and decrease j
                    // Simultaniosly we are about to end our next circle, so we increase circle
                    runningMode = Mode.up;
                    ++circle;
                    --j;
                } else {
                    // go on going left.
                    --i;
                }
            } else if(runningMode == Mode.up){
                // we reached the last assignable item.
                // add the variable trips that is used to get the last item for the inner circle
                if(j==0+circle) {
                    // We are going right now and increase i
                    runningMode = Mode.right;
                    ++i;
                } else {
                    // go on going up.
                    --j;
                }
            }
        }
        // Print the result
        for(int[] a : arr) {
            for(int b : a) {
                System.out.print(b + "\t" );
            }
            System.out.println();
        }
    }
}
        public static void main(String args[]) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter the dimension of the matrix : ");
        int dimension = Integer.parseInt(br.readLine());
        System.out.print("Enter the number of elements : ");
        int n = Integer.parseInt(br.readLine());   // total number of elements to be filled
        n = n/dimension;       // Get the number of rows and columns
        if(n % dimension != 0){
            // Not a Square matrix 
        }
        int circularArray[][] = new int[n][n];
        int k = 1, c1 = 0, c2 = n - 1, r1 = 0, r2 = n - 1;

        while (k <= n * n) {

            for (int i = c1; i <= c2; i++) {
                circularArray[r1][i] = k++;
            }

            for (int j = r1 + 1; j <= r2; j++) {
                circularArray[j][c2] = k++;
            }

            for (int i = c2 - 1; i >= c1; i--) {
                circularArray[r2][i] = k++;
            }

            for (int j = r2 - 1; j >= r1 + 1; j--) {
                circularArray[j][c1] = k++;
            }

            c1++;
            c2--;
            r1++;
            r2--;
        }

        /* Printing the Circular matrix */
        System.out.println("The Circular Matrix is:");
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(circularArray[i][j] + "\t");
            }
            System.out.println();
        }
    }

暂无
暂无

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

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