一、习题介绍
第七章
Check Point:P251 7.2,7.4,7.16,8.2
Programming Exercise:7.10,7.14,7.26
二、习题及答案
Check Point:
7.2
When is the memory allocated for an array?
7.2什么时候为数组分配内存?
答:在编程中,数组的内存分配通常发生在数组被声明并初始化时。一旦数组被声明并分配了大小,内存就会被分配给数组中的每个元素。
7.4
Indicate true or false for the following statements:
■ Every element in an array has the same type.
■ The array size is fixed after an array reference variable is declared.
■ The array size is fixed after it is created.
■ The elements in an array must be a primitive data type
7.4为下列语句指明真或假:
数组中的每个元素都有相同的类型。
在数组引用变量声明后,数组的大小是固定的。
数组的大小在创建后是固定的。
数组中的元素必须是原始数据类型
答:数组中的每个元素都有相同的类型。(真)
在数组引用变量声明后,数组的大小是固定的。(真)
数组的大小在创建后是固定的。(真)
数组中的元素必须是原始数据类型。(假,数组元素也可以是对象)
7.16
True or false? When an array is passed to a method, a new array is created and passed to the method.
7.16对还是错?当一个数组被传递给一个方法时,一个新的数组被创建并传递给该方法。
答: 这是错误的。在大多数编程语言中,数组作为对象被传递时,传递的是引用,而不是数组的副本。因此,原始数组不会被复制。
8.2
Can the rows in a two-dimensional array have different lengths?
8.2二维数组中的行可以有不同的长度吗?
答:在大多数编程语言中,二维数组的行必须具有相同的长度。二维数组实际上是一个数组的数组,每个数组(即行)必须具有相同的大小。如果需要不同长度的行,可以使用数组的数组,但这不是传统意义上的二维数组。
Programming Exercise:
7.10 (Find the index of the smallest element) Write a method that returns the index of the smallest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header:
public static int indexOfSmallestElement(double[] array)
Write a test program that prompts the user to enter ten numbers, invokes this
method to return the index of the smallest element, and displays the index.
7.10(查找最小元素的索引)编写一个返回的索引的方法整数数组中最小的元素。如果这类元素的个数为大于1,返回最小的索引。使用下面的标题:
public static int indexofsmallstelement (double[] array)
编写一个测试程序,提示用户输入十个数字,调用这个方法返回最小元素的索引,并显示该索引。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double[] array = new double[10];
System.out.println("Enter 10 numbers:");
for (int i = 0; i < 10; i++) {
array[i] = scanner.nextDouble();
}
int index = indexOfSmallestElement(array);
System.out.println("The index of the smallest element is: " + index);
}
}
public static int indexOfSmallestElement(double[] array) {
if (array == null || array.length == 0) {
throw new IllegalArgumentException("Array must not be null or empty");
}
double min = array[0];
int minIndex = 0;
for (int i = 1; i < array.length; i++) {
if (array[i] < min) {
min = array[i];
minIndex = i;
}
}
return minIndex;
}
运行结果:
7.14 (Computing gcd) Write a method that returns the gcd of an unspecified number of integers. The method header is specified as follows:
public static int gcd(int... numbers)
Write a test program that prompts the user to enter five numbers, invokes the
method to find the gcd of these numbers, and displays the gcd.
7.14(计算gcd)编写一个方法返回一个未指定数字的gcd的整数。方法头指定如下:
Public static int gcd(int…数字)
编写一个测试程序,提示用户输入五个数字,调用方法查找这些数字的GCD,并显示GCD。
GCD值:返回两个或多个整数的最大公约数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] numbers = new int[5];
System.out.println("Enter 5 numbers:");
for (int i = 0; i < 5; i++) {
numbers[i] = scanner.nextInt();
}
int gcd = gcd(numbers);
System.out.println("The GCD is: " + gcd);
}
public static int gcd(int[] numbers) {
if (numbers.length == 0) {
throw new IllegalArgumentException("At least one number is required");
}
int result = numbers[0];
for (int i = 1; i < numbers.length; i++) {
result = gcdHelper(result, numbers[i]);
}
return result;
}
private static int gcdHelper(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
}
运行结果:
7.26 (Strictly identical arrays) The arrays list1 and list2 are strictly identical
if their corresponding elements are equal. Write a method that returns true if
list1 and list2 are strictly identical, using the following header:
public static boolean equals(int[] list1, int[] list2)
Write a test program that prompts the user to enter two lists of integers and dis
plays whether the two are strictly identical. Here are the sample runs. Note that
the first number in the input indicates the number of the elements in the list. This
number is not part of the list.
7.26(严格相同数组)数组list1和list2是严格相同的如果它们对应的元素相等。编写一个if返回true的方法List1和list2严格相同,使用以下标头:
Public static Boolean = (int[] list1, int[] list2)
编写一个测试程序,提示用户输入两个整数列表和dis播放两者是否完全相同。这里是运行的样本。请注意,输入中的第一个数字表示列表中元素的数量。这号码不在列表中。
public static boolean areArraysStrictlyEqual(int[] list1, int[] list2) {
if (list1 == null || list2 == null) {
return false;
}
if (list1.length != list2.length) {
return false;
}
for (int i = 0; i < list1.length; i++) {
if (list1[i] != list2[i]) {
return false;
}
}
return true;
}
public void main() {
}
运行结果
结语
请一定相信,相信自己不止于此
相信自己值得更好
相信只要努力,岁月自有打赏
!!!