Newbee List question

T

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

Joerg Meier

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
 
T

Todor Atanasov

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
 
T

Tim Slattery

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

Roedy Green

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.
 

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

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top