Convert vector to string?

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);

Or is it something else?

Well, there are several ways…

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.

So that would work? All the vector elements storing strings would be put into str1, correct?

[edit]:
Ok, I tried this with a test file:

public class testthings {

    public static void main(String[] args) {
        Vector vec1 = new Vector();
        vec1.addElement("one");
        vec1.addElement("two");
        vec1.addElement("three");

        String str1 = vec1.toString();
        System.out.println(str1);
    }
}

And the output was this:
[one, two, three]

Is there a way to get rid of the two brackets and all the commas and spaces?

I want to get this as output:
onetwothree

Is there a way to do that?

You changed your post while i was typing my response…

:slight_smile:

So, take a look at my first reply to your post.

The three sugestions above are the main different ways to change the string representation of your vector.

If this is for a school assignment…I would recommend extending vector with your own vector class.

Regards,

Nate

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();
    }
}

I don’t think you understand extending and overriding…

You didn’t do any overriding here on the Vector’s toString() method. You overrode the toString() method of Object.

What class does testthings extend? Its Object, not Vector.

You need to extend Vector and then override the toString() of Vector…

Regards,

Nate

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();
    }
}

I still think I’m missing something…

I think so…

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();
}

BTW, is this for a school assignment?

Regards,

Nate

Nope, it’s not for an assignment.

So, you’re saying the class has to be named ExtendedVector?

[edit]: agh…this is getting frustrating. I’m going to be mad at myself if it’s someting simple that I’m missing.

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());


Regards,

Nate

Hmm…I still can’t get the overriding to work correctly

The other way you provided worked, however.

I’m still confused as to why the override doesn’t work.

public class testthings extends Vector {
    public static void main(String[] args) {
    Vector vector = new Vector();
    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();
    }
}

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();
    }
}

It would work. (or should work…)

Best of Luck,

Nate

Ah-ha! That did the trick.

I was missing this: Vector vector = new testthings();

I wasn’t aware a new object had to be created.

Gald it worked.

Cheers,

Nate

Thanks for all your help :slight_smile: