------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
自己不太懂的小题
1、请编写程序,统计一个字符串中每个字符出现的次数
答案:
public class TreeMapTest {
publicstatic void main(String[] args) {
//1:定义字符串 "aababcabcdabcde"
Stringstr = "aababcabcdabcde";
//2:创建一个TreeMap集合。用来存储每个字符与它出现次数
TreeMap
//3:遍历字符串,得到每一个字符
for(int i = 0; i < str.length(); i++) {
charch = str.charAt(i);
//4:判断该字符在集合中是否存在
if(map.containsKey(ch)) {
//存在
//a:获取该字符对应的次数
intcount = map.get(ch);
//b:次数+1
count++;
//c:再把该字符与新的次数进行存储
map.put(ch,count);
}else {
//不存在:
//a:把该字符与次数1 存储到集合中
map.put(ch,1);
}
}
//5:遍历集合,得到字符与对应的次数
//a:获取所有的键值对元素对象
Set
//b:获取每一个键值对元素对象
StringBuildersb = new StringBuilder();//用来拼接字符串
for(Entry
//获取键 获取值
Characterch = entry.getKey();
Integercount = entry.getValue();
//6;拼接成 指定的字符串形式 a(5)b(4)c(3)d(2)e(1)
sb.append(ch).append("(").append(count).append(")");
}
//打印
System.out.println(sb.toString());
}
}
2、请编写程序,存储自定义对象到HashMap集合中,并采用两种方式遍历
答案:
class Teacher {
privateString name;
privateint age;
publicTeacher() {
super();
}
publicTeacher(String name, int age) {
super();
this.name= name;
this.age= age;
}
publicString getName() {
returnname;
}
publicvoid setName(String name) {
this.name= name;
}
publicint getAge() {
returnage;
}
publicvoid setAge(int age) {
this.age= age;
}
@Override
publicint hashCode() {
finalint prime = 31;
intresult = 1;
result= prime * result + age;
result= prime * result + ((name == null) ? 0 : name.hashCode());
returnresult;
}
@Override
publicboolean equals(Object obj) {
if(this == obj)
returntrue;
if(obj == null)
returnfalse;
if(getClass() != obj.getClass())
returnfalse;
Teacherother = (Teacher) obj;
if(age != other.age)
returnfalse;
if(name == null) {
if(other.name != null)
returnfalse;
}else if (!name.equals(other.name))
returnfalse;
returntrue;
}
}
public class HashMapTest {
publicstatic void main(String[] args) {
//创建集合对象
HashMap
//添加元素
hm.put(newTeacher("小曼老师", 18),"itcast001");
hm.put(newTeacher("范老师", 28),"itcast002" );
hm.put(newTeacher("邹老师", 32),"itcast003");
hm.put(newTeacher("邹老师", 32),"itcast004");
//遍历
//键找值
Set
for(Teacher t : keys) {
Stringvalue = hm.get(t);
System.out.println(value+"--"+ t.getName() + "--" + t.getAge());
}
//键值对元素,找键找值
//a: 获取所有的键值对对象
Set
//b: 获取到每一个键值对元素对象
for(Entry
//通过键值对元素对象,找键找值
Teachert = entry.getKey();
Stringvalue = entry.getValue();
System.out.println( value +"--"+ t.getName() + "--"+ t.getAge());
}
}
}
3、请编写程序,存储自定义对象到TreeMap集合中,并采用两种方式遍历
答案:
class Student {
privateString name;
privateint age;
publicStudent() {
super();
}
publicStudent(String name, int age) {
super();
this.name= name;
this.age= age;
}
publicString getName() {
returnname;
}
publicvoid setName(String name) {
this.name= name;
}
publicint getAge() {
returnage;
}
publicvoid setAge(int age) {
this.age= age;
}
}
public class TreeMapDemo2 {
publicstatic void main(String[] args) {
//创建集合对象
TreeMap
@Override
publicint compare(Student s1, Student s2) {
//姓名比较排序
intnum = s1.getName().compareTo(s2.getName());
//年龄比较排序
intnum2 = (num==0) ? (s1.getAge() - s2.getAge()) : num;
returnnum2;
}
});
//添加元素
tm.put(newStudent("z周瑜老师", 28),"itcast110");
tm.put(newStudent("f范老师", 27),"itcast114");
tm.put(newStudent("x小曼老师", 17),"itcast911");
tm.put(newStudent("x小曼老师", 18),"itcast911");
//遍历
//键找值
Set
for(Student s : keys) {
Stringvalue = tm.get(s);
System.out.println(value + "..."+ s.getName() +"..."+s.getAge() );
}
//键值对找键找值
Set
for(Entry
Students = entry.getKey();
Stringvalue = entry.getValue();
System.out.println(value + "..."+ s.getName() +"..."+s.getAge() );
}
}
}
4、请编写程序,完成集合嵌套,并遍历
传智播客
jc 基础班
张三 20
李四 22
jy 就业班
王五 21
赵六 23
答案:
class Student {
privateString id;
privateString name;
publicStudent() {
super();
}
publicStudent(String id, String name) {
super();
this.id= id;
this.name= name;
}
publicString getId() {
returnid;
}
publicvoid setId(String id) {
this.id= id;
}
publicString getName() {
returnname;
}
publicvoid setName(String name) {
this.name= name;
}
}
HashMap嵌套HashMap
public class HashMapTest {
publicstatic void main(String[] args) {
//1:创建基础班集合 HashMap
HashMap
//2: 向基础班集合添加元素
base.put("01","陈如水");
base.put("02","左帅");
//3:创建就业班集合 HashMap
HashMap
//4:向就业班集合添加元素
job.put("01","李天成");
job.put("02","许伟");
//5:创建一个czbk集合 HashMap< String, HashMap
HashMap
//6:向czbk集合中添加班级元素
czbk.put("基础班", base);
czbk.put("就业班", job);
//7:遍历
//遍历学校集合,得到每一个班级
Set
//得到每一个班级
for(Entry
//获取班级名称
Stringname = clazz.getKey();
System.out.println(name);
//获取班级元素
HashMap
//获取到每一个学生信息
Set
for(Entry
//获取学生的学号
Stringid = student.getKey();
//获取学生的姓名
Stringn = student.getValue();
System.out.println("\t"+id+"..."+n);
}
}
}
}
HashMap嵌套ArrayList
public classHashMapTest {
publicstatic void main(String[] args) {
//1:创建基础班集合 ArrayList
ArrayList
//2:向基础班集合添加元素
base.add(newStudent("01","陈如水"));
base.add(newStudent("02","左帅"));
//3:创建就业班集合 ArrayList
ArrayList
//4:向就业班集合添加元素
job.add(newStudent("01","李天成"));
job.add(newStudent("02","许伟"));
//5:创建czbk学校集合 HashMap
HashMap
//6:把班级添加到学校集合中
czbk.put("基础班", base);
czbk.put("就业班", job);
//7:遍历
//获取到所有班级的名称
Set
//获取到每一个班级名称
for(String className : classNames) {
System.out.println(className);
//通过班级名称,获取到班级学生信息
ArrayList
//获取到每一个学生信息
for(Students : Students){
System.out.println("\t"+s.getId()+"..."+s.getName());
}
}
}
}
ArrayList嵌套HashMap
public class ArrayListTest {
publicstatic void main(String[] args) {
//1:创建基础班集合 HashMap
HashMap
//2: 向基础班集合添加元素
base.put("01","陈如水");
base.put("02","左帅");
//3:创建就业班集合 HashMap
HashMap
//4:向就业班集合添加元素
job.put("01","李天成");
job.put("02","许伟");
//5:创建一个czbk集合 ArrayList
ArrayList
//6:向czbk集合中添加班级元素
czbk.add(base);
czbk.add(job);
//7:遍历(难点)
//获取到每一个班级
for(HashMap
//获取到班级的所有学生
Set
//获取到每一个学生
for(Entry
Stringid = student.getKey();
Stringname = student.getValue();
System.out.println(id+"--"+name );
}
}
}
}
5、请编写程序,完成模拟斗地主案例
答案:
public class PokerDemo {
publicstatic void main(String[] args) {
//创建一个HashMap集合
HashMap
//创建一个ArrayList集合
ArrayList
//创建花色数组和点数数组
//定义一个花色数组
String[]colors = { "♠", "♥", "♣", "♦" };
//定义一个点数数组
String[]numbers = { "3", "4", "5", "6","7", "8", "9", "10", "J","Q",
"K","A", "2", };
//从0开始往HashMap里面存储编号,并存储对应的牌,同时往ArrayList里面存储编号即可。
intindex = 0;
for(String number : numbers) {
for(String color : colors) {
Stringpoker = color.concat(number);
hm.put(index,poker);
array.add(index);
index++;
}
}
hm.put(index,"小王");
array.add(index);
index++;
hm.put(index,"大王");
array.add(index);
//洗牌(洗的是编号)
Collections.shuffle(array);
//发牌(发的也是编号,为了保证编号是排序的,就创建TreeSet集合接收)
TreeSet
TreeSet
TreeSet
TreeSet
for(int x = 0; x < array.size(); x++) {
if(x >= array.size() - 3) {
diPai.add(array.get(x));
}else if (x % 3 == 0) {
sunQuan.add(array.get(x));
}else if (x % 3 == 1) {
caoCao.add(array.get(x));
}else if (x % 3 == 2) {
liuBei.add(array.get(x));
}
}
//看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌)
lookPoker("孙权", sunQuan, hm);
lookPoker("曹操", caoCao, hm);
lookPoker("刘备", liuBei, hm);
lookPoker("底牌", diPai, hm);
}
//写看牌的功能
publicstatic void lookPoker(String name, TreeSet
HashMap
System.out.print(name+ "的牌是:");
for(Integer key : ts) {
Stringvalue = hm.get(key);
System.out.print(value+ " ");
}
System.out.println();
}
}