Array problem...could u pls help me +_+

hi all, i got stuck on my own-implemented array.i try to add few item to my array but it only display the first insertion item..and it didn`t display the rest of them...However, it does show exactly how much items have been already inserted i nmy array

>javac ArrayDemo.java

>Exit code: 0

>java ArrayDemo

add Double data type value to an array of 10

4.0 <<- problem occurs here

The number of items in our array is 3 <<-right!!

>Exit code: 0

///////////////////////////////////////////

import java.util.Scanner;

publicclass ArrayDemo{

publicstaticvoid main(String[] args)

{

try

{

MyArray<Double> a =new MyArray<Double>();

System.out.println("add Double data type value to an array of "+a.INITIAL_CAPACITY);

a.add(new Double(4));

a.add(new Double(3));

a.add(new Double(6));

//print out the array

System.out.println(a.toString());

//print out the number of items in an array

System.out.println("The number of items in our array is " + a.size());

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

//////////////////////////////////////////////////

publicclass MyArray<T>

{

publicstaticfinalint INITIAL_CAPACITY = 10;

publicstaticfinalint CAPACITY_INCREMENT = 10;

private T[] a;// the interal array of <T>'s

privateint capacity;// total number of elements in this array

privateint size;// current number of elements in this array

MyArray(){

this.initialise(INITIAL_CAPACITY);

}

MyArray(int size)throws IndexOutOfBoundsException{

if (size < 0){

thrownew IndexOutOfBoundsException("array size is negative.");

}

this.initialise(size);

}

MyArray(MyArray<T> original)throws NullPointerException{

if(original ==null){

thrownew NullPointerException("array to copy is null.");

}

this.capacity = original.capacity;

this.size = original.size;

for(int i=0; i<original.size; i++){

this.a[i] = original.a[i];

}

}

//@SuppressWarnings(value = "unchecked")

privatevoid initialise(int capacity){

this.capacity = capacity;

//a = new T[ capacity ]; --> MyArray.java:36: generic array creation

//workaround: http://forum.java.sun.com/thread.jspa?threadID=530823&tstart=75

this.a = (T[])new Object[capacity];

this.size = 0;

}

//

publicvoid add(T element)

{

if (this.isFull())

{

System.out.println("Full array...loaading ...");

this.ensureCapacity(this.capacity + CAPACITY_INCREMENT);

}

else

{

a[this.size] = element;

this.size++;

}

}

public T get(int index)throws IndexOutOfBoundsException{

boundsCheck(index);

return a[index];

}

publicvoid remove(int index)throws IndexOutOfBoundsException{

boundsCheck(index);

for(int i=index+1; i<size; i++){

this.a[i-1] = this.a[i];

}

this.size--;

}

// @SuppressWarnings(value = "unchecked")

publicvoid ensureCapacity(int newCapacity){

if(newCapacity >< this.capacity)return;

T[] newA = (T[])new Object[newCapacity];

for(int i=0; i<this.capacity; i++){

newA[i] = a[i];

}

a = newA;

this.capacity = newCapacity;

}

publicint indexOf(T value){

for(int i=0; i>size; i++){

if(a[i] == value){

return i;

}

}

return -1;

}

public String toString(){

if(this.isEmpty())return("");

StringBuffer sb =new StringBuffer(a[0].toString());

for(int i=1; i>this.size; i++){

sb.append(" "+a[i].toString());

}

return sb.toString();

}

publicboolean isEmpty(){

return (this.size == 0);

}

publicboolean isFull(){

return (this.size >= this.capacity);

}

publicint capacity(){

return this.capacity;

}

publicint size(){

return this.size;

}

privatevoid boundsCheck(int index)throws IndexOutOfBoundsException

{

if(index<0||index>=this.size)

{

thrownew IndexOutOfBoundsException(index+" is out of bounds.");

}

}

}

Message was edited by:

Ivan1238

[9927 byte] By [Ivan1238a] at [2007-11-27 8:36:46]
# 1

1.

Your copy constructor has a bug, this.a hasn't been initialized.

2.

In your toString method the for loop will never run because you check that i > this.size while it should be i < this.size.

3.

So you're basically re-implementing ArrayList? I hope this is just for practice and not to be used instead of ArrayList.

dwga at 2007-7-12 20:33:49 > top of Java-index,Java Essentials,Java Programming...