-3

I have been watching some LinkedList videos to try and understand what it is. But I see a lot of people having code like

for(String x : model)

Can anyone help me understand what ":" does in this code besides attaching x to "model" or is that all it does?

4 Answers4

0

It means the loop will iterate through each object of the list

String x declares a String named x

model is the list of String you want to iterate through

: is the operator making the compiler doing this operation.

You can read the for like this : For each String in model, use x as variable and do the following operations.

you can then use x to do the operations you want on each elements of the list

Sirmyself
  • 1,392
  • 1
  • 11
  • 25
0

In this context, : literally means in.

jurez
  • 3,847
  • 2
  • 8
  • 18
0

This is the syntax of the enhanced for loop. It marely means that you're iterating over all the elements in model, where in each iteration the String x is assigned with the current element so you can use in the loop's body.

Mureinik
  • 277,661
  • 50
  • 283
  • 320
0

Similar to mathematical notation that represents elements in a set.

Read left to right; For all string's x that are elements in model, do .