From cb7f714d0c75608333eec80d5c4db5aff1eea896 Mon Sep 17 00:00:00 2001 From: "d.kapustin" Date: Thu, 28 Sep 2023 12:46:30 +0300 Subject: [PATCH] =?UTF-8?q?6.4.2.=20=D0=9E=D0=B1=D1=85=D0=BE=D0=B4=20?= =?UTF-8?q?=D0=BC=D0=B0=D1=81=D1=81=D0=B8=D0=B2=D0=B0=20=D1=81=20=D0=BF?= =?UTF-8?q?=D0=BE=D1=81=D0=BB=D0=B5=D0=B4=D0=BD=D0=B5=D0=B3=D0=BE=20=D1=8D?= =?UTF-8?q?=D0=BB=D0=B5=D0=BC=D0=B5=D0=BD=D1=82=D0=B0;=20https://job4j.ru/?= =?UTF-8?q?profile/exercise/2/task-view/101?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ru/j4j/array/ReverseLoopForArray.java | 12 ++++++ src/main/java/ru/j4j/array/RollBackArray.java | 11 ++++++ .../java/ru/j4j/array/RollBackArrayTest.java | 39 +++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 src/main/java/ru/j4j/array/ReverseLoopForArray.java create mode 100644 src/main/java/ru/j4j/array/RollBackArray.java create mode 100644 src/test/java/ru/j4j/array/RollBackArrayTest.java 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