java switch foreach

本文最后更新于 2020年4月22日 下午

switch

switch的条件(switch括号中的内容)可以有一下几种。

  • 可以自动转换成int的类型(byte,short,int,char)
  • 字符串类型
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    String color = "red";
    switch(color)
    {
    case "red":
    System.out.println("红色");
    break;
    case "blue:
    System.out.println("蓝色");
    break;
    default:
    System.out.println("其他颜色");
    }
  • 枚举类型
    1
    2
    3
    4
    enum Colour(red,blue);
    Colour c = Colour.red;
    Switch(c)
    ...

    foreach语句

foreach是for的特殊简化版本。用来遍历数组或集合

语法:

1
2
3
4
for(变量类型 变量名 : 待输出的集合或数组)
{
...
}

其实这也只能说是稍稍简化,直接写for就可以了。


java switch foreach
https://www.xinhecuican.tech/post/4c720881.html
作者
星河璀璨
发布于
2020年4月22日
许可协议