import
java.util.Arrays;
public
class
MatrixColumnSwap {
public
static
void
swapColumns(
int
[][] matrix,
int
col1,
int
col2)
{
if
(col1 == col2) {
return
;
}
int
num_rows = matrix.length;
for
(
int
row =
0
; row < num_rows; row++) {
int
temp = matrix[row][col1];
matrix[row][col1] = matrix[row][col2];
matrix[row][col2] = temp;
}
}
public
static
void
printMatrix(
int
[][] matrix)
{
for
(
int
[] row : matrix) {
for
(
int
element : row) {
System.out.print(element + " ");
}
System.out.println();
}
}
public
static
void
main(String[] args)
{
int
[][] matrix
= { {
1
,
2
,
3
}, {
4
,
5
,
6
}, {
7
,
8
,
9
} };
System.out.println("Original Matrix:");
printMatrix(matrix);
int
col1 =
0
;
int
col2 =
2
;
if
(
0
<= col1 && col1 < matrix[
0
].length
&&
0
<= col2 && col2 < matrix[
0
].length) {
swapColumns(matrix, col1, col2);
System.out.println(
"\nMatrix after swapping columns:");
printMatrix(matrix);
}
else
{
System.out.println(
"Invalid column indices
for
swapping.");
}
}
}