計算機17-1,2 容器測試

一、單選題

1、

答案:C

解析:compareTo是String類中定義的方法,不是Collection中定義的


2、

答案:A


3、

答案:C

解析:一般帶有Tree的類都是可以進行排序的


二、編程題

1、jmu-Java-05集合-01-ArrayListIntegerStack (30 分)

定義IntegerStack接口,該接口描述了一個存放Integer的棧的常見方法:

public Integer push(Integer item); //如item爲null,則不入棧直接返回null。否則直接入棧,然後返回item。
public Integer pop();              //出棧,如棧爲空,則返回null。
public Integer peek();             //獲得棧頂元素,如棧頂爲空,則返回null。注意:不要出棧
public boolean empty();           //如過棧爲空返回true
public int size();                //返回棧中元素數量

定義IntegerStack的實現類ArrayListIntegerStack,內部使用ArrayList存儲。該類中包含:

構造函數:
在無參構造函數中新建ArrayList或者LinkedList,作爲棧的內部存儲。
思考:查詢JDK文檔,嘗試說明本題到底使用哪個List實現類最好。

方法:
public String toString() //用於輸出List中的內容,可直接調用List的toString()方法。可用System.out.println(list)進行輸出。

提示:

  1. 不建議使用top指針。最好直接複用List實現類中已有的方法。
  2. pop時應將相應的元素從列表中移除。

main方法說明

  1. 建立ArrayIntegerStack對象
  2. 輸入m個值,均入棧。每次入棧均打印入棧返回結果。
  3. 輸出: 棧頂元素,輸出是否爲空,然後輸出size.
  4. 輸出棧中所有元素(調用其toString()方法)
  5. 輸入x,然後出棧x次,每次均打印出棧的對象。
  6. 輸出:棧頂元素,輸出是否爲空,輸出size。注意:這裏的輸出棧頂元素,僅輸出、不出棧。
  7. 輸出棧中所有元素(調用其toString()方法)。注意:返回null,也要輸出。

思考:

如果使用LinkedList來實現IntegerStack,怎麼實現?測試代碼需要進行什麼修改?

輸入樣例

5
1 3 5 7 -1
2

輸出樣例

1
3
5
7
-1
-1,false,5
[1, 3, 5, 7, -1]
-1
7
5,false,3
[1, 3, 5]

 答案

import java.util.*;

public class Main 
{
	private ArrayList<Integer> l;
	public Main()
	{
		l=new ArrayList<Integer>();
	}
	public Integer push(Integer item) 
	{
		if(item==null)
			return null;
		else
		{
			l.add(item);
			return item;
		}
	}
	public Integer pop() 
	{
		if(l.size()==0)
			return null;
		else
		{
			int m=l.get(l.size()-1);
			l.remove(l.size()-1);
			return m;
		}
	}
	public Integer peek() 
	{
		if(l.size()==0)
			return null;
		else
		{
			return l.get(l.size()-1);
		}
	}
	public boolean empty() 
	{
		if(l.size()==0) return true;
		else return false;
	}
	public int size() 
	{
		return l.size();
	}
	public String toString()
	{
		return l.toString();
	}
	public static void main(String[] args) 
	{
		Main al=new Main();
		Scanner scan=new Scanner(System.in);
		int m=scan.nextInt();
		for(int i=0;i<m;i++)
		{
			int n=scan.nextInt();
			System.out.println(al.push(n));
		}
		if(al.size()!=0)
		{
			System.out.println(al.peek()+","+false+","+al.size());
		}
		else
		{
			System.out.println(al.peek()+","+true+","+al.size());
		}
		System.out.println(al.toString());
		int x=scan.nextInt();
		for(int i=0;i<x;i++)
		{
			System.out.println(al.pop());
		}
		if(al.size()!=0)
		{
			System.out.println(al.peek()+","+false+","+al.size());
		}
		else
		{
			System.out.println(al.peek()+","+true+","+al.size());
		}
		System.out.println(al.toString());
	}
}

注意

(1)OJ提交的時候,類名只能是Main,不能帶implement,不能帶包,@SuppressWarnings("unused")一律不能加

(2)在這個題中,新建ArrayList的時候,有兩種方式

方式一:

private ArrayList<Integer> l;
public Main()
{
	l=new ArrayList<Integer>();
}

方式二(不建議使用):

private ArrayList<Integer> l=new ArrayList<Integer>();
public Main()
{
	ArrayList<Integer> l=new ArrayList<Integer>();
}

錯誤形式:

private ArrayList<Integer> l;
public Main()
{
	ArrayList<Integer> l=new ArrayList<Integer>();
}

錯誤原因:這相當於又新建了一個變量,原來的變量還是空的。在構造函數中新建了一個變量,雖然與外部private中的l名稱相同,但是實際上是兩個對象,這樣會導致下面用的時候,出現空指針異常,因爲相當於沒有new一塊空間。

(3)這裏remove的只能是某一個元素,不能是某個位置的元素


2、jmu-Java&Python-統計一段文字中的單詞個數並按單詞的字母順序排序後輸出 (40 分)

現需要統計若干段文字(英文)中的不同單詞數量。
如果不同的單詞數量不超過10個,則將所有單詞輸出(按字母順序),否則輸出前10個單詞。

注1:單詞之間以空格(1個或多個空格)爲間隔。
注2:忽略空行或者空格行。
注3:單詞大小寫敏感,即'word'與'WORD'是兩個不同的單詞 。

輸入說明

若干行英文,最後以!!!!!爲結束。

輸出說明

不同單詞數量。 然後輸出前10個單詞(按字母順序),如果所有單詞不超過10個,則將所有的單詞輸出。

輸入樣例

Failure is probably the fortification in your pole
It is like a peek your wallet as the thief when you
are thinking how to spend several hard-won lepta
when you Are wondering whether new money it has laid
background Because of you, then at the heart of the
most lax alert and most low awareness and left it
godsend failed
!!!!!

輸出樣例

49
Are
Because
Failure
It
a
alert
and
are
as
at

答案

import java.util.*;

public class Main 
{
	public static void main(String[] args) 
	{
		TreeSet<String> t=new TreeSet<String>();
		Scanner scan=new Scanner(System.in);
		String s=scan.next();
		while(s.equals("!!!!!")!=true)
		{
			t.add(s);
			s=scan.next();
		}
		System.out.println(t.size());
		if(t.size()<=10)
		{
			int n=t.size();
			for(int i=0;i<n;i++)
			{
				System.out.println(t.first());
				t.remove(t.first());
			}
		}
		else
		{
			for(int i=0;i<10;i++)
			{
				System.out.println(t.first());
				t.remove(t.first());
			}
		}
	}
}

 注意

(1)remove只能移除某個元素,而不能移除某個位置的元素,不能是位置,之能是對象

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章