浙江万里学院实验报告
课程名称: 面向对象程序设计
实验名称:实验 九 Java集合类
一、实验目的:
掌握典型的Java集合类的使用方法
二、实验内容:
- 使用LinkedList实现一个模拟KTV点歌系统的程序。
2.请使用Map实现获取字符串“bwaerbctyxbacecrtdcvr”
三、实验步骤及结果:
实验一:
1)源代码:
import java.util.LinkedList;
import java.util.Scanner;
public class txt2 {
private static void show(LinkedList song) {
for (int i = 0; i < song.size(); i++) {
System.out.print(i + “:”);
System.out.println(song.get(i) + " ");
}
}
public static void main(String[] args) {
System.out.println(“A:添加歌曲到列表”);
System.out.println(“B:将歌曲置顶”);
System.out.println(“C:将歌曲前移一位”);
System.out.println(“D:退出”);
System.out.println(“初始歌曲集:”);
LinkedList song = new LinkedList();
song.add(“关不上的窗-周传雄”);
song.add(“李白-李荣浩”);
song.add(“安和桥-宋冬野”);
song.add(“当你老了-赵照”);
song.add(“父亲写的散文诗-李建”);
song.add(“再度重相逢-孙露”);
song.add(“男孩-梁博”);
song.add(“成都-赵雷”);
show(song);
while (true) {
System.out.print(“请输入要执行的序号【A-D】:”);
Scanner a = new Scanner(System.in);
String b = a.nextLine();
switch (b) {
case “A”: {
System.out.print(“请输入要增加的歌曲:”);
String a1= new Scanner(System.in).nextLine();
song.add(a1);
System.out.println(“已添加的歌曲:” + a1);
System.out.println(“当前歌曲列表:”);
show(song);
break;
}
case “B”: {
System.out.print(“请输入要置顶的歌曲的序号:”);
Scanner a2= new Scanner(System.in);
String a22= (String) song.get(a2.nextInt());
int x = song.indexOf(a22);
if (x < 0) {
System.out.println(“你输入的序号的歌曲不存在!”);
} else {
song.remove(a22);
song.addFirst(a22);
System.out.println(“当前歌曲列表:”);
show(song);
}
break;
}
case “C”: {
System.out.print(“请输入要前移一位的歌曲的序号:”);
Scanner a3= new Scanner(System.in);
String a33= (String) song.get(a3.nextInt());
int y = song.indexOf(a33);
if (y < 0) {
System.out.println(“你输入的序号的歌曲不存在!”);
} else {
if (y == 0) {
System.out.println(“你输入的序号的歌曲已经在第一位了!”);
} else {
song.remove(a33);
song.add(y - 1, a33);
System.out.println(“当前歌曲列表:”);
show(song);
}
}
break;
}
case “D”: {
System.out.println(“你已经退出!”);
System.exit(1);
break;
}
}
}
}
}
2)运行结果:
实验二:
1)源代码:
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
public class txt1 {
public static void main(String[] args) {
String str = new String(“bwaerbctyxbacecrtdcvr”);
String s = getCharCount(str);
System.out.println(s);
}
public static String getCharCount(String str) {
char[] chs = str.toCharArray();
Map map = new TreeMap();
for (int i = 0; i < chs.length; i++) {
if (!(chs[i] >= ‘a’ && chs[i] <= ‘z’ || chs[i] >= ‘A’ && chs[i] <= ‘Z’))
continue;
Integer value = (Integer) map.get(chs[i]);
int count = 0;
if (value != null) {
count = value;
}
count++;
map.put(chs[i], count);
}
return mapToString(map);
}
private static String mapToString(Map<Character, Integer> map) {
StringBuilder sb = new StringBuilder();
Iterator it = map.keySet().iterator();
while (it.hasNext()) {
Character key = (Character) it.next();
Integer value = map.get(key);
sb.append(key + “(” + value + “)”);
}
return sb.toString();
}
}
2)运行截图:
五 实验总结:
List(列表):有序、可重复元素,可以插入多个null元素。实现List接口的常用类有LinkedList,ArrayList,Vector,Stack。
Set(集):无序、不可重复元素,最多有一个null元素。
Map(映射):不是collection的子接口或者实现类,Map是一个接口。Map 接口最流行的几个实现类是 HashMap、LinkedHashMap、Hashtable 和 TreeMap。