package myDataStructures;
//import javax.swing.text.html.HTMLDocument;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Random;
//import java.util.TreeMap;
public class MyArrayList<T> implements Iterable<T> {
private int theSize;
private T[] theArray;
private final int initialLength=10;
public MyArrayList(){
doClear();
}
public MyArrayList(int capacity){
doClear(capacity);
}
public boolean clear(){
doClear();
return theSize==0;
}
// public
private void doClear(){
this.theSize=0;
extendCapacity(this.initialLength);
}
private void doClear(int capacity){
this.theSize=0;
extendCapacity(capacity);
}
private void extendCapacity(int capacity){
if (capacity< theSize){
return;
}
else {
T[] theOld=theArray;
// @SuppressWarnings("Unchecked")
theArray=(T[]) new Object[capacity];
for (int i=0; i<theSize; i++){
theArray[i]=theOld[i];
}
}
}
public int getTheSize(){
return theSize;
}
public boolean isEmpty(){
return theSize== 0;
}
public T get(int index){
if (index<0 || index> theSize){
throw new ArrayIndexOutOfBoundsException(index); //抛出异常
}
else {
return theArray[index];
}
}
public reTurnDouble<T,Boolean> set(T input){
return set(theSize-1,input);
}
public reTurnDouble<T,Boolean> set(int index,T input){
if (index<0 || index>=theSize){
throw new ArrayIndexOutOfBoundsException(index);
}
else {
T old=theArray[index];
theArray[index]=input;// set
return new reTurnDouble<>(old, true);
}
}
public void add(T input){
add(theSize,input);
}
public void add(int index,T input){
if (index<0 || index>theSize){
throw new ArrayIndexOutOfBoundsException(index);
}
else {
if (theArray.length==theSize){
extendCapacity(2*theSize+1);
}
for (int i=index;i<theSize;i++){
theArray[i+1]=theArray[i];
}
theArray[index]=input;
++theSize;
}
}
public void remove(){
remove(theSize-1);
}
public void remove(int index){
if (index<0 || index>=theSize){
throw new ArrayIndexOutOfBoundsException(index);
}
else {
if (theSize<=(int)((theArray.length-1)/2)){
extendCapacity((int)((theArray.length-1)/2));
}
for (int i=index;i<theSize-1;i++) {
theArray[i] = theArray[i + 1];
}
}
}
public String myToString(){
T[] return1=(T[]) new Object[theSize];
for (int i=0;i<theSize;i++){
return1[i]=theArray[i];
}
return Arrays.toString(return1);
}
@Override
public Iterator<T> iterator(){
return new Xiangqian();
}
class Xiangqian implements Iterator<T>{
private int num;
private Xiangqian(){
num=-1;
}
public Xiangqian(int startIndex){
num=startIndex;
}
@Override
public boolean hasNext(){
if (num<theSize-1){
return true;
}
else {
return false;
}
}
@Override
public T next(){
if (hasNext()){
return theArray[++num];
}
throw new ArrayIndexOutOfBoundsException();
}
@Override
public void remove() {
System.out.println(next());
MyArrayList.this.remove(num--);
}
}
class reTurnDouble<A,B> {
A a;
B b;
protected reTurnDouble(A aInput,B bInput){
this.a=aInput;
this.b=bInput;
}
}
}
class testMyArrayList{
public static void main(String[] args){
MyArrayList<Integer> list=new MyArrayList<>(10);
Random random=new Random();
for (int i=0;i<10;i++){
try {
list.add(random.nextInt(100)-50);
}catch (Exception e) {
e.printStackTrace();
}finally {
System.out.printf(String.format("%d is over\n",i));
}
}
System.out.println(list.myToString());
for (int i=0;i<list.getTheSize();i++){
System.out.println(list.get(i));
}
for (Integer x:list){ //引用了Iterable 所以可以用foreach了
System.out.println(x);
}
Iterator<Integer> ite=list.iterator();
while (ite.hasNext()){
System.out.printf("now is %s,num is %d\n",String.valueOf(ite.hasNext()),ite.next());
}
}
}
java实现Arraylist
最新推荐文章于 2022-08-23 21:09:31 发布