Skip to content

Commit

Permalink
6.4.2. Обход массива с последнего элемента;
Browse files Browse the repository at this point in the history
  • Loading branch information
Temzor committed Sep 28, 2023
1 parent 0f463ab commit cb7f714
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/main/java/ru/j4j/array/ReverseLoopForArray.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
11 changes: 11 additions & 0 deletions src/main/java/ru/j4j/array/RollBackArray.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
39 changes: 39 additions & 0 deletions src/test/java/ru/j4j/array/RollBackArrayTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit cb7f714

Please sign in to comment.