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();
    groups.add(group);
    }


    ...... //the flag is set on some condition
    group.add(test);

    }

    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
    1. Advertising

  2. Todor Atanasov

    Joerg Meier Guest

    On Thu, 9 Jan 2014 04:16:42 -0800 (PST), Todor Atanasov wrote:

    > 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??


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

    Liebe Gruesse,
    Joerg

    --
    Ich lese meine Emails nicht, replies to Email bleiben also leider
    ungelesen.
     
    Joerg Meier, Jan 9, 2014
    #2
    1. Advertising

  3. 09 Ñнуари 2014, четвъртък, 14:16:42 UTC+2, Todor Atanasov напиÑа:
    > 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();
    >
    > groups.add(group);
    >
    > }
    >
    >
    >
    >
    >
    > ...... //the flag is set on some condition
    >
    > group.add(test);
    >
    >
    >
    > }
    >
    >
    >
    > I get the first nationalization of the group list with new ArrayList, butisn'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??


    Thank you both for the reply, I will have to look more deeply into the logic here :D
     
    Todor Atanasov, Jan 9, 2014
    #3
  4. Todor Atanasov

    Tim Slattery Guest

    (Leif Roar Moldskred) wrote:


    >What this code does is to create 20 instances of TestClass(), _batch
    >them together_ according to some condition, and then store the batches
    >in the list /groups/.


    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.

    >Setting the flag indicates that the next turn through the loop is
    >the start of a new batch.


    Again, that may have been the intent, but I don't see it happening.

    --
    Tim Slattery
    tim <at> risingdove <dot> com
     
    Tim Slattery, Jan 9, 2014
    #4
  5. Todor Atanasov

    Roedy Green Guest

    On Thu, 9 Jan 2014 04:16:42 -0800 (PST), Todor Atanasov
    <> wrote, quoted or indirectly quoted someone who
    said :

    >List groups = new ArrayList();


    you left off your imports. There are two List classes. Make sure you
    import the right ones. See http://mindprod.com/jgloss/list.html
    --
    Roedy Green Canadian Mind Products http://mindprod.com
    Because a thing seems difficult for you, do not think it
    impossible for anyone to accomplish.
    ~ Marcus Aurelius 121-04-26 180-03-17
     
    Roedy Green, Feb 4, 2014
    #5
  6. Todor Atanasov

    Roedy Green Guest

    On Thu, 9 Jan 2014 04:16:42 -0800 (PST), Todor Atanasov
    <> wrote, quoted or indirectly quoted someone who
    said :

    >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();
    > groups.add(group);
    > }


    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
    once.
    group should be declared inside the loop.
    --
    Roedy Green Canadian Mind Products http://mindprod.com
    Because a thing seems difficult for you, do not think it
    impossible for anyone to accomplish.
    ~ Marcus Aurelius 121-04-26 180-03-17
     
    Roedy Green, Feb 4, 2014
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vbGansta
    Replies:
    0
    Views:
    303
    vbGansta
    Feb 16, 2004
  2. Valmir
    Replies:
    3
    Views:
    4,959
    xiko tripa
    Feb 18, 2004
  3. Who

    Newbee question

    Who, Apr 5, 2004, in forum: ASP .Net
    Replies:
    8
    Views:
    480
    Kevin Spencer
    Apr 5, 2004
  4. mbs

    Followup: Newbee Question

    mbs, Apr 6, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    309
    James Hancock
    Apr 6, 2004
  5. Sébastien V.

    Newbee question : List of packages

    Sébastien V., Jun 1, 2005, in forum: Python
    Replies:
    1
    Views:
    313
    Simon Brunning
    Jun 1, 2005
Loading...

Share This Page