Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19912264
  • 博文数量: 679
  • 博客积分: 10495
  • 博客等级: 上将
  • 技术积分: 9308
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-18 10:51
文章分类

全部博文(679)

文章存档

2012年(5)

2011年(38)

2010年(86)

2009年(145)

2008年(170)

2007年(165)

2006年(89)

分类: Java

2009-07-06 19:23:19

§4 数组、逻辑和循环

Java中各个数组元素的类型必须一致。数组也是对象。

数组定义:

String[] requests;

Point[] targets;

float[] donations;

或者

String requests[];

Point targets[];

float donations[];

 

int[] temps = new int[99];使用new的方式必须指明个数。javanull指的是空对象,不像c中一般等同于‘\0

 

Integer[] series = new Integer[3];

series[0] = new Integer(10);

series[1] = new Integer(3);

series[2] = new Integer(5);

 

Point[] markup = { new Point(1,5), new Point(3,3), new Point(2,3) };

 

String[] titles = { “Mr.”, “Mrs.”, “Ms.”, “Miss”, “Dr.” };

 

Java中数组下标会有防止越界的处理,不像c没有这个功能。

数组长度

System.out.println(“Elements: “ + rating.length);

 

switch 判断的类型仅仅支持可以转换为整型的简单数据类型。

 

 

class HalfLooper {

    public static void main(String[] arguments) {

        int[] denver = { 2500000, 2900000, 3500000 };

        int[] philadelphia = { 2500000, 2900000, 3800000 };

        int[] total = new int[denver.length];

        int sum = 0;

       

        for (int i = 0; i < denver.length; i++) {

            total[i] = denver[i] + philadelphia[i];

            System.out.format((i + 2003) + " production: %,d%n",

                total[i]);

            sum += total[i];

        }

 

        System.out.format("Average production: %,d%n",

            (sum / denver.length));

    }

}

 

 

运行结果:

D:\java\9433-source-code\chapter4>java HalfDollars

2003 production: 5,000,000

2004 production: 5,800,000

2005 production: 7,300,000

Average production: 6,033,333

 

 

计算每月的天数

class DayCounter {

    public static void main(String[] arguments) {

        int yearIn = 2008;

        int monthIn = 1;

        if (arguments.length > 0)

            monthIn = Integer.parseInt(arguments[0]);

        if (arguments.length > 1)

            yearIn = Integer.parseInt(arguments[1]);

        System.out.println(monthIn + "/" + yearIn + " has "

            + countDays(monthIn, yearIn) + " days.");

    }

 

    static int countDays(int month, int year) {

        int count = -1;

        switch (month) {

            case 1:

            case 3:

            case 5:

            case 7:

            case 8:

            case 10:

            case 12:

                count = 31;

                break;

            case 4:

            case 6:

            case 9:

            case 11:

                count = 30;

                break;

            case 2:

                if (year % 4 == 0)

                    count = 29;

                else

                    count = 28;

                if ((year % 100 == 0) & (year % 400 != 0))

                    count = 28;

        }

        return count;

    }

}

 

运行结果

java DayCounter 4 2008

The output is the following:

4/2008 has 30 days.

 

 

 

class ArrayCopier {

    public static void main(String[] arguments) {

        int[] array1 = { 7, 4, 8, 1, 4, 1, 4 };

        float[] array2 = new float[array1.length];

 

        System.out.print("array1: [ ");

        for (int i = 0; i < array1.length; i++) {

            System.out.print(array1[i] + " ");

        }

        System.out.println("]");

 

        System.out.print("array2: [ ");

        int count = 0;

        while ( count < array1.length && array1[count] != 1) {

            array2[count] = (float) array1[count];

            System.out.print(array2[count++] + " ");

        }

        System.out.println("]");

    }

}

 

执行结果

array1: [ 7 4 8 1 4 1 4 ]

array2: [ 7.0 4.0 8.0 ]

 

break continue可以跳至外面的标签。

三元运算符号:test ? trueresult : falseresult;

阅读(2667) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~