-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
6.4.2. Обход массива с последнего элемента;
- Loading branch information
Showing
3 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |