#Java學習#習題四

其他JAVA學習的內容見:目錄

單選題

1.下面關於數組聲明和初始化的語句那個有語法錯誤?

A.int a1[]={3,4,5};
B.String a2[]={"string1","string1","string1"};
C.String a3[]=new String(3);
D.int[][] a4=new int[3][3];

C
A是靜態賦值D是動態賦值
[]在int後面和在a1後面都是對的,在裏面不能有數值
C應改爲:String a3[]=new String[3];
二維數組賦值,右面兩個中括號裏面,第一個必須不爲空,第二個可空可不空

2.list是一個ArrayList的對象,哪個選項的代碼填寫到//todo delete處,可以在Iterator遍歷的過程中正確並安全的刪除一個list中保存的對象?( )

        Iterator it = list.iterator();
        int index = 0;
        while (it.hasNext()){ 
              Object obj = it.next(); 
              if (needDelete(obj)) { //needDelete返回boolean,決定是否要刪除
                   //todo delete
               } 
              index ++;
        }

A.list.remove(obj);
B.list.remove(index);
C.list.remove(it.next());
D.it.remove();

D

3.下面哪個Set是按照插入順序排序的?

A.LinkedHashSet
B.HashSet
C.AbstractSet
D.TreeSet

A

4.定義了一維int型數組a[10]後,下面錯誤的引用是( ) 。

A.a[0]=1
B.a[10]=2
C.a[0]=5*2
D.a[1]=a[2]*a[0]

B,下標爲0-9

5.定義了int型二維數組a[6][7]後,數組元素a[3][4]前的數組元素個數爲( )

A.24
B.25
C.18
D.17

B

6.使用Iterator時,判斷是否存在下一個元素可以使用以下哪個方法?()

A.next()
B.hash()
C.hasPrevious()
D.hasNext()

D

7.在Java中,關於HashMap類的描述,以下選項錯誤的是

A.HashMap使用鍵/值得形式保存數據
B.HashMap能夠保證其中元素的順序
C.HashMap允許將null用作鍵
D.HashMap允許將null用作值

B

8.對於“boolean boo[] = new boolean[3];”,下列哪個敘述是正確的?

A.boo[0]、boo[1]、boo[2]的值都是0
B.boo[0]、boo[1]、boo[2]的值不確定
C.boo[0]、boo[1]、boo[2]的值都是false
D.boo[0]、boo[1]、boo[2]的值都是true

C
int默認0,double默認0.0,boolean類型默認false

9.執行完以下代碼int[ ] x = new int[25];後,以下哪項說明是正確的( )

A.x[24]爲0
B.x[24]未定義
C.x[25]爲0
D.x[0]爲空

A

10.以下語句不能通過編譯的是( )
A.int[] a={1,2,3,4};
B.int b[];
C.int c[]=new int[3];
D.int d=new int[];

D

填空題

1

請寫出以下程序運行結果:

 //環境 JDK 1.5及以上
 public static void main(String args[])
    {
    	Set<Integer> set=new TreeSet<Integer>();
    	List<Integer> list=new ArrayList<Integer>();//向下轉型
    	for (int i=-3;i<3;i++)
    	{
    		set.add(i);//自動解包,自動打包機制
    		list.add(i);
    	}
    	for (int i=0;i<3;i++)
    	{
    		set.remove(i);
    		list.remove(i);
    	}
    	System.out.println(set+" "+list);
    }

程序運行的輸出結果爲

[-3, -2, -1] [-2, 0, 2]

2

給出以下代碼:

public enum Main {//枚舉類型
  PLUS   { int eval(int x, int y) { return x + y; } },
  MINUS  { int eval(int x, int y) { return x - y; } },
  TIMES  { int eval(int x, int y) { return x * y; } },
  DIVIDE { int eval(int x, int y) { return x / y; } };
  abstract int eval(int x, int y);
  public static void main(String args[]) {
	  	int x = 4;
        int y = 2;
        for (Main op : Main.values())
            System.out.printf("%d %s %d = %d%n", x, op, y, op.eval(x, y));
    }
}

程序運行結果爲(一行一空):

4 PLUS 2 = 6

4 MINUS 2 = 2

4 TIMES 2 = 8

4 DIVIDE 2 = 2

3

使用Iterator遍歷集合時,首先需要調用( )方法判斷是否存在下一個元素,若存在下一個元素,則調用( )方法取出該元素

hasNext()

next()

4

Map集合中存儲元素需要調用( )方法,要想根據該集合的鍵獲取對應的值需要調用( )方法.

put()

get()

程序填空題

1

