java day4

4.1 数组

4.1.1 声明数组变量

String[] str;
String str[];

4.1.2 创建数组对象

int[] temps = new int[99];

使用new创建数组对象时,其所有元素都被自动地初始化(数字数组为0,布尔数组为false,字符数组为‘\0’,对象数组为null)。

  • Java关键字null指的是null对象。它并不像在C语言中的null常量那样,为零或字符‘\0’。
  • 对于String类型的数组可以这样初始化:
String titles[]={"da","Mr","farther"};

4.1.3 访问数组元素

float[] rating = new float[20];
rating[2]=3.22F;

4.1.4 修改数组元素

java对象数组是一组到对象的引用。将对象赋给这种数组数组中的元素时,将创建一个到该对象的引用。移动数组中的值,是在重新指定引用,而不是将值从一个元素复制到另一个中。对于基本数据类型(如int或float)的数组,这种操作实际上是将值从一个元素复制到另一个元素中;String数组也是如此,虽然它们是对象。

public class HalfDollars {
    public static void main(String[] args) {
        int[] denver = {1_700_000, 4_600_000, 2_100_000};
        int[] philadelphia = new int[denver.length];
        int[] total = new int[denver.length];
        int average;

        philadelphia[0] = 1_800_000;
        philadelphia[1] = 5_000_000;
        philadelphia[2] = 2_500_000;

        total[0] = denver[0] + philadelphia[0];
        total[1] = denver[1] + philadelphia[1];
        total[2] = denver[2] + philadelphia[2];
        average = (total[0] + total[1] + total[2]) / 3;

        System.out.println("2012 production: ");
        System.out.format("%,d%n", total[0]);
        System.out.println("2013 production: ");
        System.out.format("%,d%n", total[1]);
        System.out.println("2014 production: ");
        System.out.format("%,d%n", total[2]);
        System.out.println("Average production: ");
        System.out.format("%,d%n", average);
    }
}
输出:
2012 production: 
3,500,000
2013 production: 
9600000.00000000000
2014 production: 
4,600,000
Average production: 
5,900,000

4.1.5 多维数组

class Try{
    public static void main(String[] args){
        int[][][] cen = new int[100][52][7];
        System.out.println(cen.length);
        System.out.println(cen[0].length);
        System.out.println(cen[0][0].length);
    }
}

4.2 块语句

块也叫块语句(block statement),因为整个块可用任何可使用单条语句的地方。

  • 在Java中,变量的作用域是声明该变量的语句所在的块。可以在块中声明和使用局部变量,在该块执行完毕后,这些变量将不复存在。

4.3 if条件语句

if(a>b){
	操作1}
else{
	操作2}

4.4 switch条件语句

String A="B";
switch(A){
	case "A":
    	System.out.println("1");
    	break;
    case "B":
    	System.out.println("2");
    	break;
    case "C":
    	System.out.println("3");
    	break;
    default:
    	System.out.println("null");
}

switch语句中的测试只能是可转换为int的基本数据类型,如char或字符串。不能在switch中使用更大的数据类型,如long、float,也不能测试除相等性外的其他关系。

4.5 三目运算符

test ? trueResult : falseResult;

4.6 for循环

for(initialization; test; increment){
	statement;
}

4.7 while和do循环

4.7.1 while循环

while(i<13){
        x=x*i++; //the body of the loop
}

4.7.2 do…while循环

long i =1;
        do{
            i*=2;
            System.out.print(i+" ");
        }while(i<3_000_000_000_000L);

4.8 跳出循环

  • 关键字break:立即结束当前循环。如果在循环中嵌套了循环,将跳到外层循环中,否则执行循环后的语句。
  • 关键字continue直接进入循环的下一次迭代。

标号

  • break和continue都有可选的标号,指出从哪里开始继续执行程序。使用标号后,break可以跳到循环外的某个位置,continue可以跳到当前循环外的循环中。
  • 要使用标号,请在循环的起始部分前面添加标号和冒号。然后,使用break和continue在这些关键字后面加上标号的名称,如下所示:
out: for(int i=0;i<10;i++){
            for(int j=0;j<50;j++){
                if(i*j>400){
                    break out;
                }
            }
        }

在上述代码片段中,标号out标记的是外层循环。然后,在for和while循环中,当特定条件满足时,break将跳出这两个循环。如果没有标号out,break将跳出内层循环,并继续执行外层循环。

  • 在Java中外层循环用的很少,因为通常有其他替代方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值