Skip to content

Instantly share code, notes, and snippets.

@YanchevskayaAnna
Created June 8, 2016 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YanchevskayaAnna/5128f158b06b0e5ec2f49c7a1dde3021 to your computer and use it in GitHub Desktop.
Save YanchevskayaAnna/5128f158b06b0e5ec2f49c7a1dde3021 to your computer and use it in GitHub Desktop.
ACO13_HomeWork
package homeWork.myArrayList;
import java.util.Arrays;
/**
* Created by mykhailov on 04.06.2016.
*/
public class MyArrayList {
private Object[] myArray;
private int size;
// private int capacity;
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) { //Yanchevskaya A. В аналогичном методе ArrayList add - это функция
if (index < 0 || index > myArray.length)
throw new IndexOutOfBoundsException();
if (size() == myArray.length)
expandCapacity(myArray);
for (int i = size; i > index; i--) //Yanchevskaya A. Можно циклом, можно System.arraycopy. System.arraycopy - работает быстрее
myArray[i] = myArray[i - 1];
// add element
myArray[index] = object;
size++;
}
private Object[] expandCapacity(Object[] myArray) {
Object[] newArray = new Object[this.myArray.length * 2];
System.arraycopy(this.myArray, 0, newArray, 0, size());
this.myArray = newArray;
return newArray;
}
public boolean add(Object object) {
if (size > myArray.length) //Yanchevskaya A. А если равен?
myArray = expandCapacity(myArray);
add(size(), object); //Yanchevskaya A. Можно так, можно напрямую myArray[size++] = object; Чтобы заново не проходить проверки
return false;
}
public Object get(int index) {
return myArray[index]; //Yanchevskaya A. А если индекс отрицательный или за рамками массива?
}
public Object remove(int index) {
if (index >= 0 && index < this.size()) {
Object temp = myArray[index];
for (int i = index; i < this.size() - 1; i++) //Yanchevskaya A. Можно циклом, можно System.arraycopy. System.arraycopy - работает быстрее
myArray[index] = myArray[index + 1];
size--; //Yanchevskaya A. Нужно также обнулять последний элемент myArray[--size] = null;
return temp;
}
return false;
}
public boolean remove(int index, Object object) { //Yanchevskaya A. где используется индекс? Не нужен, только object
if (object == null) {
for (index = 0; index < size; index++)
if (myArray[index] == null) {
remove(index); // Yanchevskaya A. Можно и так, но в этом методе есть и не нужные операции. В ArrayList создан спец. метод fastRemove()
return true;
}
} else {
for (index = 0; index < size; index++)
if (object.equals(myArray[index])) {
remove(index);
return true;
}
}
return false;
}
public Object set(int index, Object newObject) {
if (index < 0 || index >= myArray.length - 1)//Yanchevskaya A. Индекс не может быть равен (myArray.length - 1)?
throw new IndexOutOfBoundsException();
Object oldObject = myArray[index];
myArray[index] = newObject;
return oldObject;
}
public void clear() {
myArray = null; //Yanchevskaya A. См. как работает данный метод в AL
size = 0;
}
public boolean contains(Object object) { //Yanchevskaya A. См. как работает данный метод в AL Нужно учитывать, что среди элементов может быть и null (даже до size)
for (int i = 0; i < this.size(); i++) {
if (this.myArray[i].equals(object))
return true;
}
return false;
}
@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