Vector class

H

HelpMe

I have implemented a vector class.It's fine except it shows the
element at position 2 i.e. index 1 is 19 even after removing it.But
the size i am able to decrease.What should I do for that? Can anyone
suggest me?My program is:-

import java.io.*;

class VectorEmptyException extends RuntimeException {
VectorEmptyException(String message) {
super(message);
}

}

class Vector<T> {
Object[] data;
int count;

Vector() {
data = new Object[1];
count = 0;

}

Vector(int s) {
data = new Object;
count = 0;

}

int size() {return count;}

Object elementAt(int i) throws VectorEmptyException {
if(i>=count) throw new VectorEmptyException("Vector is
empty");
return data;
}

void add(int index,Object O) {
if(data.length == count) {
Object[] newdata = new Object[data.length*2];
for(int i=0;i<count;i++) {
newdata = data;
}
data = newdata;
}

for(int i=count;i>index;i--) {
data =data[i-1];
}
data[index] = O;
count++;

}
Object remove(int index) throws VectorEmptyException{
if(index >= count) throw new VectorEmptyException("Vector
is empty");
if (data.length <= count) {
Object[] newdata = new Object[data.length-1];
for(int i=0;i<index;i++)
newdata = data;
for(int i=index;i<count-1;i++)
newdata = data[i+1];
data = newdata;}
count--;
return data[index];

}
public static void main(String[] args){
Vector<Integer> v = new Vector<Integer>();
try{
v.add(0,22);v.add(1,19);v.add(2,45)
System.out.println("The 2nd element is : " +v.elementAt(1));
System.out.println("The size of the vector is : "
+v.size());

System.out.println("The removed element is : "
+v.remove(1));
System.out.println("The 2nd element is : " +v.elementAt(1));

System.out.println("The size of the vector is : "
+v.size());
}
catch(VectorEmptyException e){
System.out.println("The vector is empty");
System.out.println(e.getMessage());
}
}
}
 
P

Peter Duniho

I have implemented a vector class.It's fine except it shows the
element at position 2 i.e. index 1 is 19 even after removing it.But
the size i am able to decrease.What should I do for that? Can anyone
suggest me?My program is:-

Well, aside from a variety of other things that make the code you posted
questionable (not the least of which is the question as to why you'd
bother to implement a vector class, given that Java already has perfectly
good dynamic collections...smells like homework to me), I'd say the most
significant thing is that your "remove()" method will only do something if
the array holding your data has a length equal to or less than the number
of elements being held.

That length can never be less than, and it will only be equal to when the
capacity of your vector is exactly the same as the number of elements
being held in it. After you've added three elements, that's not the
case. So your "remove()" method doesn't do anything when you call it.

For what it's worth, you would have seen this immediately yourself had you
just bothered to step through the code in a debugger.

Pete
 
H

Hendrik Maryns

HelpMe schreef:
I have implemented a vector class.

Why? java.util.Vector and java.util.ArrayList work just fine (use the
latter if you don’t know why you should use the former).

It's fine except it shows the
element at position 2 i.e. index 1 is 19 even after removing it.But
the size i am able to decrease.What should I do for that? Can anyone
suggest me?My program is:-

import java.io.*;

class VectorEmptyException extends RuntimeException {
VectorEmptyException(String message) {
super(message);
}

}

class Vector<T> {
Object[] data;

Make that T[]
int count;

I’d simply initialize count where you declare it:
in count = 0;
Vector() {
data = new Object[1];
count = 0;

and remove these two lines with count = 0;
}

Vector(int s) {
data = new Object;
count = 0;

}

int size() {return count;}

Object elementAt(int i) throws VectorEmptyException {
if(i>=count) throw new VectorEmptyException("Vector is
empty");


You throw an exception indicating the vector is empty when someone
retrieves an element at a wrong index?
return data;
}

void add(int index,Object O) {
if(data.length == count) {
Object[] newdata = new Object[data.length*2];
for(int i=0;i<count;i++) {
newdata = data;
}


Use System.arrayCopy
data = newdata;
}

for(int i=count;i>index;i--) {
data =data[i-1];
}
data[index] = O;
count++;

}
Object remove(int index) throws VectorEmptyException{
if(index >= count) throw new VectorEmptyException("Vector
is empty");
Again?

if (data.length <= count) {


As peter pointed out, s/count/index/
Object[] newdata = new Object[data.length-1];
for(int i=0;i<index;i++)
newdata = data;
for(int i=index;i<count-1;i++)
newdata = data[i+1];
data = newdata;}


Your {} are in the wrong place. This amounts to what Peter said.
Do you want to copy the whole array each time an element is removed?
This means if you add an element after removing one, you’ll have to copy
the whole array again! (Since it won’t fit.) Just keep some null
elements at the end.
count--;
return data[index];

I think it’s bad practice to return a value from a method that changes
things. Functions (returning a value, without side effects) and
procedures (changing stuff, no return value) should be kept separated
where possible.

H.
--
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.4-svn0 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFH1PeWe+7xMGD3itQRAvA7AJ9pGscxIYEEwm2LIYWPH3DUzEvHpQCeMwFz
DthpAdDTDLiU/Mo2eHLeHRw=
=H1re
-----END PGP SIGNATURE-----
 
L

Lew

Hendrik said:
Why? java.util.Vector and java.util.ArrayList work just fine (use the
latter if you don’t know why you should use the former).

The only reason to use Vector is to support certain classes that never were
retrofitted with the Collections framework. For any original work there is a
better alternative available.
 
L

Lew

HelpMe schreef:
Hendrik said:
I’d simply initialize count where you declare it:
int count = 0;
Vector() {
data = new Object[1];
count = 0;

and remove these two lines with count = 0;

Better yet, observe that instance variables are initialized to 0 for you, and
leave the "= 0" out altogether.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top