目录
- 第五章 习题
- 1.
- 2. 数组下标的类型是什么?最小的下标是多少?最大小标是多少?举例说明。
- 3. 使用arraycopy()方法
第五章 习题
1.
//1.反序输出
private static void u5p1(){
System.out.println("请输入10个浮点数:");
Scanner sc = new Scanner(System.in);
float num[] = new float[10];
//输入
for (int i = 0; i < 10; i++) {
num[i] = sc.nextFloat();
}
//输出
for (int i = num.length - 1; i >= 0; i--) {
System.out.print(num[i] + " ");
}
}
2. 数组下标的类型是什么?最小的下标是多少?最大小标是多少?举例说明。
- Java数组的length必须是非负的int,所以它的理论最大值就是daojava.lang.Integer.MAX_VALUE = 2^31-1 = 2147483647。
- 实际的JVM不一定能支持上面说的理论上的最大length。
- 例如说如果有JVM使用uint32_t来记录对象大小的话,那可以允许的最大的数组长度(按元素的个数计算)就会是:(uint32_t的最大值 - 数组对象的对象头大小) / 数组元素大小。
例子:int a[] = new int[2147483647]; //合法
3. 使用arraycopy()方法
- 将下述数组复制到目标数组target。float[] source = {1.5, 4.4, 7.5, 8.8 ,9.0};
private static void u5p2(){
float target[] = new float[5];
float source[] = {1.5f, 4.4f, 7.5f, 8.8f, 9.0f};
System.arraycopy(source, 0, target, 0, source.length);
//输出
for (float temp:target) {
System.out.println(temp);
}
}