Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions group14/857999411/MyArrayList/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions group14/857999411/MyArrayList/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
17 changes: 17 additions & 0 deletions group14/857999411/MyArrayList/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>MyArrayList</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
11 changes: 11 additions & 0 deletions group14/857999411/MyArrayList/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
142 changes: 142 additions & 0 deletions group14/857999411/MyArrayList/src/MyArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import java.util.*;



class SimpleArrayList
{
//����Object�������飬
//��������Ԫ�ظ���
private Object [] elementData;
private int size;

//�޲ι��캯����Ĭ�����ɳ���Ϊ10������
SimpleArrayList ()
{
this(10);
}

//�������캯�����������ָ�������С
SimpleArrayList (int initialCapacity)
{
if(initialCapacity < 0)
throw new IllegalArgumentException("IllegalArgumentException : �Ƿ�����");
elementData =new Object [initialCapacity];

}

//����ָ��λ�õ�Ԫ��
public void add (int index,Object element)
{
if(index > size || index < 0)
throw new IndexOutOfBoundsException("IndexOutOfBoundsException �������±�Խ��");
ensureCapacity(size+1);
//����ָ��λ��Ԫ��ʱ
//����λ�ú������Ԫ������
System.arraycopy(elementData,index,elementData,index+1,size-index);
elementData[index] =element;
size++;
}

//�ɵ������������
public void ensureCapacity (int mincapacity)
{
int oldlen =elementData.length;
if(mincapacity > oldlen)
{
int newlen =(oldlen * 3)/2 + 1;
if(mincapacity > newlen)
newlen =mincapacity;
elementData =Arrays.copyOf(elementData,newlen);
}
}

public int size ()
{
return size;
}

//��ȡָ��λ�õ�Ԫ��
public Object get(int index)
{
rangeCheck(index);
return elementData[index];
}

//ArrayList��������null����˷��������������
public boolean remove (Object element)
{
if(element == null)
{
for(int i=0; i < size; i++)
{
if(elementData[i] == null)
fastRemove(i);
return true;
}
}else{
for(int i=0; i < size; i++)
if(element.equals(elementData[i]))
fastRemove(i);
return true;
}
return false;

}

//�������жϱ߽�Ĵ��������ҵ�ƥ��Ԫ��ʱ���ô˷�����
public void fastRemove (int index)
{
int numMoved = size-index-1;
if(numMoved > 0)
System.arraycopy(elementData,index+1,elementData,index,numMoved);
elementData[--size] = null;

}

public void clear()
{
elementData = null;
}

//�жϴ���IJ����Ƿ�Խ��
public void rangeCheck (int index)
{
if(index >=size || index < 0)
throw new IndexOutOfBoundsException("IndexOutOfBoundsException �������±�Խ��");
}

public boolean isEmpty ()
{
return size == 0;
}

}

public class MyArrayList
{
public static void main(String[] args)
{

SimpleArrayList sa =new SimpleArrayList();
sa.add(0,"java001");
sa.add(1,"java002");
sa.add(2,"java003");
sa.add(3,"java004");

//System.out.println(sa.get(1));

for(int i=0; i<sa.size(); i++)
{
System.out.print(sa.get(i));
}

System.out.println(sa.remove("java003"));

for(int i=0; i<sa.size(); i++)
{
System.out.print(sa.get(i));
}

}
}