I’m trying to figure out a way to take all the elements of a vector and put them into a single string. The number of elements in the vector would vary, so .size() would be needed to get the size of the vector.
Is it as simple as this?
Vector vec1 = new Vector();
String str1 = null;
vec1.toString(str1);
First is to use a for loop that iterates through all your items in your vector…
You could also override the toString() method of each item in the vector. I think that would give you something like string1, string2, string3, string4, …
Finally, you could override the toString() method of the vector with your own vector class and do what ever you want there.
BTW, your code is incorrect… It should be…
Vector vec1 = new Vector();
String str1 = vec1.toString();
Regards,
Nate
PS: I’m not sure where the debate between ArrayList and Vector is right now, but it is typically slightly better to use ArrayList intead of Vector.
okay, I tried overriding the method, but it appears as though I didn’t do it correctly, for the method isn’t really overwridden. Am I missing something?
public class testthings {
private static Vector vec1 = new Vector();
public static void main(String[] args) {
vec1.addElement("one");
vec1.addElement("two");
vec1.addElement("three");
String str1 = vec1.toString();
System.out.println(str1);
}
public String toString() {
int n = vec1.size();
StringBuffer result = new StringBuffer();
//result.append("[");
for (int i = 0; i < n; i++) {
result.append(vec1.elementAt(i).toString());
//if (i < n-1) result.append(",");
}
//result.append("]");
return result.toString();
}
}
public class testthings extends Vector {
private static Vector vec1 = new Vector();
public static void main(String[] args) {
vec1.addElement("one");
vec1.addElement("two");
vec1.addElement("three");
String str1 = vec1.toString();
System.out.println(str1);
}
public final String toString() {
int n = vec1.size();
StringBuffer result = new StringBuffer();
//result.append("[");
for (int i = 0; i < n; i++) {
result.append(vec1.elementAt(i).toString());
//if (i < n-1) result.append(",");
}
//result.append("]");
return result.toString();
}
}
Why would a vector need to have an instance of Vector? Isn’t a Vector already a Vector? I would think so…
Here is another hint…
public class ExtendedVector extends Vector
{
public String toString()
{
StringBuffer sb = new StringBuffer(); // Like the use of the string buffer btw...
for(int i = 0; ........)
{
}
}
return sb.toString();
}
No, thats not what I’m saying at all. The name of the class really isn’t important. The behavior of that class is important though.
Since you are extending Vector you already have a Vector. The class you are working with is a Vector. So, you just need to modify the behavior of Vector’s toString() method now…
from the previous example…
public class AnyNameForAClassThatIsLegal extends Vector
{
public String toString()
{
StringBuffer sb = new StringBuffer();
for(int i = 0; i < size(); i++)
{
sb.append(get(i));
}
return sb.toString();
}
}
And that is all there is to it. Very simple huh?
Now, since this isn’t for a school assignment (I thought it was and that is why i was explaining the extending of Vector) then i get the feeling that returning the toString() of every object in the vector is going to work out for you…
So, here is another solution… Not as eligant, but it works.
DO NOT EXTEND VECTOR HERE… Just use the vector…
Vector vector = new Vector();
vector.add("one");
vector.add("two");
vector.add("three");
StringBuffer sb = new StringBuffer();
for(int i = 0; i < vector.size(); i++)
{
// You will need to do specific casting here to get the correct object so
// you can get the data you want.
String string = (String)vector.get(i);
sb.append(string);
}
System.out.println(sb.toString());
Because you are using an instance of Vector not your extended class testthings.
If you changed your code to the following:
public class testthings extends Vector {
public static void main(String[] args) {
Vector vector = new testthings(); // Just changing the class name here...
vector.add("one");
vector.add("two");
vector.add("three");
String str1 = vector.toString();
System.out.println(str1);
}
public String toString()
{
StringBuffer sb = new StringBuffer();
for(int i = 0; i < size(); i++)
{
sb.append(get(i));
}
return sb.toString();
}
}