您好,欢迎来到华佗小知识。
搜索
您的当前位置:首页Java数组练习题(带答案)

Java数组练习题(带答案)

来源:华佗小知识
一 填空题

1) 数组的元素通过 下标 来访问,数组Array的长度为 Array.length 。 2) 数组复制时,\"=\"将一个数组的 引用 传递给另一个数组。 3) JVM将数组存储在 栈 (堆或栈)中。

4) 数组的二分查找法运用的前提条件是数组已经 排序 。 5) Java中数组的下标的数据类型是 整型 。 6) 数组最小的下标是 0 。

7) arraycopy()的最后一个参数指明 复制元素的个数 。 8) 向方法传递数组参数时,传递的是数组的 引用 。 9) 数组初始化包括 数组的申明,创建和初始化 。

10) 数组下标访问超出索引范围时抛出 数组越界 异常 11) 浮点型数组的默认值是 0.0f 。 12) 数组创建后其大小 不能 改变。 二 选择题

1. 下面错误的初始化语句是_ABD__

A. char str[]=\"hello\";

B. char str[100]=\"hello\"; C. char str[]={'h','e','l','l','o'};

D. char str[]={'hello'};

2. 定义了一维int型数组a[10]后,下面错误的引用是_B__

A. a[0]=1;

B. a[10]=2;

C. a[0]=5*2;

D. a[1]=a[2]*a[0];

3. 下面的二维数组初始化语句中,正确的是____

A. float b[2][2]={0.1,0.2,0.3,0.4}; B. int a[][]={{1,2},{3,4}}; C. int a[2][]= {{1,2},{3,4}};

D. float a[2][2]={0};

4. 引用数组元素时,数组下标可以是_D___

A. 整型常量 B. 整型变量 C. 整型表达式 D. 以上均可 5. 定义了int型二维数组a[6][7]后,数组元素a[3][4]前的数组元素个数为____ A. 24

B. 25

C. 18

D. 17

6. 下列初始化字符数组的语句中,正确的是__B__

A. char str[5]=\"hello\"; B. char str[]={'h','e','l','l','o','\\0'}; C. char str[5]={\"hi\

D. char str[100]=\"\"; 7. 数组在Java中储存在 C 中

A. 栈

B. 队列

C. 堆

D. 链表

8. 下面程序的运行结果是____

