本次我在每个实验题下面都简单的写了一下思路解析,希望同学们先看看思路解析后尽量尝试自己做一下,不要只是简单的Ctrl c + Ctrl v,如果有疑惑的地方也欢迎找我来探讨呀🎈。
1、将下列数据:“hello”、123、6.9、“hello”、“”、“Hello”、StringBuffer s=new StringBuffer(“hello”)中的s,添加到一个ArrayList对象中。。
• 将ArrayList中的所有元素打印输出。
• 查找元素“hello”。
• 删除指定的元素“hello”。
• 将元素123替换为1000。
思路解析:
源代码:
public static void main(String[] args) {ArrayList
2、使用ArrayList集合,向集合中添加10个整数,并使用Iterator遍历该集合,并查找键盘输入的元素。提示:
• 使用add()方法将元素添加到ArrayList集合中。
• 调用集合的iterator()方法获得Iterator对象,并调用Iterator的hasNext()和next()方法,迭代出集合中的所有元素,完成查找功能,并将重复的元素删除。
思路解析:
源代码:
public static void main(String[] args) {ArrayList List = new ArrayList<>();//使用ArrayList集合,向集合中添加10个整数,for(int i = 0;i<5;i++){List.add(i);List.add(i);}System.out.println(List);//list:[0, 0, 1, 1, 2, 2, 3, 3, 4, 4]//查找功能Scanner sc=new Scanner(System.in);System.out.println("请输入要查找的数据:");int n =sc.nextInt();for (int i = 0; i < List.size(); i++) {if(n==List.get(i)){System.out.println(n+"的位置在:"+i);}}//将重复的元素删除Iterator iterator = List.iterator();while (iterator.hasNext()){int num = iterator.next();//得到当前元素int count = 0;//计数//遍历集合,找到相同元素增加计数for(int i = 0;i< List.size();i++){if(List.get(i)==num){count++;}}//利用iterator的remove()把当前元素删除if(count>1){iterator.remove();}}System.out.println("去除重复元素后: "+List);
3、分别利用Arraylist和Set随机生成十个不重复的随机整数,随机整数范围为350到450。
思路分析:
源代码:
public static void main(String[] args) {/*分别利用Arraylist和Set随机生成十个不重复的随机整数,随机整数范围为350到450。*/Random random = new Random();ArrayList list = new ArrayList<>();while (list.size()<10){int num = random.nextInt(350,450);//判断当前list中是否已经包含生成的随机数if(!list.contains(num)){list.add(num);}}System.out.println("Arraylist的十个随机数:"+list);Set set = new HashSet<>();while (set.size()<10){int num = random.nextInt(350,450);//因为Set本事内部已经去重,无法添加相同的元素所以这里不需要再判断set.add(num);}System.out.println("Set的十个随机数: "+set);
}
4、集合中不容许有重复的对象,对于多个重复对象只能添加一次。例如在HashSet集合中添加三个Person对象,把姓名相同的人当做同一个人,虽然可以添加多次但集合里只保留一个,但是这对类的设计是有要求的,假设Person类中只包含name和age属性,则需要重写hashCode()方法和equals()方法,如果两个对象的name相同,则hashCode()方法的返回值相同,equals()方法返回true。
思路解析:
HashSet在存储元素的过程中首先会去调用元素的hashCode()值,
注意相同元素的hashCode()值一定相同,但hashCode()值相同元素不一定相同,
然后看当前存放的元素的哈希值与已经存入HashSet的元素的哈希值是否相同。
如果不同 :就直接添加到集合;
如果相同 :则继续调用元素的equals() 和哈希值相同的这些元素依次去比较。如果说有返回true的,那就重复不添加;如果说比较结果都说false,那就是不重复就添加。
源代码:
public class S6_4 {public static void main(String[] args) {HashSet hs=new HashSet();Person p1=new Person("张三",18);Person p4=new Person("李四",20);Person p2=new Person("王五",19);Person p3=new Person("张三",18);hs.add(p1);hs.add(p2);hs.add(p3);hs.add(p4);System.out.println(hs);}
}
class Person{String name;int age;public Person(String name, int age) {this.name = name;this.age = age;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +'}';}@Overridepublic int hashCode() {//返回name的hashcodereturn name.hashCode();}@Overridepublic boolean equals(Object obj) {//首先直接判断传入的obj与this当前对象是否相等//相等则直接return trueif(this==obj){return true;}//判断传入的obj是否为Person类,不同则肯定不相等falseif(!(obj instanceof Person)){return false;}//将传入的obj转为Person类,再判断名字是否相等Person p=(Person) obj;return this.name.equals(p.name);}
5、编写程序将一组学生对象的姓名和成绩存入到一个树集(TreeSet)中,完成以下要求:
• 使得按照成绩自动降序排列,并输出排序的结果。
思路解析
源代码:
public class S6_5 {public static void main(String[] args) {Student stu1 = new Student("张三", 88);Student stu2 = new Student("李四", 99);Student stu3 = new Student("王五", 77);TreeSet tset = new TreeSet();tset.add(stu1);tset.add(stu2);tset.add(stu3);System.out.println(tset);}
}class Student implements Comparable {String name;int score;public Student(String name, int score) {this.name = name;this.score = score;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", score=" + score +'}';}@Overridepublic int hashCode() {return super.hashCode();}@Overridepublic boolean equals(Object obj) {return super.equals(obj);}@Overridepublic int compareTo(Student student) {//加负号为降序,不加为升序return -Integer.compare(this.score, student.score);}
}
6、编写一个程序,读取个数不定的整数,然后查找其中出现频率最高的数字。要求通过键盘输入数据,当输入为0时,表示结束输入。如: 如果输入的数据是2 3 40 3 54 -3 3 3 2 0,那么数字3的出现频率是最高的。如果出现频率最高的数字不是一个而是多个,则应该将它们全部输出。例如当数据是9 30 3 9 3 2 4时,3和9都出现了两次,3和9都应该输出。
提示:可以利用集合的元素不能重复这一特性。
思路解析:
源代码:
public class S6_6 {public static void main(String[] args){Scanner reader=new Scanner(System.in);//hashMap用来存放数字以及它对应的频率HashMap hashMap=new HashMap();System.out.println("请输入数据");while (true){int num = reader.nextInt();if (num == 0){break;}//如果当前集合中没有该元素,则hashMap.put(num,1);if(!(hashMap.containsKey(num))){hashMap.put(num,1);}else {//如果已经存在该元素,将其value加一int value = hashMap.get(num);value+=1;hashMap.put(num,value);}}int max = 0;int count = 0;//得到频率最高为多少即maxCollection values = hashMap.values();for(int num : values){//先将第一个value赋给maxif(count==0){max = num;count++;}if(num>max){max = num;}}System.out.println("当当当当~频率最高的数字如下:");//得到hashMap对应key的集合Set key = hashMap.keySet();//遍历key的值再比较其对应的value值,如果value=key,输出keyfor(int num : key){if(hashMap.get(num)==max){System.out.println(num);}}}}
7、选择合适的Map集合保存5个用户的用户名和密码,然后将这些键值对打印出来。
思路解析:
源代码:
public class S6_7 {public static void main(String[] args) {HashMap hashMap = new HashMap();hashMap.put("001","123");hashMap.put("002","234");hashMap.put("003","345");hashMap.put("004","456");hashMap.put("005","567");Set entrySet = hashMap.entrySet();Iterator iterator = entrySet.iterator();while (iterator.hasNext()){System.out.println(iterator.next());}}
}
8、(选做)统计字符串中每个单词出现的次数,使用HashMap来实现。例如:“Today, We have a class of java, as we kown, java is an object oriented programming language, and java is fun! wish you enjoy it!”,统计结果存储成以下形式:
a–>1
an–>1
and–>1
as–>1……
is–>2
提示:使用String.split((“[ \n\t\r.,;:!?()]”)方法进行分词。
思路分析:
源代码:
public class S6_8 {public static void main(String[] args) {String str = new String("Today,We have a class of java,as we kown,java is an object oriented programming language,and java is fun!wish you enjoy it!");//首先将一大串句子中的每个单词通过split转化为string数组String split[] = str.split(("[ \n\t\r.,;:!?()]"));//hashmap用来存key==value键值对,其中key为字母,value为该字母出现次数HashMap hashMap = new HashMap();for(int i = 0;iif(!(hashMap.containsKey(split[i]))){hashMap.put(split[i],1);}else {int value = (int)hashMap.get(split[i]);value++;hashMap.put(split[i],value);}}//普普通通的遍历结束Set entrySet = hashMap.entrySet();Iterator iterator = entrySet.iterator();while (iterator.hasNext()){System.out.println(iterator.next());}}
}
9、(选做)500个人围成一个圈,从1开始报数,数到3的倍数的人离开圈子,循环往复,直到最后圈子只剩下一人为止,求剩下的人原来在圈子的位置。
提示:可以使用集合(ArrayList)或队列(Deque)实现。
解题思路:
源代码:
public class S6_9 {public static void main(String[] args) {ArrayList arrayList = new ArrayList();//添加五百个人for(int i = 1;i<=500;i++){arrayList.add(i);}int i = 0;//集合的下标int count = 0;//报数while (arrayList.size()>1){count++;//报数加一if(count==3){arrayList.remove(i);i--;//如果这个人离开了,注意i--count=0;//报数归零}//如果到最后一个人了,i归零继续循环if(i==arrayList.size()-1){i = 0;}else {i++;}}System.out.println("最后一个人为:"+arrayList);}
}
public class S6_9 {public static void main(String[] args) {Queue loop = new LinkedList() ;//保存元素用于计算//n个元素入队loopfor(int i=1;i<=500;i++) {loop.offer(i);}int count = 0;//计数,逢3出队int num = 0;while (loop.size()>1){//先出队,得到当前出队的元素,//如果count=3,不再入队,否则入队到队尾,实现循环num = loop.poll();count++;if(count==3) {count=0;}else {loop.offer(num);}}//loop最后只剩一个元素System.out.println(loop);}}
🎉文章到这里就结束了,感谢诸佬的阅读。🎉
💕欢迎诸佬对文章加以指正,也望诸佬不吝点赞、评论、收藏加关注呀😘