当前位置:   article > 正文

JAVA期末速成库(7)第七、八章

JAVA期末速成库(7)第七、八章

一、习题介绍

第七章

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为下列语句指明真或假:

a.数组中的每个元素都有相同的类型。

b.在数组引用变量声明后,数组的大小是固定的。

c.数组的大小在创建后是固定的。

d.数组中的元素必须是原始数据类型

答:a.数组中的每个元素都有相同的类型。(真)

Java中的数组要求存储的元素类型必须相同,无论是原始数据类型还是对象类型。

b.在数组引用变量声明后,数组的大小是固定的。(真)

一旦声明并初始化了数组,其大小就固定了,不能通过重新声明来改变其大小。

c.数组的大小在创建后是固定的。(真)

同上,Java数组一旦创建,其大小就不可改变。

d.数组中的元素必须是原始数据类型。(假)

Java数组可以包含原始数据类型(如int、double等),也可以包含对象类型,例如自定义类或Java内置类的对象。

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对还是错?当一个数组被传递给一个方法时,一个新的数组被创建并传递给该方法。

答: 。在Java中,当一个数组被传递给一个方法时,并不是创建了一个新的数组并传递给该方法,而是传递了原数组的引用(或者说是指针)给这个方法。这意味着在方法内部对数组所做的任何非静态修改(比如修改数组元素的值)都会反映到原数组上,因为方法内部和原数组实际上引用的是同一个数组对象。

8.2

Can the rows in a two-dimensional array have different lengths?

8.2二维数组中的行可以有不同的长度吗?

答:在 Java 中,二维数组可以是“非规则的“或”锯齿形的”,这意味着每一行(实际上是一个数组)可以有不同的长度。

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)

编写一个测试程序,提示用户输入十个数字,调用这个方法返回最小元素的索引,并显示该索引。

  1. import java.util.Scanner;
  2. public class Main {
  3.     public static void main(String[] args) {
  4.         Scanner scanner = new Scanner(System.in);
  5.         double[] array = new double[10];
  6.         System.out.println("Enter 10 numbers:");
  7.         for (int i = 0; i < 10; i++) {
  8.             array[i] = scanner.nextDouble();
  9.         }
  10.         int index = indexOfSmallestElement(array);
  11.         System.out.println("The index of the smallest element is: " + index);
  12.     }
  13. }
  14. public static int indexOfSmallestElement(double[] array) {
  15.     if (array == null || array.length == 0) {
  16.         throw new IllegalArgumentException("Array must not be null or empty");
  17.     }
  18.     double min = array[0];
  19.     int minIndex = 0;
  20.     for (int i = 1; i < array.length; i++) {
  21.         if (array[i] < min) {
  22.             min = array[i];
  23.             minIndex = i;
  24.         }
  25.     }
  26.     return minIndex;
  27. }

运行结果: 

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值:返回两个或多个整数的最大公约数

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner scanner = new Scanner(System.in);
  5. int[] numbers = new int[5];
  6. System.out.println("Enter 5 numbers:");
  7. for (int i = 0; i < 5; i++) {
  8. numbers[i] = scanner.nextInt();
  9. }
  10. int gcd = gcd(numbers);
  11. System.out.println("The GCD is: " + gcd);
  12. }
  13. public static int gcd(int[] numbers) {
  14. if (numbers.length == 0) {
  15. throw new IllegalArgumentException("At least one number is required");
  16. }
  17. int result = numbers[0];
  18. for (int i = 1; i < numbers.length; i++) {
  19. result = gcdHelper(result, numbers[i]);
  20. }
  21. return result;
  22. }
  23. private static int gcdHelper(int a, int b) {
  24. while (b != 0) {
  25. int temp = b;
  26. b = a % b;
  27. a = temp;
  28. }
  29. return a;
  30. }
  31. }

运行结果: 

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播放两者是否完全相同。这里是运行的样本。请注意,输入中的第一个数字表示列表中元素的数量,这号码不在列表中。

  1. public static boolean areArraysStrictlyEqual(int[] list1, int[] list2) {
  2. if (list1 == null || list2 == null) {
  3. return false;
  4. }
  5. if (list1.length != list2.length) {
  6. return false;
  7. }
  8. for (int i = 0; i < list1.length; i++) {
  9. if (list1[i] != list2[i]) {
  10. return false;
  11. }
  12. }
  13. return true;
  14. }
  15. public void main() {
  16. }

运行结果

  结语

请一定相信,相信自己不止于此

相信自己值得更好

相信只要努力,岁月自有打赏

!!!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号