Help me please. There is an array of numbers short A [7] [7]. It is necessary to remove from the array all the columns in which there is the maximum element of the array (For example, if the maximum element of the array is 99, then it is necessary to delete all the columns in which the number 99 is present).

public class Main { static short MAX = 0; static void init(short[][] A) { for (int i = 0; i < A.length; ++i) { for (int j = 0; j < A[i].length; ++j) { A[i][j] = A[i][j] = (short) ((int) (Math.random() * 100)); } } } static void Print(short[][] A) { for (int i = 0; i < A.length; ++i) { for (int j = 0; j < A[i].length; ++j) { System.out.print(A[i][j] + "\t"); } System.out.println(); } } static void Search(short A[][]) { short max = 0; for (int i = 0; i < A.length; ++i) { for (int j = 0; j < A.length; ++j) { if (A[i][j] > max) { max = A[i][j]; } } } MAX = max; System.out.println("ΠœΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ элСмСнт Π² массивС: " + max); } static void DeleteByIndex(short A[][], int index) { System.out.println(); for (int i = 0; i < A.length; i++) { for (int j = 0; j < A[0].length - 1; j++) { if (j >= index - 1) { A[i][j] = A[i][j + 1]; } System.out.print(A[i][j] + "\t"); } System.out.println(); } } public static void main(String[] args) { int N = 7; short[][] array = new short[N][N]; init(array); Print(array); System.out.println(); Search(array); } } 

Closed due to the fact that off-topic by participants αλΡχολυτ , Kirill Stoianov , cheops , fori1ton , Streletz 27 Sep '16 at 1:00 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • β€œQuestions asking for help with debugging (β€œ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - αλΡχολυτ, Kirill Stoianov, cheops, fori1ton
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • and where is the code that you wrote with an error? - Vladislav Kuznetsov

1 answer 1

 import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Arrays; public class Main2 { static short MAX = 0; static void init(Short[][] A) { for (int i = 0; i < A.length; ++i) { for (int j = 0; j < A[i].length; ++j) { A[i][j] = A[i][j] = (short) ((int) (Math.random() * 100)); } } } static void Search(Short A[][]) { short max = 0; for (int i = 0; i < A.length; ++i) { for (int j = 0; j < A.length; ++j) { if (A[i][j] > max) { max = A[i][j]; } } } MAX = max; System.out.println("ΠœΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ элСмСнт Π² массивС: " + max); } public static <T> T[] deleteElement(T[] src, int idx) { if (idx > src.length - 1) throw new RuntimeException("idx > src length"); @SuppressWarnings("unchecked") T[] dest = (T[]) Array.newInstance(src[0].getClass(), src.length - 1); for (int i = 0; i < src.length; i++) { if (i < idx) { dest[i] = src[i]; } else if (i > idx) { dest[i - 1] = src[i]; } } return dest; } public static <T> T[][] deleteRow(T[][] src, int idx) { if (idx > src.length - 1) throw new RuntimeException("idx > src length"); for (int i = 0; i < src.length; i++) { src[i] = deleteElement(src[i], idx); } return src; } static <T> void print2DArray(T[][] src) { for (int i = 0; i < src.length; i++) { System.out.println(Arrays.toString(src[i])); } } public static void main(String[] args) { int N = 7; /*Short[][] array = { {1, 2, 3, 4}, {11, 22, 33, 44}, {111, 333, 333, 44}, };*/ Short[][] array = new Short[N][N]; init(array); print2DArray(array); System.out.println(); Search(array); int max; ArrayList<Integer> rowsIndex = new ArrayList<>(); max = array[0][0]; for (int r = 0; r < array.length; r++) { for (int c = 0; c < array.length; c++) { if (array[r][c] > max) { max = array[r][c]; } } } for (int r = 0; r < array.length; r++) { for (int c = 0; c < array.length; c++) { if (array[r][c] == max) rowsIndex.add(c); } } int iter = 0; for (Integer c1 : rowsIndex) { array = deleteRow(array, c1 - iter); /* for (int i = 0; i < array.length; i++) { for (int c = c1; c < array.length - 1; c++) array[i][c] = array[i][c + 1]; }*/ iter++; } print2DArray(array); } } 

conclusion

 [10, 27, 50, 60, 90, 9, 2] [49, 33, 14, 80, 71, 30, 19] [84, 6, 44, 34, 16, 40, 4] [38, 19, 63, 0, 4, 37, 51] [15, 26, 32, 65, 95, 0, 88] [16, 89, 68, 94, 77, 54, 35] [41, 1, 84, 71, 59, 94, 28] ΠœΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ элСмСнт Π² массивС: 95 [10, 27, 50, 60, 9, 2] [49, 33, 14, 80, 30, 19] [84, 6, 44, 34, 40, 4] [38, 19, 63, 0, 37, 51] [15, 26, 32, 65, 0, 88] [16, 89, 68, 94, 54, 35] [41, 1, 84, 71, 94, 28] Process finished with exit code 0 

conclusion when several highs

 [1, 2, 3, 4] [11, 22, 33, 44] [111, 333, 333, 44] ΠœΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ элСмСнт Π² массивС: 333 [1, 4] [11, 44] [111, 44] Process finished with exit code 0 
  • This is not exactly that. I need to remove all the bars in which the maximum element occurs. For example, here the maximum element is 333, but it can occur in the matrix several times. Then you need to delete all the columns in which it is located. - Kirill Sadovy
  • @ KirillSadovy corrected. will it come down? - Senior Pomidor