Created
June 11, 2016 20:48
-
-
Save YanchevskayaAnna/50cc2e80f6880fb14160f8741eaaf943 to your computer and use it in GitHub Desktop.
MyArrayList_MihaylovA
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
package homeWork.myArrayList; | |
import java.util.Arrays; | |
/** | |
* Created by mykhailov on 04.06.2016. | |
*/ | |
public class MyArrayList { | |
private Object[] myArray; | |
private int size; | |
public static final int DEFAULT_CAPACITY = 10; | |
public MyArrayList() { | |
this(DEFAULT_CAPACITY); | |
} | |
public MyArrayList(int capacity) { | |
if (capacity < 0) { | |
throw new IllegalArgumentException("capacity: " + capacity); | |
} | |
myArray = new Object[capacity]; | |
size = 0; | |
} | |
public int size() { | |
return size; | |
} | |
public void add(int index, Object object) { | |
if (index < 0 && index >= myArray.length) | |
throw new IndexOutOfBoundsException(); | |
expandCapacity(); | |
System.arraycopy(myArray, size - 1, myArray, index, size - index); | |
// add element | |
myArray[index] = object; | |
size++; | |
} | |
public void expandCapacity() { | |
if (size >= myArray.length) { | |
/* Yanchevskaya A. можно в одну строку кода | |
Object[] newArray = new Object[this.myArray.length * 2]; | |
System.arraycopy(this.myArray, 0, newArray, 0, size()); | |
this.myArray = newArray; */ | |
myArray = Arrays.copyOf(myArray, myArray.length * 2); | |
} | |
} | |
public boolean add(Object object) { | |
/* Yanchevskaya A. проверила, работает. | |
if (size < myArray.length) { | |
myArray[size++] = object; | |
} else if (size == myArray.length) | |
expandCapacity(); | |
// myArray[size++] = object; // так не работает | |
add(size(), object); // а так add элемент к поседнему эл. в массиве*/ | |
expandCapacity(); | |
myArray[size++] = object; | |
return false; //Yanchevskaya A. а почему возвращает false? | |
} | |
public Object get(int index) { | |
if (index >= size || index < 0 || index >= myArray.length) //Yanchevskaya A. достаточно проверки на отрицательный и на то что меньше size | |
throw new ArrayIndexOutOfBoundsException(index); | |
else | |
return myArray[index]; | |
} | |
public Object remove(int index) { | |
if (index >= size && index < 0 && index >= myArray.length) //Yanchevskaya A. достаточно проверки на отрицательный и на то что меньше size | |
throw new ArrayIndexOutOfBoundsException(index); | |
if (index >= 0 && index < this.size()) { | |
Object temp = myArray[index]; | |
fastRemove(index); | |
return temp; | |
} | |
return false; | |
} | |
public void fastRemove(int index) { | |
int moved = size - index - 1; | |
System.arraycopy(myArray, index + 1, myArray, index, moved); | |
myArray[size--] = null; | |
} | |
public boolean remove(Object object) { | |
if (object == null) { | |
for (int i = 0; i < size; i++) | |
if (myArray[i] == null) { | |
fastRemove(i); | |
return true; | |
} | |
} else { | |
for (int j = 0; j < size; j++) | |
if (object.equals(myArray[j])) { | |
fastRemove(j); | |
return true; | |
} | |
} | |
return false; | |
} | |
public Object set(int index, Object newObject) { | |
if (index < 0 || index > myArray.length - 1) | |
throw new IndexOutOfBoundsException(); | |
Object oldObject = myArray[index]; | |
myArray[index] = newObject; | |
return oldObject; | |
} | |
public void clear() { | |
for (int i = 0; i < size; i++) { | |
myArray[i] = null; | |
size = 0; | |
} | |
} | |
public boolean contains(Object object) { | |
return indexOf(object) != -1; | |
} | |
public int indexOf(Object object) { | |
if (object == null) { | |
for (int i = 0; i < size; i++) | |
if (myArray[i] == null) return i; | |
} else { | |
for (int j = 0; j < size; j++) | |
if (object.equals(myArray[j])) return j; | |
} | |
return -1; | |
} | |
@Override | |
public String toString() { | |
return "MyArrayList{" + | |
"myArray=" + Arrays.toString(myArray) + | |
'}'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment