Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java用特定方法填充2D数组_Java_Arrays_Matrix - Fatal编程技术网

java用特定方法填充2D数组

java用特定方法填充2D数组,java,arrays,matrix,Java,Arrays,Matrix,我有一个2D数组(矩阵)和一个整数n.n是全平方 例如,如果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; }

我有一个2D数组(矩阵)和一个整数n.n是全平方

例如,如果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();
        }
    }
}
publicstaticvoidmain(字符串参数[]){
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
System.out.print(“输入矩阵的维度:”);
int-dimension=Integer.parseInt(br.readLine());
System.out.print(“输入元素数:”);
int n=Integer.parseInt(br.readLine());//要填充的元素总数
n=n/dimension;//获取行数和列数
如果(n%维度!=0){
//不是方阵
}
整数循环数组[][]=新整数[n][n];
int k=1,c1=0,c2=n-1,r1=0,r2=n-1;

而(k)你可以写一个循环,这样填充它。
        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();
        }
    }