-
Notifications
You must be signed in to change notification settings - Fork 0
ArrayList/JAEKWANG97 #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JAEKWANG97
wants to merge
1
commit into
main
Choose a base branch
from
ArrayList-JAEKWANG97
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,73 @@ | ||
| package List.ArrayList; | ||
|
|
||
| import List.List; | ||
|
|
||
| public class ArrayList_JAEKWANG97<E> implements List<E> { | ||
| private static final int DEFAULT_CAPACITY = 10; | ||
| private int size; | ||
| private Object[] elementData; | ||
|
|
||
| public ArrayList_JAEKWANG97() { | ||
| this.size = 0; | ||
| this.elementData = new Object[DEFAULT_CAPACITY]; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean contains(Object o) { | ||
| for (int i = 0; i < size; i++) { | ||
| if (elementData[i].equals(o)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| @Override | ||
| public E get(int index) { | ||
| if (index < 0 || index >= size) { | ||
| throw new IndexOutOfBoundsException(); | ||
| } | ||
| return (E) elementData[index]; | ||
| } | ||
|
|
||
| @Override | ||
| public void insert(Object data) { | ||
| elementData[size] = data; | ||
| size++; | ||
|
|
||
| if (size == elementData.length) { | ||
| grow(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isEmpty() { | ||
| return size == 0; | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| @Override | ||
| public E remove(int index) { | ||
| if (index < 0 || index >= size) { | ||
| throw new IndexOutOfBoundsException(); | ||
| } | ||
| E removedElement = (E) elementData[index]; | ||
| System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); | ||
| size--; | ||
| return removedElement; | ||
| } | ||
|
|
||
| @Override | ||
| public int size() { | ||
| return size; | ||
| } | ||
|
|
||
| private void grow() { | ||
| int newCapacity = elementData.length * 2; | ||
| Object[] newElementData = new Object[newCapacity]; | ||
| System.arraycopy(elementData, 0, newElementData, 0, elementData.length); | ||
| elementData = newElementData; | ||
| } | ||
|
|
||
| } | ||
61 changes: 61 additions & 0 deletions
61
src/test/java/List/ArrayList/ArrayListTest_JAEKWANG97.java
This file contains hidden or 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,61 @@ | ||
| package List.ArrayList; | ||
|
|
||
| import List.List; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
|
||
| public class ArrayListTest_JAEKWANG97 { | ||
|
|
||
| @Test | ||
| void ArrayList์_์์ฑ_๋ฐ_์ด๊ธฐํ() { | ||
| // ๊ตฌํ ํ ์์ | ||
| List<Integer> list = new ArrayList_JAEKWANG97<Integer>(); | ||
|
|
||
| assertThat(list).isNotNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void ๋ฐ์ดํฐ_5๊ฐ_์ ์ฅ() { | ||
| // ๊ตฌํ ํ ์์ | ||
| List<Integer> list = new ArrayList_name<Integer>(); | ||
|
|
||
| list.insert(11); | ||
| list.insert(11); | ||
| list.insert(22); | ||
| list.insert(22); | ||
| list.insert(33); | ||
|
|
||
| assertThat(list.size()).isEqualTo(5); | ||
| assertThat(list.get(0)).isEqualTo(11); | ||
| assertThat(list.get(1)).isEqualTo(11); | ||
| assertThat(list.get(2)).isEqualTo(22); | ||
| assertThat(list.get(3)).isEqualTo(22); | ||
| assertThat(list.get(4)).isEqualTo(33); | ||
| } | ||
|
|
||
| @Test | ||
| void ๋ฐ์ดํฐ_5๊ฐ_์ ์ฅ_ํ_22์ธ_๋ฐ์ดํฐ_๋ชจ๋_์ญ์ () { | ||
| // ๊ตฌํ ํ ์์ | ||
| List<Integer> list = new ArrayList_name<Integer>(); | ||
|
|
||
| list.insert(11); | ||
| list.insert(11); | ||
| list.insert(22); | ||
| list.insert(22); | ||
| list.insert(33); | ||
|
|
||
| // Iterable์ ๊ตฌํํ๋ฉด enhanced for loop๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. | ||
| for (int i = 0; i < list.size(); i++) { | ||
| if (list.get(i) == 22) { | ||
| list.remove(i); | ||
| i--; // ์ญ์ ๋๋ฉด index๊ฐ ์กฐ์ ๋๋ฏ๋ก ํด๋น index๋ถํฐ ๋ค์ ํ์ธํด์ผ ํฉ๋๋ค. | ||
| } | ||
| } | ||
|
|
||
| assertThat(list.size()).isEqualTo(3); | ||
| assertThat(list.get(0)).isEqualTo(11); | ||
| assertThat(list.get(1)).isEqualTo(11); | ||
| assertThat(list.get(2)).isEqualTo(33); | ||
| } | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ๋ ์ถ๊ฐ ์ size๋ฅผ ํ์ธ ์ฉ๋์ ๋๋ ค์ฃผ์์ต๋๋ค. size๊ฐ ์ฉ๋์ ๋์ผ๋ฉด, 2๋ฐฐ๋ก ๋ฐ๋ก ๋๋ ค์ฃผ๊ฒ ๋๋ฉด ์ถ๊ฐ๋ก element๋ฅผ ๋ฃ์ง ์์ผ๋ฉด ๋ฉ๋ชจ๋ฆฌ ๋ญ๋น๊ฐ ์๊ธฐ์ง ์์๊น์? @JAEKWANG97 ๋์ ์๊ฒฌ์ด ๊ถ๊ธํด์!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ๋ ๊ถ๊ธํฉ๋๋ค!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hellomatia ๋ ์ข์ ์ง์ ๊ฐ์ฌ๋๋ฆฝ๋๋ค. ๋ง์ํด์ฃผ์ ๋๋ก, size๊ฐ ์ฉ๋์ ์ด๊ณผํ๋ ๊ฒฝ์ฐ ๋ฐฐ์ด ํฌ๊ธฐ๋ฅผ 2๋ฐฐ๋ก ๋๋ฆฌ๋ ๋ก์ง์ ์ค์ ๋ก ๋ฉ๋ชจ๋ฆฌ ๋ญ๋น๋ฅผ ์ผ์ผํฌ ์ ์์ต๋๋ค. ์ด ๋ถ๋ถ์ ๋ํด ์ถฉ๋ถํ ๊ณ ๋ คํ์ง ๋ชปํ๋ ๊ฒ ๊ฐ์ต๋๋ค. ๊ท์คํ ํผ๋๋ฐฑ์ ํตํด ๋ง์ด ๋ฐฐ์ฐ๊ฒ ๋์์ต๋๋ค. ๊ฐ์ฌํฉ๋๋ค!