---
title: "自定义泛型的数组List"
author: "vortexchoo"
date: "2015-10-09T10:12:16Z"
tags: []
categories: ["编程语言"]
slug: "list"
draft: false
lastmod: "2015-10-09T10:12:16Z"
id: 2247745
url: "https://vortexchoo.iteye.com/blog/2247745"
---

```
public class MyArrayList<T> {
	
	private static final int DEFAULT_SIZE = 10;
	
	private Object[] elements;
	
	private int count = -1;
	
	public MyArrayList(){
		elements = new Object[DEFAULT_SIZE];
	}
	
	public int size(){
		return count+1;
	}
	
	public void add(T t){
		count++;
		if(count<elements.length){
			elements[count] = t;
		}else{
			Object[] newArray = new Object[elements.length+DEFAULT_SIZE];
			for(int i=0;i<elements.length;i++){
				Object o = elements[i];
				newArray[i] = o;
			}
			elements = newArray;
			elements[count] = t;
		}
	}
	
	@SuppressWarnings("unchecked")
	public T get(int index){
		return (T) this.elements[index];
	}

}
```