Skip to content

Comments

Created custom ArrayList#1

Open
frolovGeorgy wants to merge 1 commit intoiaulitin:mainfrom
frolovGeorgy:main
Open

Created custom ArrayList#1
frolovGeorgy wants to merge 1 commit intoiaulitin:mainfrom
frolovGeorgy:main

Conversation

@frolovGeorgy
Copy link

No description provided.


# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
/.idea/
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

out еще надо бы добавить и iml

@@ -0,0 +1,226 @@
package Frolov;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Пакеты обычно не содержат в именах заглавных букв.


import java.util.*;

public class GarriksArrayList implements List {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

На дженерики совсем забил. Есть желание переделать с ними?


@Override
public boolean isEmpty() {
return elemsCount <= 0;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А почему не просто ==?

return elemsCount <= 0;
}

// тут лучше использовать == или equals? Я понимаю, что equals вернет true в отличии от ==, если экзмепляры разные, но все поля одинаковые.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А надо в таких случаях смотреть в джавадоки интерфейсов.

Для List # contains написано следующее:

Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that Objects.equals(o, e).


@Override
public void clear() {
// допускается ли так писать?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В целом да, конечно. Но ты можешь просто содержимое конструктора без аргументов утащить в отдельный метод,
Будешь вызывать его из конструктора и из clear.


public GarriksArrayList() {
this.elemsCount = 0;
this.realArr = new Object[10];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Magic numbers - 10

return new GarriksListIterator(this);
}

// я же правильно понял, что этот метод возвращает итератор, начиная от указанного индекса i?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Да, все так.

Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list. The specified index indicates the first element that would be returned by an initial call to next. An initial call to previous would return the element with the specified index minus one.

//
@Override
public Object[] toArray(Object[] objects) {
return new Object[0];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Чего-то тут странное

boolean result = false;
for (Object elem : collection) {
if (!this.contains(elem)) {
this.remove(elem);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Удаление элементов в этом методе и следующих неоптимально.
Лучше бы было набрать кандидатов на удаление, и потом разом их удалить, выполнив один сдвиг. Сейчас у тебя на каждый такой удаленный элемент будет по сдвигу.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants