how to implement an array that can pass object in...

i tried to implemented an array that could pass any type in...But i got an error when i compier my code..

it works fine if i only deal with the data type in integer only ...could any one show me how to fix it...thx ..

publicclass MyArray<T>

{

privateint maxNumberElements;//Same as a.length

private T [] a;

privateint numberUsed;//Number of indices currently in use

//set the maximum number of allowable elements to 10

MyArray()

{

maxNumberElements = 10;

//a = new T[ maxNumberElements ];

a = (T[])new Object[maxNumberElements];

numberUsed = 0;

}

//Precondition arraySize > 0

MyArray(int arraySize)

{

if(arraySize < 0)

{

System.out.println("Error Array size zero or negative.");

System.exit(0);

}

maxNumberElements = arraySize;

//a = new T[ maxNumberElements ];

a = (T[])new Object[maxNumberElements];

numberUsed = 0;

}

MyArray(MyArray original)

{

if(original ==null)

{

System.out.println("Fatal Error: aborting program.");

System.exit(0);

}

maxNumberElements = original.maxNumberElements;

numberUsed = original.numberUsed;

for(int i = 0; i < numberUsed; i++)

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

}

publicvoid add(T newElement)

{

if(numberUsed >= a.length)

{

System.out.println("Error: Adding to a full array.");

System.exit(0);

}

else

{

a[numberUsed] = newElement;

numberUsed++;

}

}

public T getElement(int index)

{

if(index < 0 || index >= numberUsed)

{

System.out.println("Error: illegal or used index.");

System.exit(0);

}

return a[index];

}

publicboolean empty()

{

return (numberUsed == 0);

}

publicboolean full()

{

return (numberUsed == maxNumberElements);

}

publicint getMaxCapacity()

{

return maxNumberElements;

}

publicint getNumberOfElements()

{

return numberUsed;

}

}

>javac MyArray.java

MyArray.java:40: incompatible types

found: java.lang.Object

required: T

a = original.a;

^

Note: MyArray.java uses unchecked or unsafe operations.

Note: Recompile with -Xlint:unchecked for details.

1 error

>Exit code: 1

Message was edited by:

Ivan1238

[4759 byte] By [Ivan1238a] at [2007-11-27 6:15:12]
# 1
Your copy constructor must specifcy T for the argument MyArray.Kaj
kajbja at 2007-7-12 17:25:28 > top of Java-index,Java Essentials,Java Programming...
# 2
thx for your suggestion...but i still not really get it..could u show me more directly? ...thx..
Ivan1238a at 2007-7-12 17:25:28 > top of Java-index,Java Essentials,Java Programming...
# 3
That error is due to the fact that the constructors parameter of type MyArray is not generified.By the way, you should definitely look into exceptions instead of just killing the JVM on any error.
dwga at 2007-7-12 17:25:29 > top of Java-index,Java Essentials,Java Programming...
# 4
yes...thx for showing me the error ...what u would suggust me to renew my construcor...thx
Ivan1238a at 2007-7-12 17:25:29 > top of Java-index,Java Essentials,Java Programming...
# 5
<spoonfeeding>MyArray(MyArray<T> original)</spoonfeeding>
dwga at 2007-7-12 17:25:29 > top of Java-index,Java Essentials,Java Programming...
# 6
thx for your suggestion..
Ivan1238a at 2007-7-12 17:25:29 > top of Java-index,Java Essentials,Java Programming...
# 7

> By the way, you should definitely look into

> exceptions instead of just killing the JVM on any

> error.

Further to that, if you anticipate even a remote possibility that an extra element might ever be added to your array, I'd question the wisdom of using an array at all

georgemca at 2007-7-12 17:25:29 > top of Java-index,Java Essentials,Java Programming...
# 8

> > By the way, you should definitely look into

> > exceptions instead of just killing the JVM on any

> > error.

>

> Further to that, if you anticipate even a remote

> possibility that an extra element might ever be added

> to your array, I'd question the wisdom of using an

> array at all

How do you think ArrayList and Vector are implemented? :p

kajbja at 2007-7-12 17:25:29 > top of Java-index,Java Essentials,Java Programming...
# 9

> > > By the way, you should definitely look into

> > > exceptions instead of just killing the JVM on

> any

> > > error.

> >

> > Further to that, if you anticipate even a remote

> > possibility that an extra element might ever be

> added

> > to your array, I'd question the wisdom of using an

> > array at all

>

> How do you think ArrayList and Vector are

> implemented? :p

Last time I looked, neither of those classes killed your JVM stone dead as soon as you tried to increase their capacity!

George (looking forward to the release of Wheel 2.0 Sudden Death edition)

georgemca at 2007-7-12 17:25:29 > top of Java-index,Java Essentials,Java Programming...
# 10

> > > > By the way, you should definitely look into

> > > > exceptions instead of just killing the JVM on

> > any

> > > > error.

> > >

> > > Further to that, if you anticipate even a remote

> > > possibility that an extra element might ever be

> > added

> > > to your array, I'd question the wisdom of using

> an

> > > array at all

> >

> > How do you think ArrayList and Vector are

> > implemented? :p

>

> Last time I looked, neither of those classes killed

> your JVM stone dead as soon as you tried to increase

> their capacity!

It was a comment on the wisdom of using an array for storage. :)

kajbja at 2007-7-12 17:25:29 > top of Java-index,Java Essentials,Java Programming...
# 11

> > > > > By the way, you should definitely look into

> > > > > exceptions instead of just killing the JVM

> on

> > > any

> > > > > error.

> > > >

> > > > Further to that, if you anticipate even a

> remote

> > > > possibility that an extra element might ever

> be

> > > added

> > > > to your array, I'd question the wisdom of

> using

> > an

> > > > array at all

> > >

> > > How do you think ArrayList and Vector are

> > > implemented? :p

> >

> > Last time I looked, neither of those classes

> killed

> > your JVM stone dead as soon as you tried to

> increase

> > their capacity!

>

> It was a comment on the wisdom of using an array for

> storage. :)

Which was addressed by the Wheel 2.0 addendum :-p

georgemca at 2007-7-12 17:25:29 > top of Java-index,Java Essentials,Java Programming...