显然,冒号在 Java 中有多种使用方式。有人介意解释一下它的作用吗?
例如这里:
String cardString = "";
for (PlayingCard c : this.list) // <--
{
cardString += c + "\n";
}
你会如何以不同的方式编写这个
for-each
循环,以免合并 :
?
Java 代码中有几个地方使用了冒号:
跳出标签(教程):
label: for (int i = 0; i < x; i++) {
for (int j = 0; j < i; j++) {
if (something(i, j)) break label; // jumps out of the i loop
}
}
// i.e. jumps to here
三元条件(教程):
int a = (b < 4)? 7: 8; // if b < 4, set a to 7, else set a to 8
For-each 循环(教程):
String[] ss = {"hi", "there"}
for (String s: ss) {
print(s); // output "hi" , and "there" on the next iteration
}
断言(指南):
int a = factorial(b);
assert a >= 0: "factorial may not be less than 0"; // throws an AssertionError with the message if the condition evaluates to false
switch 语句中的情况(教程):
switch (type) {
case WHITESPACE:
case RETURN:
break;
case NUMBER:
print("got number: " + value);
break;
default:
print("syntax error");
}
方法参考(教程)
class Person {
public static int compareByAge(Person a, Person b) {
return a.birthday.compareTo(b.birthday);
}}
}
Arrays.sort(persons, Person::compareByAge);
没有“冒号”运算符,但冒号出现在两个地方:
1:在三元运算符中,例如:
int x = bigInt ? 10000 : 50;
在这种情况下,三元运算符充当表达式的“if”。如果 bigInt 为 true,则 x 将被分配 10000。如果不是,50。这里的冒号表示“其他”。
2:在 for-each 循环中:
double[] vals = new double[100];
//fill x with values
for (double x : vals) {
//do something with x
}
这会依次将 x 设置为 'vals' 中的每个值。因此,如果 vals 包含 [10, 20.3, 30, ...],则 x 在第一次迭代时将为 10,在第二次迭代时为 20.3,等等。
注意:我说它不是一个运算符,因为它只是语法。它本身不能出现在任何给定的表达式中,这只是 for-each 和三元运算符都使用冒号的机会。
补充一下,当在 for-each 循环中使用时,“:”基本上可以读作“in”。
所以
for (String name : names) {
// remainder omitted
}
应阅读“对于名称中的每个名称......”
你会如何以不同的方式编写这个 for-each 循环以避免合并“:”?
假设
list
是一个 Collection
实例...
public String toString() {
String cardString = "";
for (Iterator<PlayingCard> it = this.list.iterator(); it.hasNext(); /**/) {
PlayingCard c = it.next();
cardString = cardString + c + "\n";
}
}
我应该补充一点迂腐的观点,即
:
在此上下文中不是运算符。运算符在表达式中执行运算,而 ( ... )
语句中 for
内的内容不是表达式……根据 JLS。
它在 for 循环中用于迭代对象列表。
for (Object o: list)
{
// o is an element of list here
}
将其视为 Python 中的
for <item> in <list>
。
根据您的具体情况,
String cardString = "";
for (PlayingCard c : this.list) // <--
{
cardString = cardString + c + "\n";
}
this.list
是一个集合(列表、集合或数组),该代码将 c
分配给集合的每个元素。
所以,如果
this.list
是一个集合 {"2S", "3H", "4S"} 那么末尾的 cardString
将是这个字符串:
2S
3H
4S
你通常会在三元赋值运算符中看到它;
语法
variable = `condition ? result 1 : result 2;`
示例:
boolean isNegative = number > 0 ? false : true;
本质上与 if else 是“等价的”
if(number > 0){
isNegative = false;
}
else{
isNegative = true;
}
除了不同发帖者给出的例子,
您还可以使用 : 来表示块的标签,您可以将其与 continue 和 break 结合使用..
例如:
public void someFunction(){
//an infinite loop
goBackHere: { //label
for(int i = 0; i < 10 ;i++){
if(i == 9 ) continue goBackHere;
}
}
}
用在新的简写 for/loop 中
final List<String> list = new ArrayList<String>();
for (final String s : list)
{
System.out.println(s);
}
和三元运算符
list.isEmpty() ? true : false;
冒号实际上与
?
一起存在
int minVal = (a < b) ? a : b;
相当于:
int minval;
if(a < b){ minval = a;}
else{ minval = b; }
也在 foreach 循环中:
for(Node n : List l){ ... }
字面意思:
for(Node n = l.head; n.next != null; n = n.next)
它将打印字符串“something”三次。
JLabel[] labels = {new JLabel(), new JLabel(), new JLabel()};
for ( JLabel label : labels )
{
label.setText("something");
panel.add(label);
}
由于大多数 for 循环都非常相似,Java 提供了一种快捷方式来减少 编写名为 foreach 循环的循环所需的代码量。
以下是每个循环的简洁示例:
for (Integer grade : quizGrades){
System.out.println(grade);
}
在上面的例子中,冒号(:)可以读作“in”。 for 每个循环 总共可以读作“对于每个整数元素(称为等级) quizGrades,打印出成绩值。”
冒号在 for-each 循环中使用, 试试这个例子,
import java.util.*;
class ForEachLoop
{
public static void main(String args[])
{`enter code here`
Integer[] iray={1,2,3,4,5};
String[] sray={"ENRIQUE IGLESIAS"};
printME(iray);
printME(sray);
}
public static void printME(Integer[] i)
{
for(Integer x:i)
{
System.out.println(x);
}
}
public static void printME(String[] i)
{
for(String x:i)
{
System.out.println(x);
}
}
}