سلام !
با وجود اینکه جاوا کلاس Vector را دارا می باشد اما خب پیاده سازی آن به روش دلخواه مفید است .
(زیاد حرف نمی زنم برنامه ببنید
/*package Matrix*/
import java.lang.Exception;
import java.lang.NullPointerException;
public class Vector<T> {
private int size=80;
protected T [] cont;
protected int low=0,high=0,pos=0;
//=================init with no parameter===============
protected void init(){
low=pos=0;
high=size;
high++;
try{
cont=(T[])new Object[high];
}catch(NullPointerException exc){
System.out.println(“nul pointer Exception”);
}
for(Integer i=low;i<high;i++)
cont[i]=null;
}
//===================init with low par.=================
protected void init(int l){
low=pos=l;
high=size;
high++;
if(low>high)
throw new NullPointerException();
try{
cont=(T[])new Object[high];
}catch(NullPointerException exc){
System.out.println(“nul pointer Exception”);
}
for(Integer i=low;i<high;i++)
cont[i]=null;
}
//===================int with two parameter===================
protected void init(int l,int h){
low=pos=l;
high=h;
high++;
if(low>high)
throw new NullPointerException();
if(high>size)
size=high;
try{
cont=(T[])new Object[high];
}catch(NullPointerException exc){
System.out.println(“nul pointer Exception”);
}
for(Integer i=low;i<high;i++)
cont[i]=null;
}
//====================add element at pos i=====================
public T addelement(T e,int p)
{
// if(p > pos || p < low)
if(p<low)
throw new ArrayIndexOutOfBoundsException(p);
// else
//{
cont[p]=e;
pos++;
//}
return e;
}
public int currentsize(){
return this.size;
}
/** Creates a new instance of Vector */
public Vector(int l,int h) {
try{
init(l,h);
}catch(NullPointerException exc){
exc.printStackTrace();
return ;
}
}
public Vector(int l){
init(l);
}
//=======================contstructor===============
public Vector(){
init();
}
//=====================constructor====================
public Vector(Vector<T> ob){
this.high=ob.high;
this.low=ob.low;
for(int i=low;i<=high;i++)
{
try{
this.cont[i]=ob.cont[i];
}catch(NullPointerException exc){
exc.printStackTrace();return ;
}
}
}
//——————low and high————————–
public int hi(){
return this.high;
}
public int lo(){
return this.low;
}
//——————return element at i pos—————————–
public T reap(int i) throws ArrayIndexOutOfBoundsException//return element at position i;
{
if(i>this.high || i<this.low)
throw new ArrayIndexOutOfBoundsException();
return cont[i];
}
public T reap(int i,T e) throws ArrayIndexOutOfBoundsException//return element at position i;
{
if(i>this.high || i<this.low)
throw new ArrayIndexOutOfBoundsException();
return cont[i];
}
public int size()
{
return this.size;
}
}
class Grater extends Exception{
public Grater(){
System.out.println(“your Domain have Error!\n The low is Grater Than High “);
}
}