计算机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只能移除某个元素,而不能移除某个位置的元素,不能是位置,之能是对象

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值