Solve nothing! But explain why the result in the console is not displayed ??

import static java.lang.System.out; import java.util.Arrays; class Ideone{ public static void main (String[] args){ int[] arr={1,2,3,4,5,6,7}; task(arr); } public static String task(int[] array) { if (array.length <= 2) { throw new IllegalArgumentException("Нечего менять"); } for (int i = 1; i < array.length; i += 2) { array[i] = array[i] + array[i + 1]; array[i + 1] = array[i] - array[i + 1]; array[i] = array[i] - array[i + 1]; } return ("Массив:"+Arrays.toString(array)); }} 

(Task: In a given one-dimensional array, swap adjacent elements at even places with elements at odd ones)

  • So you in main did not print anything. - Alexey Sarovsky
  • The counter question - why should the result be displayed in the console? - post_zeew
  • I need to understand how the cycle works) - yonce

1 answer 1

Method task(arr); although it returns a string, it does not display it. It is worth writing System.out.println(task(arr)); , then the console returns the string returned by the task() method.

  • And you can just out.println(task(arr)); , since the author uses static import import static java.lang.System.out; . - post_zeew