NEWBIE: Help getting String[] from Vector

P

Peter

Hi, could someone help me with this silly problem I have. I am trying
to learn how to use Vectors and I want to 'flatten' them. I want my
for loop to print out

1
2.1
2.2
2.3.1
2.3.2

from the vector contents (I really want all the vectors and vectors of
vectors to be put in a String[]).

Thank you
Peter

BTW: here is the code I have that doesn't work.

package mypackage1;
import java.util.Vector;
public class Class1
{
public Class1()
{
}
public static void main (String[] args)
{
Vector v1=new Vector();
v1.add("1");

Vector v2=new Vector();
v2.add("2.1");
v2.add("2.2");

Vector v3=new Vector();
v3.add("2.3.1");
v3.add("2.3.2");

v2.add(v3);
v1.add(v2);

Object[] z=new Object[]{v1.toArray()};
for (int i=0;i<z.length;i++)
{
System.out.print("z["+String.valueOf(i)+"]=");
System.out.println(z);
}
}
}
 
D

david m-

The code you provided appears not to work.
The output I get is

z[0]=[Ljava.lang.Object;@601bb1

Which I'm guessing is be cause you instantiate the Object[] with the return
from v1.toArray();

Initial problem is the creation of z

Object[] z=new Object[]{v1.toArray()};

which I think should be

Object[] z=v1.toArray();

The output of the program is then

z[0]=1
z[1]=[2.1, 2.2, [2.3.1, 2.3.2]]

which is still probably not what you want.

AFAIK
The problem being that toArray converts a vector of 'objects' to an array of
'objects'
The vector doesn't know much about its contents you won't get a deep
flattening without further action on your part.

david m.
 
S

Sudsy

Peter said:
Hi, could someone help me with this silly problem I have. I am trying
to learn how to use Vectors and I want to 'flatten' them. I want my
for loop to print out

1
2.1
2.2
2.3.1
2.3.2

from the vector contents (I really want all the vectors and vectors of
vectors to be put in a String[]).

Thank you
Peter

BTW: here is the code I have that doesn't work.
<snip>

This is a perfect situation for recursion. The Vectors can contain
object of two different types: String or Vector. As you loop through
each Vector element you examine the class of the object. If it's a
Vector then you recurse through that element. If it's a String then
you just add it to your list of results. You also need to read the
javadocs: Vector.toArray() returns Object[]. There's no need to cast
to a new object array. Here's some working code:

import java.util.Vector;
public class Class1
{
public Class1()
{
}
public static void main (String[] args)
{
Vector v1=new Vector();
v1.add("1");

Vector v2=new Vector();
v2.add("2.1");
v2.add("2.2");

Vector v3=new Vector();
v3.add("2.3.1");
v3.add("2.3.2");

v2.add(v3);
v1.add(v2);

Vector results = new Vector(); // create vector of results
getChildren( results, v1 ); // build the list

Object[] z= results.toArray(); // note the difference here
for (int i=0;i<z.length;i++)
{
System.out.print("z["+String.valueOf(i)+"]=");
System.out.println(z);
}
}

private static void getChildren( Vector results, Object obj ) {
if( obj instanceof String )
results.add( (String) obj );
if( obj instanceof Vector ) {
Vector v = (Vector) obj;
for( int i = 0; i < v.size(); i++ )
getChildren( results, v.elementAt( i ) );
}
// we just fall through here if object is unrecognized
}
}
 
D

david m-

Some more working code.
Sorry about the formatting, notepad :-(.

david m.

//package mypackage1;
import java.util.Vector;
import java.util.Iterator;

public class Class1
{
public Class1()
{
}

public static void main (String[] args)
{
Vector v1=new Vector();
v1.add("1");

Vector v2=new Vector();
v2.add("2.1");
v2.add("2.2");

Vector v3=new Vector();
v3.add("2.3.1");
v3.add("2.3.2");

v2.add(v3);
v1.add(v2);

Object [] z = flatten(v1).toArray();
for (int i=0; i<z.length; i++)
{
System.out.print("z["+String.valueOf(i)+"]=");
System.out.println(z);
}
}

private static Vector flatten( Vector vector )
{
Vector items = new Vector();

if( vector.size() != 0 )
{
for (int i=0; i<vector.size(); i++)
{
Object object = vector.elementAt(i);

if( object instanceof Vector )
{
// It's a Vector so flatten it!
items.addAll(flatten((Vector)object));
}
else
{
// don't know how to 'flatten' any other object types!
items.add(object);
}
}
}

return (items);
}
}

david m- said:
The code you provided appears not to work.
The output I get is

z[0]=[Ljava.lang.Object;@601bb1

Which I'm guessing is be cause you instantiate the Object[] with the return
from v1.toArray();

Initial problem is the creation of z

Object[] z=new Object[]{v1.toArray()};

which I think should be

Object[] z=v1.toArray();

The output of the program is then

z[0]=1
z[1]=[2.1, 2.2, [2.3.1, 2.3.2]]

which is still probably not what you want.

AFAIK
The problem being that toArray converts a vector of 'objects' to an array of
'objects'
The vector doesn't know much about its contents you won't get a deep
flattening without further action on your part.

david m.
Peter said:
Hi, could someone help me with this silly problem I have. I am trying
to learn how to use Vectors and I want to 'flatten' them. I want my
for loop to print out

1
2.1
2.2
2.3.1
2.3.2

from the vector contents (I really want all the vectors and vectors of
vectors to be put in a String[]).

Thank you
Peter

BTW: here is the code I have that doesn't work.

package mypackage1;
import java.util.Vector;
public class Class1
{
public Class1()
{
}
public static void main (String[] args)
{
Vector v1=new Vector();
v1.add("1");

Vector v2=new Vector();
v2.add("2.1");
v2.add("2.2");

Vector v3=new Vector();
v3.add("2.3.1");
v3.add("2.3.2");

v2.add(v3);
v1.add(v2);

Object[] z=new Object[]{v1.toArray()};
for (int i=0;i<z.length;i++)
{
System.out.print("z["+String.valueOf(i)+"]=");
System.out.println(z);
}
}
}

 

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

Similar Threads

Chatbot 0
pointer to a vector 7
unchecked conversion warning. 32
Problem with vector ... 3
memory leak problem 0
memory leak problem 2
memory leak problem 0
How to implement generics with clone()? 7

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top