(一) 选择题
1. 创建二维数组a[3][4]后,a[0].length的值应该等于
A.0
B.2
C.3
D.4
2. 下列对长度为4的数组a的定义中,正确的是
A.int[4] a=new int[];
B.int a[]=new int[5];
C.int a[]={2,4,2,1};
D.int[4] a=new int[4];
3. 创建二维数组a[3][4]后,a.length的值应该等于
A.0
B.2
C.3
D.4
4. 下列关于数组的使用,不正确的是
A.数组长度定义后不可以修改;
B.数组中只能存放同类型数据;
C.数组下标范围从0开始最大到数组长度;
D.数组中可以存放普通数据也可以存对象;
(二)简答题
1.写出程序的运行结果。
public class Test {
public static void main(String[] args) {
int a[]={45,18,98,56,304};
for(int i=a.length-1;i>=0;i--)
System.out.println(a[i]);
}
}
2.写出程序的运行结果。
public class Test {
public static void main(String[] args) {
int i,j;
int a[]={9,27,10,1,49};
for(i=0;i<a.length-1;i++){
int k=i;
for(j=i;j<a.length;j++){
if(a[j]>a[k])
k=j;
}
int temp=a[i];
a[i]=a[k];
a[k]=temp;
}
for(i=0;i<a.length;i++)
System.out.println(a[i]+"");
System.out.println();
}
}
3.阅读下列程序,写出程序的运行结果。
public class abc{
public static void main(String args[ ]){
int i ;
int s = 0 ;
int a[ ] = { 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 };
for ( i = 0 ; i < a.length ; i ++ ){
if ( a[i]%3 == 0 )
s += a[i] ;
}
System.out.println("s="+s);
}
}
(三)编程题
1、编写一个简单程序,要求数组长度为5,分别赋值10,20,30,40,50,在控制台输出该数组的值。
2、将一个字符数组的值(neusofteducation)拷贝到另一个字符数组中。
3、给定一个有9个整数(1,6,2,3,9,4,5,7,8)的数组,先排序,然后输出排序后的数组的值。
4、在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。
5、将一个数组中的元素逆序存放到另一各数组中。
6、将一个数组中的重复元素保留一个其他的清零。
7、给定一维数组{ -10,2,3,246,-100,0,5} ,计算出数组中的平均值、最大值、最小值。
8、【难】假设队列中共有500人,每次从1开始数,数到3的人出队,下一个人接着从1开始数,到结尾后接着从头继续循环数,编写程序找到最后剩下的人是哪一个。
public class Count3Quit {
public static void main(String args[]) {
// 用布尔值来标识每个人是否出队 出队为false,未出队为true 并将每个人的初始值都赋为true
boolean a[] = new boolean[500];
for (int i = 0; i < a.length; i++) {
a[i] = true;
}
/*leftNum:未出队的人数 countNum:取值为1、2、3, 每次从1开始数,数到3的人出队,下一个人接着从1开始数
index:数数的人的编号,取值范围0~500
*/
int leftNum = a.length;
int countNum = 0;
int index = 0;
// 循环数数,直到只剩下一个人,即leftNum的值为1
while (leftNum > 1) {
if (a[index] == true) {
countNum++;
if (countNum == 3) {
countNum = 0;
a[index] = false;
leftNum--;
}
}
index++;
// 如果数到第500个人,index回0,又从第1个人开始数
if (index == a.length) {
index = 0;
}
}
//循环遍历每个数数的人,找到值为true的并输出
for (int i = 0; i < a.length; i++) {
if (a[i] == true)
System.out.println("最后剩下的人是第" + (i + 1) + "的人");
}
}
}
运行结果如下图所示。