1、java数组表方法的实现Java中数组表类的各个方法:public class MyArrayList private int theSize; private Object theItems; public MyArrayList() clear(); public void clear() theSize = 0; ensureCapacity(10); public void ensureCapacity(int newCapacity) if (newCapacity theSize) return; Object old = theItems; theItems = (Object)
2、 new ObjectnewCapacity; for (int i = 0; i size(); i+) theItemsi = oldi; public int size() return theSize; public boolean isEmpty() if (size() = 0) System.out.println(The list is empty); else System.out.println(The list is not empty); return size() = 0; public Object get(int idx) if(idx size() throw
3、new ArrayIndexOutOfBoundsException(); return theItemsidx; public Object set(int idx, Object newvalue) if(idx size() throw new ArrayIndexOutOfBoundsException(); Object old = theItemsidx; theItemsidx =newvalue; return old; public boolean add(Object x) add(size(),x); return true; public void add(int id
4、x, Object x) if(theItems.length = size() ensureCapacity(size()*2 + 1); for (int i = theSize; i idx; i-) theItemsi = theItemsi-1; theItemsidx = x; theSize+; public Object addAraay(int idx, Object x) if(theItems.length = size() ensureCapacity(size() + x.length); for (int i = idx + x.length; i size() +
5、 x.length; i+) Object old=theItems; theItemsi = oldi-x.length; /System.arraycopy(theItems, idx, theItems, idx + x.length, x.length); for(int i = idx; i idx + x.length; i+) theItemsi = xi-idx; theSize=theSize+x.length; return theItems; public Object remove(int idx) if (idx size() throw new ArrayIndex
6、OutOfBoundsException(); Object old = theItemsidx; for(int i = idx; i size()-1; i+) theItemsi = theItemsi+1; theSize-; return old; public int indexOf(Object x) if(x = null) for (int i = 0; i size(); i+) if(theItemsi = x) return i; else for (int i = 0; i = 0) System.out.println(true); else System.out.
7、println(false); return indexOf(x) = 0; public void showList() System.out.print(List: ); for(int i = 0; i size(); i+) if(i size() - 1) System.out.print(theItemsi + -); else System.out.print(theItemsi); System.out.println(); public void showList(Object x) System.out.print(List: ); for (int i = 0; i si
8、ze(); i+) if (i size() - 1) System.out.print(xi + -); else System.out.print(xi); System.out.println(); public void turnList() Object oldList = theItems; theItems = (Object) new Objectsize(); System.out.println(The old list is : ); /showList(); for(int i = 0; i size(); i+) theItemsi = oldListsize() -
9、1 - i; System.out.println(The overturn list is : + theItemsi + ); System.out.println(); public void listNext(int idx) Object old = null; if (idx size() throw new ArrayIndexOutOfBoundsException(); else if (idx size() - 1) old = theItemsidx + 1; System.out.println(The + theItemsidx + s next note is +
10、old); else System.out.println(The + theItemsidx + dont has next note); public boolean hasNext(int idx) if (idx size() throw new ArrayIndexOutOfBoundsException(); else if (idx size() - 1) System.out.println(true); else System.out.println(false); return idx size () -1; 测试程序:public class MyArrayListTes
11、t static Object intE = 1,2,3,4; public static void main(String args) MyArrayList items = new MyArrayList(); items.ensureCapacity(10); /System.out.println(MyArrayList) items).size(); /items.add(0,intE0); /System.out.println(MyArrayList) items).size(); /System.out.println(MyArrayList) items).get(0); i
12、tems.addAraay(0, intE); /System.out.println(MyArrayList) items).size(); /System.out.println(MyArrayList) items).get(1); /System.out.println(MyArrayList) items).set(4,5); /System.out.println(MyArrayList) items).get(4); items.remove(3); System.out.println(MyArrayList) items).size(); System.out.println
13、(MyArrayList) items).get(3); items.contains(3); items.contains(5); items.showList(); items.add(7); System.out.println(MyArrayList) items).size(); items.add(3, 10); items.showList(); items.add(2, a); items.showList(); items.turnList(); items.showList(); items.listNext(5); items.listNext(4); items.hasNext(5);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1