Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #27258 > unrolled thread

Newbee List question

Started byTodor Atanasov <xumepoc@gmail.com>
First post2014-01-09 04:16 -0800
Last post2014-02-03 19:39 -0800
Articles 8 — 5 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Newbee List question Todor Atanasov <xumepoc@gmail.com> - 2014-01-09 04:16 -0800
    Re: Newbee List question Joerg Meier <joergmmeier@arcor.de> - 2014-01-09 13:38 +0100
    Re: Newbee List question leif.roar@invalid.com (Leif Roar Moldskred) - 2014-01-09 07:50 -0600
      Re: Newbee List question Tim Slattery <tim@risingdove.com> - 2014-01-09 14:12 -0500
        Re: Newbee List question leif.roar@invalid.com (Leif Roar Moldskred) - 2014-01-09 14:41 -0600
    Re: Newbee List question Todor Atanasov <xumepoc@gmail.com> - 2014-01-09 06:52 -0800
    Re: Newbee List question Roedy Green <see_website@mindprod.com.invalid> - 2014-02-03 19:36 -0800
    Re: Newbee List question Roedy Green <see_website@mindprod.com.invalid> - 2014-02-03 19:39 -0800

#27258 — Newbee List question

FromTodor Atanasov <xumepoc@gmail.com>
Date2014-01-09 04:16 -0800
SubjectNewbee List question
Message-ID<4bf24581-c504-4225-a584-38b3249e2edc@googlegroups.com>
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??

[toc] | [next] | [standalone]


#27259

FromJoerg Meier <joergmmeier@arcor.de>
Date2014-01-09 13:38 +0100
Message-ID<18qf2f1o2tzly.14u6uaa1msbue.dlg@40tude.net>
In reply to#27258
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.

[toc] | [prev] | [next] | [standalone]


#27260

Fromleif.roar@invalid.com (Leif Roar Moldskred)
Date2014-01-09 07:50 -0600
Message-ID<AoudnX8K7N26NFPPnZ2dnUVZ8uudnZ2d@giganews.com>
In reply to#27258
Todor Atanasov <xumepoc@gmail.com> wrote:
> 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??

Yes, but be aware that a particular /group/ list doesn't _stay_ empty, 
and that it might stick around for more than one iteration of the loop.

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/.

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

-- 
Leif Roar Moldskred

[toc] | [prev] | [next] | [standalone]


#27265

FromTim Slattery <tim@risingdove.com>
Date2014-01-09 14:12 -0500
Message-ID<eustc9h7stgb83bb7e09cb8kp7frlqmn51@4ax.com>
In reply to#27260
leif.roar@invalid.com (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

[toc] | [prev] | [next] | [standalone]


#27266

Fromleif.roar@invalid.com (Leif Roar Moldskred)
Date2014-01-09 14:41 -0600
Message-ID<wqydndkdCvjtlFLPnZ2dnUVZ8hOdnZ2d@giganews.com>
In reply to#27265
Tim Slattery <tim@risingdove.com> wrote:
> 
> 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.

In the very last line of the for loop : 
  group.add(test);

-- 
Leif Roar Moldskred

[toc] | [prev] | [next] | [standalone]


#27261

FromTodor Atanasov <xumepoc@gmail.com>
Date2014-01-09 06:52 -0800
Message-ID<5315cf7a-be8d-4057-a11e-76d5f08dfe9f@googlegroups.com>
In reply to#27258
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, 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??

Thank you both for the reply, I will have to look more deeply into the logic here :D

[toc] | [prev] | [next] | [standalone]


#27459

FromRoedy Green <see_website@mindprod.com.invalid>
Date2014-02-03 19:36 -0800
Message-ID<81o0f9d67roctd632iuf31uvk6oq67g7ts@4ax.com>
In reply to#27258
On Thu, 9 Jan 2014 04:16:42 -0800 (PST), Todor Atanasov
<xumepoc@gmail.com> 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 

[toc] | [prev] | [next] | [standalone]


#27460

FromRoedy Green <see_website@mindprod.com.invalid>
Date2014-02-03 19:39 -0800
Message-ID<c4o0f9hqaodu14qje9a45uouoi6df2eqa8@4ax.com>
In reply to#27258
On Thu, 9 Jan 2014 04:16:42 -0800 (PST), Todor Atanasov
<xumepoc@gmail.com> 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 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web