标签:style blog class c code java
本次内容:arraylist()
1、
1 package list; 2 3 import java.util.ArrayList; 4 import java.util.Date; 5 import java.util.List; 6 7 /** 8 * 测试list的基本方法 9 * @author acer 10 * 11 */ 12 public class arraylist { 13 public static void main(String[] args) 14 { 15 List list=new ArrayList(); 16 //Arraylist():低层实现是数组,查找容易,插入删除复杂。线程不安全 17 //LinkedList():低层实现是链表,查找复杂,插入删除容易。线程不安全 18 //Vector():低层实现是数组,线程安全 19 list.add("aaa"); 20 list.add(new Date()); 21 list.add(1234);//包装类:自动装箱 22 System.out.println(list.size()); 23 list.remove(0); 24 System.out.println(list.size()); 25 list.get(0); 26 System.out.println(list.get(0)); 27 list.set(0, "bbb"); 28 System.out.println(list.get(0)); 29 System.out.println(list.get(1)); 30 } 31 }
运行结果:
3
2
Tue May 20 22:18:58 CST 2014
bbb
1234
2、自己定义arraylist()函数
1 package list; 2 3 import java.util.Date; 4 5 /** 6 * 自己定义arraylist,熟悉其低层实现 7 * @author acer 8 * 9 */ 10 public class myarraylist { 11 private Object[] elementData; 12 private int size; 13 public int size()//数组的大小 14 { 15 return size; 16 } 17 public myarraylist(int initalCapacity)//构造器 18 { 19 elementData=new Object[initalCapacity]; 20 } 21 public Object get(int index)//取数组的值 22 { 23 rangeCheck(index); 24 return elementData[index]; 25 } 26 public void add(Object object)//添加对象 27 { 28 ensureCapacity(); 29 elementData[size++]=object; 30 } 31 public void add(int index,Object obj)//指定位置添加对象 32 { 33 rangeCheck(index); 34 ensureCapacity(); 35 int numMoved=size-index; 36 System.arraycopy(elementData, index, elementData, index+1, numMoved); 37 elementData[index]=obj; 38 size++; 39 40 } 41 private void ensureCapacity()//数组的扩容 42 { 43 if(size==elementData.length) 44 { 45 Object[] newarraylist=new Object[size*2+1]; 46 System.arraycopy(elementData, 0,newarraylist,0,elementData.length); 47 elementData=newarraylist; 48 } 49 } 50 public void remove(int index)//移除对象 51 { 52 rangeCheck(index); 53 int numMoved=size-index-1; 54 if(numMoved>0) 55 { 56 System.arraycopy(elementData, index+1, elementData, index, numMoved); 57 } 58 elementData[--size]=null; 59 } 60 public void remove(Object obj)//移除对象 61 { 62 for(int i=0;i<size;i++) 63 { 64 if(get(i).equals(obj)) 65 { 66 remove(i); 67 } 68 } 69 } 70 public boolean isEmpty()//判是否为空 71 { 72 return size==0; 73 } 74 private void rangeCheck(int index)//检测检索值是否越界 75 { 76 if(index<0||index>=size) 77 { 78 try { 79 throw new Exception(); 80 } catch (Exception e) { 81 e.printStackTrace(); 82 } 83 } 84 } 85 public static void main(String[] args) 86 { 87 myarraylist list=new myarraylist(3); 88 list.add("aaa"); 89 list.add("bbb"); 90 list.add(new Date()); 91 System.out.println(list.isEmpty()); 92 System.out.println(list.size()); 93 System.out.println(list.get(0)); 94 System.out.println(list.get(1)); 95 System.out.println(list.get(2)); 96 list.remove(0); 97 list.remove(new Date()); 98 System.out.println(list.get(0)); 99 list.add(1, "aaa"); 100 System.out.println(list.get(1)); 101 } 102 }
运行结果:
false
3
aaa
bbb
Tue May 20 23:41:15 CST 2014
bbb
aaa
java开始到熟悉100-102,布布扣,bubuko.com
标签:style blog class c code java
原文地址:http://www.cnblogs.com/xiaojingang/p/3740171.html