background image

Java 

 

的核心 API

6.1 基本类型数组

(1)首先预测下列程序的运行结果,然后编译、执行以验证你的预测:

        class JLab0601{

public static void main(String[] args){

double[] fatGrams = new double[6];
fatGrams[0] = 12.6;
fatGrams[1] = 32.0;
fatGrams[2] = 2.0;
fatGrams[3] = 11.2;
fatGrams[4] = 0.5;
fatGrams[5] = 3.99;
for(int j = 0; j < fatGrams.length; j++){

System.out.println(fatGrams[j]);

}

}

}

(2)改写上述程序,将数组元素的值按由大到小的顺序输出。

对象的数组

(1)首先预测下列程序的运行结果,然后编译、执行以验证你的预测:

  

  class JLab0602{

public static void main(String[] args){

String[] animals = {"cat", "fish", "mouse"};
for(int j = 0; j < animals.length; j++){

System.out.print(animals[j] + " ");

}
System.out.println();

}

}

(2)使用 String 类中的 toUpperCase()方法,改写循环体中的语句,

使得上述程序将 animals 数组中的元素转换为大写输出。

(3)改写(1)中的程序,使得数组中的元素按照单词字母数量由多到