diff --git a/src/main/java/ru/j4j/array/ReverseLoopForArray.java b/src/main/java/ru/j4j/array/ReverseLoopForArray.java new file mode 100644 index 0000000..db394be --- /dev/null +++ b/src/main/java/ru/j4j/array/ReverseLoopForArray.java @@ -0,0 +1,12 @@ +package ru.j4j.array; + +public class ReverseLoopForArray { + public static void main(String[] args) { + int[] numbers = {1, 2, 3, 4, 5, 6, 7}; + System.out.println("Элементы массива в обратном порядке: "); + for (int i = 0; i < numbers.length; i++) { + int el = numbers[numbers.length - i - 1]; + System.out.println(el); + } + } +} diff --git a/src/main/java/ru/j4j/array/RollBackArray.java b/src/main/java/ru/j4j/array/RollBackArray.java new file mode 100644 index 0000000..41e116c --- /dev/null +++ b/src/main/java/ru/j4j/array/RollBackArray.java @@ -0,0 +1,11 @@ +package ru.j4j.array; + +public class RollBackArray { + public static int[] rollback(int[] array) { + int[] result = new int[array.length]; + for (int index = 0; index < array.length; index++) { + result[index] = array[array.length - 1 - index]; + } + return result; + } +} diff --git a/src/test/java/ru/j4j/array/RollBackArrayTest.java b/src/test/java/ru/j4j/array/RollBackArrayTest.java new file mode 100644 index 0000000..fed7210 --- /dev/null +++ b/src/test/java/ru/j4j/array/RollBackArrayTest.java @@ -0,0 +1,39 @@ +package ru.j4j.array; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class RollBackArrayTest { + @Test + public void whenEmpty() { + int[] input = new int[] {}; + int[] expected = new int[] {}; + int[] result = RollBackArray.rollback(input); + assertThat(result).containsExactly(expected); + } + + @Test + public void whenOne() { + int[] input = new int[] {1}; + int[] expected = new int[] {1}; + int[] result = RollBackArray.rollback(input); + assertThat(result).containsExactly(expected); + } + + @Test + public void whenFull() { + int[] input = new int[] {1, 2, 3, 4}; + int[] expected = new int[] {4, 3, 2, 1}; + int[] result = RollBackArray.rollback(input); + assertThat(result).containsExactly(expected); + } + + @Test + public void whenTheSame() { + int[] input = new int[] {1, 1, 1, 1}; + int[] expected = new int[] {1, 1, 1, 1}; + int[] result = RollBackArray.rollback(input); + assertThat(result).containsExactly(expected); + } +} \ No newline at end of file