main() {

int a[][]={{1,2,3},{4,5,6}}; System.out.printf(\"%d\

}

A. 3 B. 4 C. 5 D. 6 9. 下面程序的运行结果是_C___

main() {

1

int x=30;

int[] numbers=new int[x]; x=60;

System.out.println(numbers.length); }

A. 60 B. 20 C. 30 D. 50 10. 下面程序的运行结果是__BDF__

main() {

char s1[]=\"ABCDEF\".toCharArray(); int i=0;

while(s1[i++]!='\\0')

System.out.println(s1[i++]); }

A. ABCDEF

B. BDF C. ABCDE

D. BCDE

11. 下面 不是创建数组的正确语句C

A.float f[][]=new float[6][6]; B.float f[]=new float[6]; C.float f[][]=new float[][6];

D.float [][]f=new float[6][];

12. 下面不是数组复制方法的是(C) A. 用循环语句逐个复制数组 B. 用方法arraycopy C. 用\"=\"进行复制

D. 用clone方法 13. 数组a的第三个元素表示为D A. a(3)

B. a[3]

C.a(2)

D. a[2]

14. 当访问无效的数组下标时,会发生B

A. 中止程序 B. 抛出异常 C. 系统崩溃 D. 直接跳过 15. 使用arraycopy()方法将数组a复制到b正确的是A

A. arraycopy(a,0,b,0,a.length) B. arraycopy(a,0,b,0,b.length) C.. arraycopy(b,0,a,0,a.length) D. arraycopy(a,1,b,1,a.length) 16. 关于数组默认值,错误的是 B

A. char--'\"u0000' B. Boolean--true C. float--0.0f D. int-- 0 17. 关于数组作为方法的参数时,向方法传递的是 A

A. 数组的引用 B. 数组的栈地址 C. 数组自身 D. 数组的元素18. 关于数组复制,下列说法错误的是AC A. \"=\"可以实现数组复制

B. 运用循环语句进行数组复制必须两个数组长度相同 C. arraycopy()方法没有给目标数组分配内存空间 D. 数组复制是数组引用的传递

19. 下列语句会造成数组new int[10]越界是D A. a[0] += 9; B. a[9]=10;

C. —a[9] D. for(int i=0;i<=10;i++) a[i]++; 2

20. 在JDK环境下编译JAVA源程序使用的命令是(B )

A.java

B.javac

C.jvm

D.tomcat

D. 子类不能使用父类的构造方法

21. main方法是java Application 程序执行的入口点。关于main方法放入方法以下合法的是

( )

A. public static void main();

B. public static void main(String[]args)

C. public static int main(String[] arg) D. public void main(String arg[]) 22. 执行完代码\"int[] x=new int[25];\"后以下( A )说明正确的

A. x[24]为0

B. x[24]未定义 C. x[25]为0

D. x[0]为空

D. 用arrays.sort( )排序

23. 关于数组排序方法,错误的是 C

A. 选择排序 B. 插入排序 C. 二分排序 24. 关于char类型的数组,说法正确的是 D

A. 其数组的默认值是'A' B. 可以仅通过数组名来访问数组 C. 数组不能转换为字符串 D. 可以存储整型数值 25. 对于数组a[10],下列表示错误的是B A. a[0] B. a(0) A. int[] a B. int a[] 三、是非题

1.下标用于指出数组中某个元素位置的数字。( ) 2.把数组中元素按某种顺序排列的过程叫做查找。( ) 3.确定数组中是否含有某个关键字的过程叫做排序。( ) 4.一个数组可以存放许多不同类型的数值。( ) 5.数组的下标通常是float型。( )

6.数组的某个元素被传递给一个方法并被该方法修改,当被调用方法执行完毕时,这个元素中含有修改过的数值。( )

7.数组可以声明为任何数据类型。( )

8.数组由具有一名字和相同类型的一组连续内存单元构成。( )

9.在数组声明中可以用等号及一个逗号分隔的初始值表初始化数组元素,该数组大小只能由用户来决定。( )

10.将一个数组传递给一个方法,必须加在数组名后加方括号。( )

11.Java语言中的数组元素下标总是从0开始,下标可以是整数或整型表达式。( ) 12.下面这条语句正确吗?( ) double[] myList;

myList = {1.9, 2.9, 3.5, 4.6};

14. 数组中有length()这个方法,如array.length()表示数组array中元素的个数( ) 15.下面这条语句正确吗?( ) int t[3][2] = {{1,2},{3,4},{5,6}};

C. a[9] D. a[1] C. int[][] a D. int[]a[]

26. 下列数组声明,下列表示错误的是

3

16.数组声明后其大小固定。( )

17.设有整型数组的定义:int A.[]=new int[8]; ,则a.length的值为7。( ) 18. 数组一旦创建,其大小不能再改变。( )

19.用任何方式创建数组时,都必须指定数组的长度。( ) 20.声明数组时,要指定数组长度,以便为数组分配内存。( ) 四、简答题

1. 如何声明和创建一个一维数组? 2. 如何访问数组的元素?

3.数组下标的类型是什么?最小的下标是什么?一维数组a的第三个元素如何表示? 4.数组越界访问会发生什么错误?怎样避免该错误?

5.给方法传递数组参数与传递基本数据类型变量的值有何不同? 6.复制数组有哪些方法?

8.声明数组变量会为数组分配内存空间吗?为什么? 五、程序题

1.有一个整数数组,其中存放着序列1,3,5,7,9,11,13,15,17,19。请将该序列倒序存放并输出。

1) public class Test { 2) public static void main(String[] args) { 3) int a[] = {1,3,5,7,9,11,13,15,17,19}; 4) int t; 5) System.out.println(\"数组的初始状态为:\"); 6) for (int i=0; i < a.length; i++) 7) System.out.print(\" \" + a[i]); 8) System.out.println(); 9) 10) for (int i=0; i < a.length/2; i++) { 11) t = a[i]; 12) a[i] = a[a.length-i-1]; 13) a[a.length-i-1]=t; 14) } 15) 16) System.out.println(\"数组逆序存放后的状态为:\"); 17) for (int i=0; i < a.length; i++) 18) System.out.print(\" \" + a[i]); 19) } 20) }

2.编写一个程序,提示用户输入学生数量 姓名和他们的成绩,并按照成绩的降序来打印学生的姓名。

1) public class exercise16 {

2) public static void main(String[] args) {

4

3) String numberString =

