0

Swap the first and the last columns of array.

I`ve done it correctly for square matrixes and those when columns > strings. But something`s wrong with cases when strings > columns. How to fix it? import java.util.Scanner; public class Lab5_2 { static void lolSwap(String[][] matrix, byte n, byte m) { // Auxiliary array String[] helpingHand = new String[n]; if (n >= m) { for (byte i = 0; i < m; i++) { helpingHand[i] = matrix[n - 1][i]; } for (byte i = 0; i < m; i++) { matrix[n - 1][i] = matrix[0][i]; matrix[0][i] = helpingHand[i]; } } else { for (byte i = 0; i < n; i++) { helpingHand[i] = matrix[n - 1][i]; } for (byte i = 0; i < n; i++) { matrix[n - 1][i] = matrix[0][i]; matrix[0][i] = helpingHand[i]; } } } public static void main(String[] args) { Scanner input = new Scanner(System.in); byte n = input.nextByte(); byte m = input.nextByte(); input.close(); if (n < 2) { throw new IllegalArgumentException("There must be at least 2 columns"); } String[][] array = new String[n][m]; for (byte i = 0; i < n; i++) { for (byte j = 0; j < m; j++) { array[i][j] = "a" + (i + 1) + (j + 1); System.out.print(array[i][j] + "\t"); } System.out.println(); } System.out.println("\n"); lolSwap(array, n, m); for (byte i = 0; i < n; i++) { for (byte j = 0; j < m; j++) { System.out.print(array[i][j] + "\t"); } System.out.println(); } } }

2nd Oct 2019, 6:38 PM
Suspense
Suspense - avatar
2 Answers
+ 3
if "n" is the number of columns and "m" is number of rows then shouldn't it be , String[][] array = String[M][N] ; ?
2nd Oct 2019, 6:55 PM
Nidhi
0
Yes, it is.
2nd Oct 2019, 7:04 PM
Suspense
Suspense - avatar