From 6a80293d092417ee496ffcdc2678619e14d66973 Mon Sep 17 00:00:00 2001 From: Giovanni Di Grezia Date: Tue, 25 Mar 2014 01:05:59 +0100 Subject: [PATCH] ArrayIndexList completed --- arraylist/ArrayIndexList.java | 122 +++++++++++++++++----------- com/xgiovio/ArrayIndexListTest.java | 34 ++++++++ 2 files changed, 110 insertions(+), 46 deletions(-) create mode 100644 com/xgiovio/ArrayIndexListTest.java diff --git a/arraylist/ArrayIndexList.java b/arraylist/ArrayIndexList.java index 27e6400..fa55fff 100644 --- a/arraylist/ArrayIndexList.java +++ b/arraylist/ArrayIndexList.java @@ -6,59 +6,89 @@ package arraylist; * Date: 24/03/14 * Time: 14:31 */ -public class ArrayIndexList implements IndexList { + public class ArrayIndexList implements IndexList { + private E[] array; + private int capacity = 100; + private int size = 0; - public ArrayIndexList(){ - indexlist = (E[])new Object [def_capacity]; - } - - public ArrayIndexList( int in_n){ - indexlist = (E[])new Object [in_n]; - } - - - @Override - public E remove(int i) throws IndexOutOfBoundsException { - return null; - } - - @Override - public void add(int i, E e) throws IndexOutOfBoundsException { - - } - - @Override - public E set(int i, E e) throws IndexOutOfBoundsException { - return null; - } - - @Override - public E get(int i) throws IndexOutOfBoundsException { - return null; - } - - @Override - public boolean isEmpty() { - if (size() == 0){ - return true; - }else{ - return false; + public ArrayIndexList() { + array = (E[]) new Object[capacity]; } + + public ArrayIndexList(int in_size) { + array = (E[]) new Object[in_size]; + capacity = in_size; } - @Override - public int size() { - return nelements; - } + public int size() { + return size; + } + + public boolean isEmpty() { + return size() == 0; + } + + public E get(int r) throws IndexOutOfBoundsException { + checkIndex(r, size()); + return array[r]; + } + + public E set(int r, E e) throws IndexOutOfBoundsException { + checkIndex(r, size()); + E temp = array[r]; + array[r] = e; + return temp; + } + + public void add(int r, E e) throws IndexOutOfBoundsException { + checkIndex(r, size() + 1); + if (size == capacity) { + capacity *= 2; + E[] B =(E[]) new Object[capacity]; + for (int i=0; i=r; i--) + array[i+1] = array[i]; + array[r] = e; + size++; + } + + public E remove(int r) throws IndexOutOfBoundsException { + checkIndex(r, size()); + E temp = array[r]; + for (int i=r; i= n) + throw new IndexOutOfBoundsException("Illegal index: " + r); + } + @Override public String toString() { - return super.toString(); + + String to_return = ""; + to_return = to_return + "["; + + for ( int i = 0; i< size() ; i++){ + + if ( i== size() -1){ + to_return+=(get(i).toString()); + }else{ + to_return+=(get(i).toString() + ","); + } + } + to_return = to_return + "]"; + + return to_return; } - - private E[] indexlist; - private int def_capacity = 100; - private int nelements = 0; - } diff --git a/com/xgiovio/ArrayIndexListTest.java b/com/xgiovio/ArrayIndexListTest.java new file mode 100644 index 0000000..f6626bc --- /dev/null +++ b/com/xgiovio/ArrayIndexListTest.java @@ -0,0 +1,34 @@ +package com.xgiovio; + +import arraylist.ArrayIndexList; +import general_utility.test_object; +import stack.NodeStack; + +/** + * Created with xgiovio.macbookair. + * User: xgiovio + * Date: 23/03/14 + * Time: 20:37 + */ +public class ArrayIndexListTest { + + public static void main(String[] args) { + ArrayIndexList a = new ArrayIndexList(2); + + a.add(0,new test_object(1)); + a.add(1,new test_object(2)); + a.add(2,new test_object(3)); + a.add(3,new test_object(4)); + + System.out.print(a); + a.remove(2); + System.out.print(a); + + + + + + + } + +}