Skip to content

Instantly share code, notes, and snippets.

@kbutti
Created March 2, 2013 19:56
Show Gist options
  • Select an option

  • Save kbutti/5072996 to your computer and use it in GitHub Desktop.

Select an option

Save kbutti/5072996 to your computer and use it in GitHub Desktop.
ArrayList for Doja v1.2
package p.util;
import p.struct.Cloneable;
/**
* Doja(iアプリ環境)用の高速な挿入・削除を行う動的配列クラス。
* 配列内に加えられた要素の順序は保証されません。
* 具体的にはremoveAt() 実行後に addElement() を呼び出すと、新しい要素は削除された要素の位置に配置されます。
*
* @author niffy(http://niffy67.blog110.fc2.com/) + kbutti
* @version 1.2.20130303
*
*/
public class ArrayList {
protected Object[] items;
protected int itemCount;
public ArrayList() {
items = new Object[16];
// itemCount = 0;
}
public ArrayList(int size) {
items = new Object[size];
// itemCount = 0;
}
public void add(Object element) {
addElement(element);
}
public void addElement(Object element) {
if (itemCount < items.length) {
items[itemCount] = element;
itemCount++;
} else {
int increment = (items.length >> 1);
if (increment <= 1) increment = 2;
realloc(items.length + increment);
addElement(element);
}
}
public Object get(int index) {
return elementAt(index);
}
public Object elementAt(int index) {
return items[index];
// throws ArrayIndexOutOfBoundsException,
// NoSuchElementException
}
public boolean removeElement(Object element) {
int count = 0;
for (int i = 0; i < items.length - 1; i++) {
if (items[i].equals(element)) {
removeElementAt(i);
i--;
count++;
}
}
return count != 0 ? true : false;
}
public void removeAllElements() {
itemCount = 0;
}
/**
* 保持している要素たちへの参照を、新しいArrayList を作りコピーします。
* コピーしているのは、インスタンスへの参照であって、実体でない事に注意してください。(浅いコピー)。
*/
public ArrayList clone() {
ArrayList newone = new ArrayList(size());
copyInto(newone.items);
return newone;
}
/**
* 要素はすべて Cloneable である必要がある。
* Cloneable は Doja には存在しないので独自に定義します。
* 実装先では、個々のプロパティをコピーする処理を追加してください。
* public interface Cloneable { public Object clone();}
*/
public ArrayList deepClone() {
ArrayList newone = new ArrayList(size());
try {
for (int i = 0; i < size(); i++) {
newone.items[i] = ((Cloneable) items[i]).clone();
}
} catch (ClassCastException e) {
Log.p("Item is not a Cloneable object.");
e.printStackTrace();
}
return newone;
}
/**
* capacity(最大容量) を増減します。
*
* @param size
* 現在の capacity(最大容量) よりも少ない値を指定すると、溢れた配列の後半部分は破壊されます、
*/
public void trim(int size) {
realloc(size);
}
private void realloc(int size) {
Object[] swap = new Object[items.length];
swap = items;
items = new Object[size];
int times = swap.length < size ? swap.length : size;
for (int i = 0; i < times; i++) {
items[i] = swap[i];
}
swap = null;
System.gc();
}
public void copyInto(Object[] to) {
// if (to.length < itemCount) {
// Log.p("ArrayList.copyInto() argument array 'to' is too short.");
// return;
// }
for (int i = 0; i < itemCount; i++) {
to[i] = items[i];
}
}
public void ensureCapacity(int min) {
if (size() < itemCount + min) {
realloc(itemCount + min);
}
}
/**
* @return 現在のcapacity(最大容量)を返します、実際に代入されているインスタンスの数である、要素数(size)ではありません。
*/
public int capacity() {
return items.length;
}
/**
* @return 現在の要素数を返します、実際に代入されているインスタンスの数です。capacity(最大容量)ではありません。
*/
public int size() {
return itemCount;
}
public boolean isEmpty() {
return itemCount == 0;
}
public boolean contains(Object element) {
for (int i = 0; i < items.length; i++) {
if (items[i].equals(element)) {
return true;
}
}
return false;
}
public int indexOf(Object element) {
for (int i = 0; i < items.length; i++) {
if (items[i].equals(element)) {
return i;
}
}
return -1;
}
public int indexOf(Object element, int index) {
for (int i = index; i < items.length; i++) {
if (items[i].equals(element)) {
return i;
}
}
return -1;
}
public int lastIndexOf(Object element) {
for (int i = items.length - 1; 0 <= i; i--) {
if (items[i].equals(element)) {
return i;
}
}
return -1;
}
public int lastIndexOf(Object element, int index) {
for (int i = index - 1; 0 <= i; i--) {
if (items[i].equals(element)) {
return i;
}
}
return -1;
}
public Object firstElement() {
return elementAt(0);
}
public Object lastElement() {
return elementAt(size() - 1);
}
public void setElementAt(Object element, int index) {
items[index] = element;
}
public void removeElementAt(int index) {
itemCount--;
items[index] = items[itemCount];
}
// public void getSortedArray() {}
// public void trimToSize() { realloc(itemCount);}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment