Can I borrow your eyes for debuging (Vector inside Vector)?

Discussion in 'Java' started by RC, Sep 27, 2005.

  1. RC

    RC Guest

    private Vector<Vector<String>> vector;
    .....


    vector = new Vector<Vector<String>>();
    int index = 0;
    while (index < 5) {
    vector.elementAt(index).addElement(new Vector<String>());
    index++;
    }

    This is the compiled error, please tell me what's wrong.

    addElement(java.lang.String) in java.util.Vector<java.lang.String>
    cannot be applied to (java.util.Vector<java.lang.String>)
    vector.elementAt(ct).addElement(new Vector<String>());

    Thank Q very much for your eyes in advance!
     
    RC, Sep 27, 2005
    #1
    1. Advertisements

  2. Hi,
    Well, "vector.elementAt(index)" returns a "Vector<String>", on this you
    cann call "addElement()", but this will of course require a String to be
    added.

    Ciao,
    Ingo

    PS: Additionaly, use ArrayList, which is not synchronized, instead of
    Vector.
     
    Ingo R. Homann, Sep 27, 2005
    #2
    1. Advertisements

  3. RC

    Roedy Green Guest

    This is beside your problem, but how does your loop ever terminate?
    Normally you use a FOR loop for that sort of thing.
     
    Roedy Green, Sep 27, 2005
    #3
  4. RC

    Roedy Green Guest

    Nesting discombobulates the human mind.

    Just what are you trying to add an element to?

    1. an inner vector to the outer vector?

    2. a string to an inner vector at a particular slot?

    Which do you want? The following does compile.

    import java.util.Vector;
    public class NestedVectors

    {

    private static Vector<Vector<String>> vector;

    /**
    * test harness
    *
    * @param args not used
    */
    public static void main ( String[] args )
    {
    vector = new Vector<Vector<String>>(5);
    for ( int i=0; i<5; i++ )
    {
    // add inner Vector to outer Vector
    vector.add( new Vector<String>() );
    // add String to inner vector
    vector.elementAt(i).addElement( "bananas" );
    }
    }
    }
     
    Roedy Green, Sep 27, 2005
    #4
  5. RC

    RC Guest

    Try to make a table(or net), one row of Vectors(or ArrayLists),
    I think both.
     
    RC, Sep 27, 2005
    #5

  6. The loop terminates when index is no longer strictly less than five.
    Since index is initialized to zero, then incremented once per loop
    iteration, the loop will end after five iterations.

    I almost never use for-loops. I get the idea of groupin' the loopin',
    but I don't like the idea of having multiple statements on a single
    line. Separating the statements onto separate lines is ugly, too:

    for (initialize();
    test();
    increment()) {
    // ...
    }
     
    Jeffrey Schwab, Sep 28, 2005
    #6
  7. As Roedy was hinting, you are confused about what {type of) element
    you're adding to which Vector. Here is an equivalent piece of (still
    buggy) code:

    private Vector<Vector<String>> vector;

    // ...

    vector = new Vector<Vector<String>>();

    for (int index = 0; index < 5; index++) {
    Vector<String> element = vector.elementAt(index);

    element.addElement(new Vector<String>());
    }


    If you don't recognize the error in that then see what the compiler has
    to say about it (much the same as before, actually, but note which line
    is flagged).
     
    John C. Bollinger, Sep 28, 2005
    #7
  8. RC

    Roedy Green Guest

    Roedy Green, Sep 28, 2005
    #8
  9. RC

    Jeff Schwab Guest

    Jeff Schwab, Sep 28, 2005
    #9
  10. Whoops, you didn't say obscure, you said "obfuscated." Still: Wow.
    Anyway, I reserve the right to avoid constructs I find messy, including
    most for loops.
     
    Jeffrey Schwab, Sep 28, 2005
    #10
  11. RC

    Roedy Green Guest

    Yes, when used for counted loops. They are normally used for loops
    that do not stop on a count. There is a standard idiom for doing
    something N times with an index running 0..n-1 and it is not an while
    loop. Doing that sort of stunt is inconsiderate of those who have to
    read or maintain your code.

    Unlike English, in code, you want to be trite.

    There was a similar weird loop posted today.

    for ( int n=0, n<5001; n++ )

    n is traditionally the count. i the index. Violating that convention
    trips someone up reading the code.

    I guess this bugs me more than it does others because of my Fortran
    and Forth background where the convention is rigidly followed.
     
    Roedy Green, Sep 28, 2005
    #11
  12. RC

    Roedy Green Guest

    You have that right in your own code that you don't broadcast. But
    you most certainly don't have that right if you ever work on a team.
    You will get fired quickly if you persist after a warning.

    If you post such code I also have the right to throw tomatoes at it
    attempting to dissuade others from following your example.

    Have a look at point 34 in http://mindprod.com/unmainobfuscation.html
     
    Roedy Green, Sep 28, 2005
    #12
  13. RC

    Jeff Schwab Guest

    A while loop isn't a "stunt," it's a code construct. Stunts typically
    involve hoops of fire, or professional drivers on closed courses. :)

    Anyway, I get that you like to use for-loops to count. Good for you. I
    don't. By expressing an opinion, I did not mean to criticize yours.
    Move on.
    "Trite?" ITYM "terse." And by "you want to be," ITYM "I want you to be."
    I do typically follow that convention, but not always. It doesn't get
    my goat, but I understand your point of view.
    I didn't realize the Forth community had quite agreed on the syntax of
    the language, much less established rigid code conventions. My
    impression was that many Forth programmers began each program by
    implementing a compiler. Not to put down the language or community at
    all; Forth is something I really would like to learn if I ever find the
    time.
     
    Jeff Schwab, Sep 28, 2005
    #13
  14. RC

    Jeff Schwab Guest

    Really? Maybe you should call my boss, and make sure he realizes he
    should have fired me years ago. Tell my teammates to get outraged, too.

    No thanks. I've already wasted enough time following your "mindprod"
    links. Not that they're all bad, but using them to answer usenet posts
    all the time is annoying, like someone who insists on posting in HTML.
     
    Jeff Schwab, Sep 28, 2005
    #14
  15. RC

    E11 Guest

    I agree with Jeff on his last point. While i think its good site, it
    does *seem* a tad like you are using every opportunity to direct
    traffic there, and it can be annoying for people like me with a slow
    connection.

    Anyhow, regarding the use of a while-loop where a for-loop would do, i
    do not see a problem with the while-loop UNLESS the body of the
    while-loop is long and the variable increment statement can't be easily
    seen.

    while (index < 5) {
    vector.elementAt(index).addElement(new Vector<String>());
    index++;
    }

    This seems fine to me. (The use of the while-loop that is.) The
    "index++" seems obvious enough, and it is quite easy to see that the
    loop is not endless and will terminate after 5 iterations.
     
    E11, Sep 28, 2005
    #15
  16. Todd de Gruyl, Sep 28, 2005
    #16
  17. RC

    Roedy Green Guest

    What I mean is you must write code in a stereotypical way. You want to
    "blend" . You write code the way everyone else does. Coding is not
    novel-writing. People don't want to linger over your code and enjoy
    the nuance. They want to be able to rapidly eyeball it and grasp the
    meaning quickly.

    If you avoid the common idioms you slow people down.

    Most coding is not a solitary activity. You have to get along with
    fellow team members which means everyone coding in a similar style.
    Wherever you work the standards imposed will be slightly different.
    The exact rules are much less important than the fact everyone agrees
    to follow them. Ditto code indentation and spacing.

    Further, even if you code alone, still likely others will eventually
    see your code. You should not get into idiosyncratic habits.

    All you do is make it harder for others to understand your code.

    It is not a matter of your way vs my way. It is a matter of following
    conventions vs you playing artiste.

    I know being conventional feels sinful, especially to the young, but
    there are times when it is appropriate. Think of it as form of
    consideration for others rather than a damper on your creativity. If
    you want to be creative, express it elsewhere than making pointless
    variations on common idioms.

    I am not going to leave this alone. I have been coding myself 42
    years. I have made those exact same mistakes and had people trip over
    my fancy-pants code. I know how being coerced rankles, but writing in
    a standard way really does matter.

    Look at Sun's code. You will not see any of their programmers avoiding
    common idioms. That is part of what design patterns is all about,
    making code more boring.
     
    Roedy Green, Sep 28, 2005
    #17
  18. RC

    Roedy Green Guest

    That is illogical. It takes about the same bandwidth to get material
    from my site as from a newsgroup. My way cuts down your total
    bandwidth since you don't have to look at what is not a pressing
    concern to you. You can just mentally file it away for future
    reference or ignore it.

    I try to give a short answer and a link. If the question comes up
    frequently I dispense with the short answer and just give the link.

    The advantages of the links are:

    1. I can properly format my answers.

    2. I can use tables, colour, bold , fonts etc to enhance my answer.

    3. I have previously answered this question and done a more thorough
    job of answering it on the website than I could come up with off the
    top of my head.

    4. You might get the hint that most questions are already answered
    there if you just looked up the right keyword.

    5. I can go on at great length on my website answering the question
    without wasting bandwidth of people who are not interested in that
    level of detail.

    6. The website is not a commercial website in the ordinary sense.. All
    it does it pay the ISP bill. It does not even cover my Internet
    access. It exists as a favour to people like you, whether they see it
    that way or not. People ask a question. I point them to the answer. I
    am not extracting any money out of them when they visit the website.
    Even if you download my utilities, all come with source. Paying for
    the few that are shareware is purely optional.

    7. The website is full of hyperlinks to hyperlinks to help you find
    related information, including the relevant Sun docs.

    8. I have already answered many of the questions. I have been doing
    this since the days of Java 1.0. Why should I have to compose a novel
    response to the same old questions?

    9. My glossary entries have been previously viewed by many people and
    are more likely been through the mill and corrected and augmented and
    completed than something I type off the top of my head.

    Perhaps what is bugging you is that you are having to cut and paste
    the link to a browser to view it and it takes a long time for the page
    to come up. You resent that waste of your time. Fair enough. You can
    download a copy of the website and keep it up to date using the
    Replicator. Then you have instant access. No charge. See
    http://mindprod.com/webstarts/replicator.html

    Also you might look into a different newsreader that just lets you
    click a link to view it. see
    http://mindprod.com/jgloss/newsreader.html
     
    Roedy Green, Sep 28, 2005
    #18
  19. RC

    Roedy Green Guest

    Eventually you will come around, but apparently that day is a few
    years off. Non-standard code bites the author too eventually.
     
    Roedy Green, Sep 28, 2005
    #19
  20. RC

    Roedy Green Guest

    I have found there are basically three kinds of places to work.

    1. loosey goosey places where the boss does not care about code
    quality at all, just deadlines and lines of code produced. You end up
    fighting with him over wanting to do things properly.

    2. places run by genius tyrants. They want to control your every
    move. You fight with him over leaving you alone in things that are
    none of his business, e.g. which IDE you use or your keyboard layout.

    3. bureaucracies. See Dilbert.

    Though (2) are frustrating at the time, you do learn a lot being
    forced to do things new ways, and of course you can feel proud of the
    product.

    One genius type I worked for had the rule "blend". He said "I don't
    want to be able to tell who wrote a given piece of code just by
    looking at the code itself". He had many rules to ensure that was so.
    He and I disagreed on the amount of commentary necessary but the nice
    thing about the code was everything was beautifully consistent across
    all manner of apps. He could put an team member to work on any app
    and they were very quickly at home.
     
    Roedy Green, Sep 28, 2005
    #20
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.