How to swap the elements of an array and put them in a column? The user himself enters the number in the array, an indefinite number
Suppose the user has entered 4 numbers, i.e. an array of 4 elements in length
According to the task, the elements should be interchanged, according to this scheme
1 2 3 4
2 3 4 1
3 4 1 2
4 3 2 1
I just can not figure out how to do it. Wrote the code only to the point where you need to enter elements.

public static void main(String[] args) { System.out.println("Введите длину массива: "); Scanner scan = new Scanner(System.in); int size = scan.nextInt(); int [] array = new int[size]; System.out.println("Введите элементы массива: "); for (int i = 0; i < size; i++) array[i] = scan.nextInt(); String one = Arrays.toString(array); } } 
  • There is no pattern in this system. For 4 numbers, for example, of course, we clearly rearrange according to your scheme. And if the numbers 10? - Olexiy Morenets 8:40 pm

1 answer 1

 //int[] result = new int[size * size]; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { //result[i * size + j] = array[(i + j) % size]; System.out.print(array[(i + j) % size]); System.out.print(" "); } System.out.println(); } 
  • Thanks, and you could write in detail what the line System.out.print (array [(i + j)% size] does)? - LoopEx
  • @LoopEx I could). Try you. The % operator is called the remainder of a division by. - Igor
  • That's it, I figured it out, thanks for the help, I'm just stupid :) - LoopEx
  • @LoopEx This is the main thing - that you yourself have built the logic of the code in your head. Successes! - Igor