help please deal with the line-by-line search for the maximum element, does not work

package untitled; import java.io.*; public class Main{ public static void main(String[] args) throws java.io.IOException{ int n=5, max=0, m=6, maxb=0, nn=3, cn=8; int[] a = new int[n]; //созданиС ΠΎΠ΄Π½ΠΎΠΌΠ΅Ρ€Π½ΠΎΠ³ΠΎ массива System.out.println("ΠžΠ΄Π½ΠΎΠΌΠ΅Ρ€Π½Ρ‹ΠΉ массив:"); for (int i = 0; i < a.length; i++) { //Π·Π°ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ массива случайными числами Π² ΠΈΠ½Ρ‚Π΅Ρ€Π²Π°Π»Π΅ [-7;5] a[i] = (int) Math.round((Math.random() * 13) - 7); System.out.print(a[i] + " "); } for (int i = 0; i < a.length; i++){ if (max < a[i]){ max=a[i]; } } System.out.println(" "); System.out.println("ΠœΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ элСмСнт ΠΎΠ΄Π½ΠΎΠΌΠ΅Ρ€Π½ΠΎΠ³ΠΎ массива " + max); int[][] b = new int[n][m]; System.out.println("Π”Π²ΡƒΠΌΠ΅Ρ€Π½Ρ‹ΠΉ массив:"); for(int i = 0; i<b.length; i++){ for(int j = 0; j<b[i].length; j++){ b[i][j]=a[i]>>j; System.out.format("%4d", b[i][j]); } System.out.println(); } for(int i = 0; i<b.length; i++){ for(int j = 0; j<b[i].length; j++){ if (b[i][maxb] < b[i][j]){ maxb=j; } } } System.out.println("ΠœΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ элСмСнт Π΄Π²ΡƒΠΌΠ΅Ρ€Π½ΠΎΠ³ΠΎ массива " + maxb); int c [][] = new int[nn][cn]; System.out.println("НСрСгулярный массив:"); for(int i = 0; i<c.length; i++){ for(int j = 0; j<c[i].length; j++){ c[i][j]=3*i+j+maxb; System.out.format("%4d", c[i][j]); } System.out.println(); } } } 

Closed due to the fact that the essence of the issue is incomprehensible by the participants in pavel , cheops , fori1ton , post_zeew , aleksandr barakin Oct 26 '16 at 13:02 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • four
    What exactly "does not work"? - Roman

3 answers 3

Let's localize the problem. Since the question is about finding the maximum element in a two-dimensional array, then we’ll focus on that.

 public static int getMax(int[][] arr) { if (arr.length == 0) throw new IllegalArgumentException(); // пустой массив, ΠΊΠΈΠ΄Π°Π΅ΠΌ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ // случай для всСх пустых Π²Π»ΠΎΠΆΠ΅Π½Π½Ρ‹Ρ… массивов Π½Π΅ Ρ€Π°ΡΡΠΌΠ°Ρ‚Ρ€ΠΈΠ²Π°ΡŽ. int result = Integer.MIN_VALUE; for (int[] i : arr) { for (int j : i) result = Math.max(result, j); } return result; } 

And the test for this method:

 public static void main(String[] args) { int[][] arr = {{10,20,30}, {5,7,8,99,14}, {}, {91}}; System.out.println(getMax(arr)); } 

Result:

 99 

PS: Try to divide the big task into several small ones. In one method, you define and fill two arrays, search for the maximum in both and then output to the screen. All in one heap. Think over several specialized methods for each of the tasks and implement them. Such code is easier to debug and maintain.

    If I understood correctly, then the problem arose when displaying the maximum value of the element of a two-dimensional array? Here is the working version:

     package untitled; import java.io.*; public class Main{ public static void main(String[] args) throws java.io.IOException { int n = 5, max = 0, m = 6, maxb = 0, nn = 3, cn = 8; int[] a = new int[n]; //созданиС ΠΎΠ΄Π½ΠΎΠΌΠ΅Ρ€Π½ΠΎΠ³ΠΎ массива System.out.println("ΠžΠ΄Π½ΠΎΠΌΠ΅Ρ€Π½Ρ‹ΠΉ массив:"); for (int i = 0; i < a.length; i++) { //Π·Π°ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ массива случайными числами Π² ΠΈΠ½Ρ‚Π΅Ρ€Π²Π°Π»Π΅ [-7; 5] a[i] = (int) Math.round((Math.random() * 13) - 7); System.out.print(a[i] + " "); } for (int i = 0; i < a.length; i++) { if (max < a[i]){ max = a[i]; } } System.out.println(" "); System.out.println("ΠœΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ элСмСнт ΠΎΠ΄Π½ΠΎΠΌΠ΅Ρ€Π½ΠΎΠ³ΠΎ массива " + max); int[][] b = new int[n][m]; System.out.println("Π”Π²ΡƒΠΌΠ΅Ρ€Π½Ρ‹ΠΉ массив:"); for (int i = 0; i < b.length; i++) { for(int j = 0; j < b[i].length; j++) { b[i][j] = a[i] >> j; System.out.format("%4d", b[i][j]); } System.out.println(); } for (int i = 0; i < b.length; i++) { for(int j = 0; j < b[i].length; j++) { if (maxb < b[i][j]){ maxb = b[i][j]; } } } System.out.println("ΠœΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ элСмСнт Π΄Π²ΡƒΠΌΠ΅Ρ€Π½ΠΎΠ³ΠΎ массива " + maxb); int c [][] = new int[nn][cn]; System.out.println("НСрСгулярный массив:"); for(int i = 0; i < c.length; i++){ for(int j = 0; j < c[i].length; j++){ c[i][j] = 3 * i + j + maxb; System.out.format("%4d", c[i][j]); } System.out.println(); } } } 

    The result was in the form:

     ΠžΠ΄Π½ΠΎΠΌΠ΅Ρ€Π½Ρ‹ΠΉ массив: -1 -5 1 6 2 ΠœΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ элСмСнт ΠΎΠ΄Π½ΠΎΠΌΠ΅Ρ€Π½ΠΎΠ³ΠΎ массива 6 Π”Π²ΡƒΠΌΠ΅Ρ€Π½Ρ‹ΠΉ массив: -1 -1 -1 -1 -1 -1 -5 -3 -2 -1 -1 -1 1 0 0 0 0 0 6 3 1 0 0 0 2 1 0 0 0 0 ΠœΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ элСмСнт Π΄Π²ΡƒΠΌΠ΅Ρ€Π½ΠΎΠ³ΠΎ массива 6 НСрСгулярный массив: 6 7 8 9 10 11 12 13 9 10 11 12 13 14 15 16 12 13 14 15 16 17 18 19 

    UPDATE

    The problem was with the following code snippet

      for(int i = 0; i<b.length; i++){ for(int j = 0; j<b[i].length; j++){ if (b[i][maxb] < b[i][j]){ maxb=j; } } } 

    Namely, an error was made in the condition if (b[i][maxb] < b[i][j]) and the action performed under the condition. Based on the task of these cycles, namely the search for the maximum value of the element of a two-dimensional array, it is necessary to determine which number will be greater from the original maxb=0; for use in the following cycles. As a consequence of the error in the condition, the variable maxb remained always equal to zero, which led to a further execution error of the algorithm.

    The problem was solved by correcting the condition in the given code fragment, as well as the action performed in case the condition is true.

      for (int i = 0; i < b.length; i++) { for(int j = 0; j < b[i].length; j++) { if (maxb < b[i][j]){ maxb = b[i][j]; } } } System.out.println("ΠœΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ элСмСнт Π΄Π²ΡƒΠΌΠ΅Ρ€Π½ΠΎΠ³ΠΎ массива " + maxb); 

    In this fragment, we compare the value of the variable maxb with the value of the element of a two-dimensional array. If the maxb value maxb less than the value of the array element, then this value is assigned to the maxb variable.

    • one
      It is possible, for the sake of decency, to at least explain what the problem was and why the solution is - Alexey Shimansky
    • Finished in the update. Mistakenly decided that the problem is obvious to all. - A pogromist engineer

    I tried to make a universal solution for the multidimensional case. I think it turned out not much started:

     @SuppressWarnings("unchecked") private static <R, E extends Comparable<? super E>> E getMax(R[] array) { Comparator<E> comparator = (e1, e2) -> e1 == null ? (e2 == null ? 0 : -1) : (e2 == null ? 1 : e1.compareTo(e2)); E result = null; for (R e : array) if (e.getClass().isArray()) { E localMax = getMax((R[]) e); result = comparator.compare(localMax, result) > 0 ? localMax : result; } else result = comparator.compare((E) e, result) > 0 ? (E) e : result; return result; } public static void main(String[] args){ Integer[] a1 = new Integer[]{1, 0, 10, 100, 4, 19}; Integer r1 = getMax(a1); System.out.println(r1); Integer[] a2 = new Integer[]{19, 199191, 999, -1, 10}; Integer[][] a3 = new Integer[][]{a1, a2}; Integer r2 = getMax(a3); System.out.println(r2); Integer[][] a4 = new Integer[][]{new Integer[]{1, 202, 2000, 1000000}, a1}; Integer[][][] a5 = new Integer[][][]{a3, a4}; Integer r3 = getMax(a5); System.out.println(r3); }