Re: NEWBIE: Help getting String[] from Vector
From: david m- (utc.4eecd8c5_at_ntlworld.com)
Date: 01/04/04
- Next message: Real Gagnon: "Re: colors in JLabel"
- Previous message: Osaman: "Help : Casting after a clone & method polymorphism"
- In reply to: david m-: "Re: NEWBIE: Help getting String[] from Vector"
- Next in thread: Sudsy: "Re: NEWBIE: Help getting String[] from Vector"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 4 Jan 2004 17:41:22 -0000
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[i]);
}
}
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-" <utc.4eecd8c5@ntlworld.com> wrote in message
news:SqWJb.16739$FN.6829@newsfep4-winn.server.ntli.net...
> 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" <pignarson@hotmail.com> wrote in message
> news:a60bd78.0401040640.59f1a606@posting.google.com...
> > 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[i]);
> > }
> > }
> > }
>
>
- Next message: Real Gagnon: "Re: colors in JLabel"
- Previous message: Osaman: "Help : Casting after a clone & method polymorphism"
- In reply to: david m-: "Re: NEWBIE: Help getting String[] from Vector"
- Next in thread: Sudsy: "Re: NEWBIE: Help getting String[] from Vector"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|