Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

To put the result of a Vector in a string???

Status
Not open for further replies.

javaken

Technical User
May 15, 2004
20
BE
Is this possible?

Thanks
 
Suppose the result of your vector gives you 2 rows .... Is it possible to make 1 string of this result, also if you have 3 rows or more ....

I work with a linked list, collection ...

Result: one column in my case has 2 or more rows ...
1
2

I want to make of this a string = "1 2 " or "1 2 3 .... "

Solution?

Thanks
 
Sure - here's an example - you can do this for just about any object. You weren't specific about what was in your vector.

StringBuffer strBuf = new StringBuffer();
for (int i=0; i < vector.size(); i++) {
strBuf.append((object)vector.elementAt(i));
}
String newString = strBuf.toString();

Let me know if this solution doesn't work for you and post more information about what's in your vector.
 
a piece of my code ...

XManager is a class which me return a collection

Another class use this ....:
Collection entries = XManager.INSTANCE.findX(ThingsFrom);
Iterator iter = entries.iterator();
while (iter.hasNext()){
object = (Object)iter.next();
Vector row = new Vector();
row.add("" + object.getId());
Enumeration items = row.elements();
while (items.hasMoreElements())
//////////TEST/////////
System.out.println(items.nextElement());
////////////////////////
}
The result of this is (column getId)
1
2

How to make of this 1 string instead of 2 rows to make a nice output....

Thanks ....
 
Code:
Foo foo;
Iterator iter = entries.iterator();
String row = "";
while (iter.hasNext())
{
	foo = (Foo) iter.next ();
	row = row + ("" + foo.getId () + " ");
}
row = row.trim ();
System.out.println (row);
a) You create a new, empty Vector on each iter.hasNext ().
b) System.out.println (...) prints a newline after each output - consider using System.out.print (...) instead.
c) Object has no method getId. I used 'Foo foo' - replace with your type.

seeking a job as java-programmer in Berlin:
 

Java uses an internal toString() method to print like you did via: System.out.println(items.nextElement());

Here's your code with changes I added to create and print the string.

Collection entries = XManager.INSTANCE.findX(ThingsFrom);
Iterator iter = entries.iterator();
String buf = "";
while (iter.hasNext()){
object = (Object)iter.next();
Vector row = new Vector();
row.add("" + object.getId());
Enumeration items = row.elements();
while (items.hasMoreElements())
buf += items.nextElement() + " "
}
System.out.println("The buffer is: " + buf.trim());
 
...is nearly the same, not working, not compiling humbug like javaken posted - but you saw my remarks!

Object still has no 'getId ()' method.
The Vector is needless, since you always add one element - which you remove later -
Code:
while (iter.hasNext ())
{
	foo = (Foo) iter.next ();
	buf += foo.getId () + " ";
}
would do it.

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top