4) JOptionPane.showInputDialog(\"Enter the number of students\"); 5) int numberOfStudents = Integer.parseInt(numberString); 6)

7) String[] names = new String[numberOfStudents]; 8) double[] scores = new double[numberOfStudents]; 9)

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

11) names[i] = JOptionPane.showInputDialog(\"Enter a student name\"); 12) scores[i] = Double.parseDouble(

13) JOptionPane.showInputDialog(\"Enter a student score\")); 14) } 15)

16) for (int i = scores.length - 1; i >= 1; i--) { 17) double currentMax = scores[0]; 18) int currentMaxIndex = 0; 19)

20) for (int j = 1; j <= i; j++) { 21) if (currentMax < scores[j]) { 22) currentMax = scores[j]; 23) currentMaxIndex = j; 24) } 25) } 26)

27) if (currentMaxIndex != i) {

28) scores[currentMaxIndex] = scores[i]; 29) scores[i] = currentMax;

30) String temp = names[currentMaxIndex]; 31) names[currentMaxIndex] = names[i]; 32) names[i] = temp; 33) } 34) } 35)

36) for (int i = scores.length - 1; i >= 0; i--) {

37) System.out.println(names[i] + \"\\" + scores[i]); 38) } 39) } 40) }

3.编写一个程序,使它能够读入10个整数,并且存储其中互不相同的数,最后将这10个数输出。

1) import javax.swing.JOptionPane; 2)

3) public class exercise5 {

5

4) private static int j = 0; 5) 6) public static void main(String[] args) { 7) int[] arrayInt = new int[10]; 8) int i = 0; 9) 10) do { 11) String numberString = JOptionPane 12) .showInputDialog(\"Enter a number: \"); 13) 14) int number = Integer.parseInt(numberString); 15) 16) if (isNotHave(arrayInt, number)) { 17) arrayInt[i] = number; 18) i++; 19) j++; 20) } 21) else 22) { 23) do { 24) numberString = JOptionPane 25) .showInputDialog(\"This number is exit,enter a

another number: \"); 26) number = Integer.parseInt(numberString); 27) } while (!isNotHave(arrayInt, number)); 28) 29) arrayInt[i] = number; 30) i++; 31) j++; 32) } 33) } while (i < arrayInt.length); 34) 35) String output = \"\"; 36) for (int k : arrayInt) { 37) output += k + \" \"; 38) } 39) 40) JOptionPane.showMessageDialog(null, \"The elements of arrayInt is \" 41) + output, \"output arrayInt\

JOptionPane.INFORMATION_MESSAGE); 42) } 43) 44) public static boolean isNotHave(int[] arrayInt, int n) { 45) for (int i = 0; i < j; i++) {

6

46) 47) 48) 49) 50) 51) }

}

if (arrayInt[i] == n) return false; }

return true;

4.先对数组{1,3,9,5,6,7,1,5,4,8}进行排序,然后二分查找法找出数组中的元素8,标出其排序后其下标的位置。

1) public class Test { 2) public static void main(String[] args) { 3) int numbers[] = {1,3,9,5,6,7,1,5,4,8}; 4) java.util.Arrays.sort(numbers); 5) 6) System.out.println(\"排序后的数组为:\"); 7) for (int i=0; i low) { mid = (mid == (low + high) / 2) ? 22) (mid + 1) : ((low + high) / 2); 23) if (key < list[mid]) 24) high = mid - 1; 25) else if (key == list[mid]) 26) return mid; 27) else 28) low = mid - 1; 29) } 30) return -low - 1; 31) } 32) }

5.编写一个程序,读入数目确定的考试分数,并且判断有多少个分数高于或等于平均分,有

7

多少个分数低于平均分。输入一个负数标志输入结束,假设最高分为100。

1) import javax.swing.JOptionPane; 2)

