Re: NEWBIE: Help getting String[] from Vector

From: david m- (utc.4eecd8c5_at_ntlworld.com)
Date: 01/04/04


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



Relevant Pages

  • Re: pool configuration directive on Windows
    ... David L. Mills wrote: ... The specific accusation directed to me personally was about the notrust ... I already have working code for this. ...
    (comp.protocols.time.ntp)
  • Re: Reading BLOB data from SQL using a datareader
    ... Perhaps it will also have some relevance to what you are doing. ... David ... > I have some working code that reads BLOBs from an ... > is fine for small BLOBs, but if they are large I want to ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: WinFixer 2005
    ... open the notepad, and just save those two Hkey strings in the notepad, then ... "David H. Lipman" wrote: ... > | A filerequires a reboot to complete the repair. ...
    (microsoft.public.security.virus)
  • Re: Getting rid of NoteAs a matter of fact yespad
    ... Are you r-clicking .txt file and then picking change program, then checking the box for "Always use this program...." ... David wrote: ... HTML Editor is listed as Notepad with a drop down list which has no other options. ... The reason that I want to kill this particular "wife" is that I cannot get XP to associate .TXT files with Notepad+. ...
    (microsoft.public.windowsxp.general)
  • Re: Text files still wont open with Notepad - fixed
    ... > Alright!! ... David, we fixed it. ... > (which should open a window with notepad selected - is it there?) ... > and there was a notpad.exe with the proper notepad icon ...
    (microsoft.public.windowsxp.basics)

Loading