ArrayIndexList completed
This commit is contained in:
@@ -6,59 +6,89 @@ package arraylist;
|
||||
* Date: 24/03/14
|
||||
* Time: 14:31
|
||||
*/
|
||||
public class ArrayIndexList<E> implements IndexList<E> {
|
||||
|
||||
public class ArrayIndexList<E> implements IndexList<E> {
|
||||
private E[] array;
|
||||
private int capacity = 100;
|
||||
private int size = 0;
|
||||
|
||||
public ArrayIndexList(){
|
||||
indexlist = (E[])new Object [def_capacity];
|
||||
public ArrayIndexList() {
|
||||
array = (E[]) new Object[capacity];
|
||||
}
|
||||
|
||||
public ArrayIndexList( int in_n){
|
||||
indexlist = (E[])new Object [in_n];
|
||||
public ArrayIndexList(int in_size) {
|
||||
array = (E[]) new Object[in_size];
|
||||
capacity = in_size;
|
||||
}
|
||||
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return nelements;
|
||||
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<size; i++)
|
||||
B[i] = array[i];
|
||||
array = B;
|
||||
}
|
||||
for (int i=size-1; 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<size-1; i++)
|
||||
array[i] = array[i+1];
|
||||
size--;
|
||||
return temp;
|
||||
}
|
||||
|
||||
|
||||
protected void checkIndex(int r, int n) //
|
||||
throws IndexOutOfBoundsException { //
|
||||
if (r < 0 || r >= 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 + "]";
|
||||
|
||||
private E[] indexlist;
|
||||
private int def_capacity = 100;
|
||||
private int nelements = 0;
|
||||
|
||||
return to_return;
|
||||
}
|
||||
}
|
||||
|
||||
34
com/xgiovio/ArrayIndexListTest.java
Normal file
34
com/xgiovio/ArrayIndexListTest.java
Normal file
@@ -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<test_object> a = new ArrayIndexList<test_object>(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);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user