21.ArrayList内部类迭代器 + java 反射

package com.example.test6;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Iterator;

/**
 * 
 * 使用内部类的方式来实现ArrayList迭代器
 * 
 * 1. ArrayList的构造函数应该是 array = (T[]) new Object[initCapacity]
 *                       而不是 array = new T[initCapacity[];
 *                       
 * 2. 当删除ArrayList中数组元素的之后,将数组整体往前移动过程中,
 *    记得将数组size-1位置处引用值为空,帮助GC尽早释放资源
 * 
 */

class ArrayList implements Iterable{
	
	private T[] array;
	private int size;

	public ArrayList() {	
		this(10);
	}
	
	public ArrayList(int initCapacity){
		array = (T[]) new Object[initCapacity];
		size = 0;
	}
	
	public void add(T value){
		if (size >= array.length){
			array = Arrays.copyOf(array, array.length * 2);
		}
		array[size++] = value;
	}
	
	public void del(T value){
		for (int i = 0; i < size; i++){
			if (array[i].equals(value)){
				System.arraycopy(array, i+1, array, i, size-i-1);
				size--;
				array[size] = null;
			}
		}
	}
	
	public T get(int index){
		if (index < 0 || index >= size){
			throw new IndexOutOfBoundsException();
		}
		
		return array[index];
	}
	
	public String toString(){
		StringBuilder s = new StringBuilder();
		s.append("[");
		
		for (int i = 0; i < size; i++){
			s.append(array[i] + " ");
		}
		s.append("]");
		
		return s.toString();
	}



	@Override
	public Iterator iterator() {
		// TODO Auto-generated method stub
		return new MyIterator();
	}
	
	class MyIterator implements Iterator{
		
		private int index;
		
		public MyIterator(){
			index = 0;
		}

		@Override
		public boolean hasNext() {
			// TODO Auto-generated method stub
			return index < ArrayList.this.size;
		}

		@Override
		public T next() {
			// TODO Auto-generated method stub
			return ArrayList.this.array[index++];
		}

		/**
		 * 
		 * 删除当前index下标处的元素
		 */
		@Override
		public void remove() {
			// TODO Auto-generated method stub
			if (index >= size){
				return;
			}
			
			System.arraycopy(array, index+1, array, index, size-index-1);
			size--;
			array[size] = null;
		}

	}
	
}

public class TestDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		ArrayList list = new ArrayList();
		
		for (int i = 0; i < 15; i++){
			list.add(i);
		}
		
		System.out.println(list);
		
		Iterator it = list.iterator();
		while (it.hasNext()){
			it.remove();
		}
		
		System.out.println(list);
		
		try {
			Class c = Class.forName("com.example.test6.ArrayList");
			
			Constructor[] cons = c.getConstructors();
			for (Constructor con : cons){
				System.out.println(con);
			}
			
			//通过反射调用构造函数生成类对象
			
			//调用默认构造函数生成类对象
			ArrayList list1 = (ArrayList)c.newInstance();
			for (int i = 0; i < 10; i++){
				list1.add(i);
			}
			System.out.println(list1);
			
			//通过调用带参数的构造函数生成类对象
			Constructor con = c.getConstructor(int.class);
			ArrayList list2 = (ArrayList) con.newInstance(10);
			for (int i = 0; i < 10; i++){
				list2.add(i);
			}
			System.out.println(list2);
			
			//////////////////////////////////////////////
			
			//通过反射调用类的成员变量
			Field[] fields = c.getDeclaredFields();
			for (Field field : fields){
				System.out.println(field);
			}
			
			Field field = c.getDeclaredField("size");
			field.setAccessible(true);
			field.setInt(list2, 5);
			System.out.println(list2);
			
			///////////////////////////////////////////////
			
			//通过反射调用类的成员方法
			
			Method[] methods = c.getDeclaredMethods();
			for (Method method : methods){
				System.out.println(method);
			}
			
			Method method = c.getMethod("get", int.class);
			Integer value = (Integer) method.invoke(list2, 3);
			System.out.println(value);
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

你可能感兴趣的:(java,Iterable,Iterator,反射,reflect)