Newbee List question

Discussion in 'Java' started by Todor Atanasov, Jan 9, 2014.

  1. Hi,
    I am very new to the JAVA, but I have some experience in programming, like C++/MFC and php.

    I have a question regarding List, and more precisely a specific case which I fail to get the logic.

    This is the example code:

    List groups = new ArrayList();
    List group = null;
    for(int i = 0; i < 20; i++{
    TestClass test = new TestClass();
    if(group == null || flag)){
    group = new ArrayList();

    ...... //the flag is set on some condition


    I get the first nationalization of the group list with new ArrayList, but isn't every single group list after that, when the flag is set, also initialized, resulting in a empty group, which are then added to the groups??
    Todor Atanasov, Jan 9, 2014
    1. Advertisements

  2. Todor Atanasov

    Joerg Meier Guest

    Yes, that's what's happening. It seems you understood correctly.

    Liebe Gruesse,
    Joerg Meier, Jan 9, 2014
    1. Advertisements

  3. 09 Ñнуари 2014, четвъртък, 14:16:42 UTC+2, Todor Atanasov напиÑа:
    Thank you both for the reply, I will have to look more deeply into the logic here :D
    Todor Atanasov, Jan 9, 2014
  4. Todor Atanasov

    Tim Slattery Guest

    How do you figure? It may have been the programmer's intention to
    store the TestClass instances in the current "group", but that's not
    in the code. TestClass instances are created, then thrown away.
    Again, that may have been the intent, but I don't see it happening.
    Tim Slattery, Jan 9, 2014
  5. Todor Atanasov

    Roedy Green Guest

    you left off your imports. There are two List classes. Make sure you
    import the right ones. See
    Roedy Green, Feb 4, 2014
  6. Todor Atanasov

    Roedy Green Guest

    Why do you computer test then throw it away?
    Where is flag defined?
    group will be null only once, your groups.add will be executed only
    group should be declared inside the loop.
    Roedy Green, Feb 4, 2014
    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.