下列給定程序中,函數fun的功能是:計算N×N矩陣的主對角線元素和反向對角線元 素之和,並作爲函數值返回。要求先累加主對角線元素中的值,再累加反向對角線 元素中的值。

例如:若N=3,有下列矩陣:
1 2 3
4 5 6
7 8 9

首先累加1、5、9,然後累加3、5、7,函數返回值爲30。

public class Main {
	public static void main(String[] args) {
		int  t[][]={{21,2,13,24},{25,16,47,38},{29,11,32,54},{42,21,3,10}};
		System.out.println("The original data:");
		for(int i=0;i<t.length; i++){
			for(int j=0; j<t[i].length; j++)//第一個空
				System.out.printf("%4d",t[i][j]);
			System.out.println();
		}
		System.out.println("The result is:"+fun(t));
	}
	public static int fun(int  t[][]){
		int  sum;
		sum=0;//第二個空
		for(int i=0; i<t.length; i++)
			sum+=t[i][i];//第三個空
		for(int i=0; i<t.length; i++)
			sum+= t[i][t.length-i-1];//第四個空
		return sum;
	}
}

2

以下程序的功能是求一個二維數組中每行的最大值和每行的和。

輸入樣例
3
1 2 3
6 5 4
7 9 8

輸出樣例
1 2 3 3 6
6 5 4 6 15
7 9 8 9 24

import java.util.Scanner;

public class Main {
	
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);//第一個空
		int n=sc.nextInt();
		int a[][]=new int[n][n];
		int b[]=new int[n];
		int c[]=new int[n];
		for(int i=0;i<a.length;i++){
			for(int j=0;j<a[i].length;j++){//第二個空
				a[i][j]=sc.nextInt();
			}
		}		
		int max,s;
		for(int i=0;i<a.length;i++){
			max=a[i][0];	
			s=0;//第三個空
			for(int j=0;j<a[i].length;j++){
				if(a[i][j]>max){
					max=a[i][j];//第四個空
				}
				s+=a[i][j];
			}
			b[i]=max;
			c[i]=s;
		}
		for(int i=0;i<a.length;i++){
			for(int j=0;j<a[i].length;j++){
				System.out.printf("%3d",a[i][j]);//第五個空
			}
			System.out.printf("%3d%3d",b[i],c[i]);
			System.out.println();
		}
	}
}

函數題

jmu-Java-05集合-List中指定元素的刪除

編寫以下兩個函數

/*以空格(單個或多個)爲分隔符,將line中的元素抽取出來,放入一個List*/
public static List<String> convertStringToList(String line) 
/*在list中移除掉與str內容相同的元素*/
public static void remove(List<String> list, String str)

裁判測試程序:

public class Main {

    /*covnertStringToList函數代碼*/   
		
    /*remove函數代碼*/
		
     public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextLine()){
            List<String> list = convertStringToList(sc.nextLine());
            System.out.println(list);
            String word = sc.nextLine();
            remove(list,word);
            System.out.println(list);
        }
        sc.close();
    }

}

樣例說明:底下展示了4組測試數據。

輸入樣例

1 2 1 2 1 1 1 2
1
11 1 11 1 11
11
2 2 2
1
1 2 3 4 1 3 1
1

輸出樣例

[1, 2, 1, 2, 1, 1, 1, 2]
[2, 2, 2]
[11, 1, 11, 1, 11]
[1, 1]
[2, 2, 2]
[2, 2, 2]
[1, 2, 3, 4, 1, 3, 1]
[2, 3, 4, 3]

AC代碼

	/*covnertStringToList函數代碼*/   
	public static List<String> convertStringToList(String line){
		String[] s = line.split("\\s+");
		List<String> list = new ArrayList<String>();
		int i;
		for(i = 0; i < s.length; i++)
			list.add(s[i]);
		return list;
	}
	
    /*remove函數代碼*/
	public static void remove(List<String> list, String str){
		if(!list.contains(str))
			return;
		else
		{
			int i;
			for(i = 0; i < list.size(); i++)
			{
				if(list.get(i).equals(str)){
					list.remove(i);
					i--;
				}
			}
		}
	}

老師代碼

	/*covnertStringToList函數代碼*/   
	public static List<String> convertStringToList(String line){
		String[] lst = line.split(" ");//可以使用轉義字符:\\
		List<String> tmp = new ArrayList<String>();
		for(String a:lst){
			if(!a.equals(""))
				tmp.add(a);
		}
		return tmp;
	}
	
    /*remove函數代碼*/
	public static void remove(List<String> list, String str){
		Iterator it = list.iterator();
		while(it.hasNext()){
			String tmp = (String)it.next();
			if(tmp.equals(str)){
				it.remove();
			}
		}
	}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章