3) public class Test {

4) public static void main(String[] args) { 5) double[] scores = new double[100]; 6) double sum = 0; 7) int count = 0; 8)

9) do {

10) String scoreString =

11) JOptionPane.showInputDialog(null, \"Enter a new score\"); 12) scores[count] = Double.parseDouble(scoreString); 13) sum += scores[count];

14) } while (scores[count++] >= 0); 15)

16) double average = (sum - scores[count]) / (count - 1); 17)

18) int numOfAbove = 0; 19) int numOfBelow = 0;

20) for (int i = 0; i < count - 1; i++) 21) if (scores[i] >= average) 22) numOfAbove++; 23) else

24) numOfBelow++; 25)

26) System.out.println(\"Average is \" + average);

27) System.out.println(\"Number of scores above or equal to the average \" 28) + numOfAbove);

29) System.out.println(\"Number of scores below the average \" 30) + numOfBelow); 31)

32) System.exit(0); 33) } 34) }

6.编写一个程序,生成0-9之间的100个随机整数并且显示每一个数的个数。

1) public class exercise7 { 2) public static void main(String[] args) { 3) int[] numbers = new int[100]; 4) int[] counts = new int[10]; 5) 6) int k; 7)

for (int i = 0; i < 100; i++)

8

8) 9) 10) 11) 12) 13) 14) 15) 16) 17) 18) 19) 20) 21) 22) 23) 24) 25) 26) }

}

{ numbers[i] = (int)(Math.random() * 10); counts[numbers[i]]++; }

System.out.println(\"the 100 numbers is :\"); for(int i = 1; i <= numbers.length; i++) { if (i % 10 != 0) System.out.print(numbers[i-1] + \" \"); else System.out.println(numbers[i-1]); }

System.out.println(\"the counts of each number is :\"); for(int i = 1; i <= counts.length; i++) System.out.print(counts[i-1] + \" \");

7. 求平均值和方差。利用 mean(int []numbers)求平均值,利用 deviation(int []numbers)求标准差。

1) import java.util.Scanner;

2) public class MeanDeviation { 3) public static void main(String[] args) { 4) Scanner in = new Scanner(System.in); 5) 6) System.out.print(\"输入数组的长度:\"); 7) int n = in.nextInt(); 8) 9) System.out.print(\"输入数组元素:\"); 10) int[] array = new int[n]; 11) for (int i = 0; i < array.length; i++) 12) { 13) array[i] = in.nextInt(); 14) } 15) 16) System.out.print(\"The mean is: \" + mean(array) + \"\\n\" 17) + \"The deviation is: \" + deviation(array)); 18) } 19) 20) public static double mean(int []numbers) 21) { 22) double sum = 0.0;

9

23) 24) 25) 26) 27) 28) 29) 30) 31) 32) 33) 34) 35) 36) 37) 38) 39) 40) }

for(int i = 0; i < numbers.length; i++) { sum += numbers[i]; } return sum / numbers.length; }

public static double deviation(int []numbers) { double powSum = 0.0; for(int i = 0; i < numbers.length; i++) { powSum += Math.pow(numbers[i] - mean(numbers),2); } return Math.sqrt(powSum / (numbers.length - 1)); }

8.编写一个程序,求出整数数组中最小元素的下标。如果这样的元素个数大于1,则返回下标最小的数的下标。

1) public class MinIndex {

2) public static void main(String[] args) { 3) int[] list = {1, 2, 4, 5, 10, 100, 2, -22}; 4)

5) System.out.println(\"The min is \" + minIndex(list)); 6) } 7)

8) public static int minIndex(int[] list) { 9) int min = list[0]; 10) int minIndex = 0; 11)

12) for (int i = 1; i < list.length; i++) 13) if (min > list[i]) { 14) min = list[i]; 15) minIndex = i; 16) } 17)

18) return minIndex; 19) }

10

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo0.cn 版权所有 湘ICP备2023017654号-2

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务