Skip to content
This repository was archived by the owner on Dec 28, 2024. It is now read-only.

Commit 418cc4a

Browse files
committed
Лабораторная работа №26
1 parent 492fd6f commit 418cc4a

File tree

7 files changed

+147
-0
lines changed

7 files changed

+147
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<artifactId>23K0815</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0815-p26</artifactId>
12+
<description>Массивы</description>
13+
</project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package ru.mirea.practice.s0000001.task1;
2+
3+
import java.util.Stack;
4+
5+
public abstract class ArrayInverter {
6+
public static void main(String[] args) {
7+
int[] array = {1, 2, 3, 4, 5};
8+
9+
System.out.print("Начальный массив: ");
10+
printArray(array);
11+
12+
invertArray(array);
13+
14+
System.out.print("Инвертированный массив: ");
15+
printArray(array);
16+
}
17+
18+
public static void invertArray(int[] array) {
19+
Stack<Integer> stack = new Stack<>();
20+
21+
for (int value : array) {
22+
stack.push(value);
23+
}
24+
25+
for (int i = 0; i < array.length; i++) {
26+
array[i] = stack.pop();
27+
}
28+
}
29+
30+
public static void printArray(int[] array) {
31+
for (int value : array) {
32+
System.out.print(value + " ");
33+
}
34+
System.out.println();
35+
}
36+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package ru.mirea.practice.s0000001.task2;
2+
3+
public abstract class Main {
4+
public static void main(String[] args) {
5+
MyList<String> myList = new MyArrayList<>();
6+
myList.add("Hello");
7+
myList.add("World");
8+
myList.add("!");
9+
10+
MyIterator<String> iterator = myList.iterator();
11+
while (iterator.hasNext()) {
12+
System.out.println(iterator.next());
13+
}
14+
}
15+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package ru.mirea.practice.s0000001.task2;
2+
3+
class MyArrayList<E> implements MyList<E> {
4+
private Object[] elements;
5+
private int size;
6+
7+
public MyArrayList() {
8+
elements = new Object[10]; // Начальный размер массива
9+
size = 0;
10+
}
11+
12+
@Override
13+
public void add(E element) {
14+
if (size == elements.length) {
15+
resize();
16+
}
17+
elements[size++] = element;
18+
}
19+
20+
private void resize() {
21+
Object[] newArray = new Object[elements.length * 2];
22+
System.arraycopy(elements, 0, newArray, 0, elements.length);
23+
elements = newArray;
24+
}
25+
26+
@Override
27+
public E get(int index) {
28+
if (index < 0 || index >= size) {
29+
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
30+
}
31+
return (E) elements[index];
32+
}
33+
34+
@Override
35+
public int size() {
36+
return size;
37+
}
38+
39+
@Override
40+
public MyIterator<E> iterator() {
41+
return new MyIterator<>(this);
42+
}
43+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package ru.mirea.practice.s0000001.task2;
2+
3+
import java.util.Iterator;
4+
import java.util.NoSuchElementException;
5+
6+
class MyIterator<E> implements Iterator<E> {
7+
private MyArrayList<E> list;
8+
private int currentIndex;
9+
10+
public MyIterator(MyArrayList<E> list) {
11+
this.list = list;
12+
this.currentIndex = 0;
13+
}
14+
15+
@Override
16+
public boolean hasNext() {
17+
return currentIndex < list.size();
18+
}
19+
20+
@Override
21+
public E next() {
22+
if (!hasNext()) {
23+
throw new NoSuchElementException();
24+
}
25+
return list.get(currentIndex++);
26+
}
27+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ru.mirea.practice.s0000001.task2;
2+
3+
interface MyList<E> {
4+
5+
void add(E element);
6+
7+
E get(int index);
8+
9+
int size();
10+
11+
MyIterator<E> iterator();
12+
}

students/23K0815/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@
3333
<module>23K0815-p23</module>
3434
<module>23K0815-p24</module>
3535
<module>23K0815-p25</module>
36+
<module>23K0815-p26</module>
3637
</modules>
3738
</project>

0 commit comments

Comments
 (0)