1 package com
.github
.puzzles
.util
;
3 import java
.util
.Arrays
;
5 public class Matrices
{
7 //Don't let anyone instance this class.
12 * Copy 2 dimensions array.
14 * @param original the matrix which you want make a copy of it.
15 * @return the new copy.
17 public static <T
> T
[][] copyOf(T
[][] original
, int colsLength
, int rowsLength
) {
18 T
[][] returnedPuzzle
= Arrays
.copyOf(original
, colsLength
);
19 for (int i
= 0; i
< colsLength
; i
++) {
20 returnedPuzzle
[i
] = Arrays
.copyOf(original
[i
], rowsLength
);
23 return returnedPuzzle
;
26 public static <T
> T
[][] copyOf(T
[][] original
) {
27 if (original
.length
< 0) {
30 return copyOf(original
, original
.length
, original
[0].length
);
34 * Fill a value in a matrix.
36 * @param matrix the matrix which you want to fill in it.
37 * @param val the value which you want fill it in the matrix.
39 public static <T
> void fill(T
[][] matrix
, T val
) {
40 for (int i
= 0; i
< matrix
.length
; i
++) {
41 Arrays
.fill(matrix
[i
], val
);
45 public static <T
> int getWidth(T
[][] matrix
) {
52 public static <T
> int getHeight(T
[][] matrix
) {
53 if ((getWidth(matrix
) != 0) && (matrix
[0] != null)) {
54 return matrix
[0].length
;
59 //Swap two elements with XOR swap algorithm.
60 public static void swap(Integer
[][] matrix
, int fromX
, int fromY
, int toX
, int toY
){
61 matrix
[fromX
][fromY
] = matrix
[fromX
][fromY
] ^ matrix
[toX
][toY
];
62 matrix
[toX
][toY
] = matrix
[fromX
][fromY
] ^ matrix
[toX
][toY
];
63 matrix
[fromX
][fromY
] = matrix
[fromX
][fromY
] ^ matrix
[toX
][toY
];