for(Person p : persons){
if (p.getGender() == 'F'){
count++;
}
}
Can you please explain what the meaning of "Person p : persons" inside the for loop is?
What is p and where does it come from? What is the meaning of ":"? Thanks.
1
Expert's answer
2016-04-21T11:52:05-0400
In a basics it looks like this - for(Type of iteration variable: collection) unit operators. In our example persons – it is collection of Person elements, in our case array. In this case creates a link “p”(type – Person), which in order assigned to each element of the array. You can rewrite this loop with the standart for loop realization: // for each int count = 0; for(Person p : persons){ if (p.getGender() == 'F'){ count++; } // for loop int count = 0; for(int i = 0; i < persons.size(); i++){ if (persons[i] == 'F'){ count++; } You are asking what is p – it is a single instance of the collection – for Person[] it will be Person, for int[] it will be int, for Object[] it will be Object. ‘:’ – it is a part of for each style – it means that it will iterate throw every element of the collection persons.
"assignmentexpert.com" is professional group of people in Math subjects! They did assignments in very high level of mathematical modelling in the best quality. Thanks a lot
Comments
Leave a comment