Apparently a colon is used in multiple ways in Java. Would anyone mind explaining what it does?
For instance here:
String cardString = "";
for (PlayingCard c : this.list) // <--
{
cardString += c + "\n";
}
How would you write this for-each
loop a different way so as to not incorporate the :
?
The colon(:) is one of the most useful operator in MATLAB. It is used to create vectors, subscript arrays, and specify for iterations. You can use the colon operator to create a vector of indices to select rows, columns or elements of arrays.
In the esoteric programming language INTERCAL, the colon is called "two-spot" and is used to identify a 32-bit variable—distinct from a spot (.)
Since most for loops are very similar, Java provides a shortcut to reduce the amount of code required to write the loop called the for each loop. Here is an example of the concise for each loop: for (Integer grade : quizGrades){ System.out.println(grade); } In the example above, the colon (:) can be read as "in".
For-each loop syntax consists of datatype with the variable followed by a colon(:) then collection or array.
In your specific case,
String cardString = "";
for (PlayingCard c : this.list) // <--
{
cardString = cardString + c + "\n";
}
this.list
is a collection (list, set, or array), and that code assigns c
to each element of the collection.
So, if this.list
were a collection {"2S", "3H", "4S"} then the cardString
on the end would be this string:
2S
3H
4S
There is no "colon" operator, but the colon appears in two places:
1: In the ternary operator, e.g.:
int x = bigInt ? 10000 : 50;
In this case, the ternary operator acts as an 'if' for expressions. If bigInt is true, then x will get 10000 assigned to it. If not, 50. The colon here means "else".
2: In a for-each loop:
double[] vals = new double[100];
//fill x with values
for (double x : vals) {
//do something with x
}
This sets x to each of the values in 'vals' in turn. So if vals contains [10, 20.3, 30, ...], then x will be 10 on the first iteration, 20.3 on the second, etc.
Note: I say it's not an operator because it's just syntax. It can't appear in any given expression by itself, and it's just chance that both the for-each and the ternary operator use a colon.
Just to add, when used in a for-each loop, the ":" can basically be read as "in".
So
for (String name : names) {
// remainder omitted
}
should be read "For each name IN names do ..."
How would you write this for-each loop a different way so as to not incorporate the ":"?
Assuming that list
is a Collection
instance ...
public String toString() {
String cardString = "";
for (Iterator<PlayingCard> it = this.list.iterator(); it.hasNext(); /**/) {
PlayingCard c = it.next();
cardString = cardString + c + "\n";
}
}
I should add the pedantic point that :
is not an operator in this context. An operator performs an operation in an expression, and the stuff inside the ( ... )
in a for
statement is not an expression ... according to the JLS.
It's used in for loops to iterate over a list of objects.
for (Object o: list)
{
// o is an element of list here
}
Think of it as a for <item> in <list>
in Python.
You usually see it in the ternary assignment operator;
Syntax
variable = `condition ? result 1 : result 2;`
example:
boolean isNegative = number > 0 ? false : true;
which is "equivalent" in nature to the if else
if(number > 0){
isNegative = false;
}
else{
isNegative = true;
}
Other than examples given by different posters,
you can also use : to signify a label for a block which you can use in conjunction with continue and break..
for example:
public void someFunction(){
//an infinite loop
goBackHere: { //label
for(int i = 0; i < 10 ;i++){
if(i == 9 ) continue goBackHere;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With