1、将下列数据:“hello”、123、6.9、“hello”、“”、“Hello”、StringBuffer s=new StringBuffer(“hello”)中的s,添加到一个ArrayList对象中。。
• 将ArrayList中的所有元素打印输出。
• 查找元素“hello”。
• 删除指定的元素“hello”。
• 将元素123替换为1000。
import java.util.*;
public class S6_1 {
public static void main(String[] args) {
ArrayList list= new ArrayList();
StringBuffer s=new StringBuffer("hello");
list.add("hello");
list.add(123);
list.add(6.9);
list.add("hello");
list.add("");
list.add("Hello");
list.add(s);
//将ArrayList中的所有元素打印输出。
System.out.println("将ArrayList中的所有元素打印输出:");
Iterator it=list.iterator();
while(it.hasNext()){
System.out.print(it.next()+" ");
}
System.out.println();
//查找元素“hello”
ArrayList index = new ArrayList();
for(int i =0;i<list.size();i++){
if(list.get(i).equals("hello")){
index.add(i);
}
}
System.out.println("hello在list中的下标为:"+index);
//删除指定的元素“hello”
list.remove("hello");
list.remove("hello");
Iterator it1=list.iterator();
while(it1.hasNext()){
System.out.print(it1.next()+" ");
}
System.out.println();
//将元素123替换为1000
for(int i = 0 ;i< list.size();i++){
if(list.get(i).equals(123)){
list.set(i,1000);
}
}
Iterator it2=list.iterator();
while(it2.hasNext()){
System.out.print(it2.next()+" ");
}
System.out.println();
}
}
2、使用ArrayList集合,向集合中添加10个整数,并使用Iterator遍历该集合,并查找键盘输入的元素。提示:
• 使用add()方法将元素添加到ArrayList集合中。
• 调用集合的iterator()方法获得Iterator对象,并调用Iterator的hasNext()和next()方法,迭代出集合中的所有元素,完成查找功能,并将重复的元素删除。
import java.util.*;
public class S6_2 {
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);//[0,0,1,1,2,2,3,3,4,4]
//调用集合的iterator()方法获得Iterator对象.完成查找功能
Iterator it=list.iterator();
Scanner sca=new Scanner(System.in);
System.out.print("请输入要查找的数据:");
int n =sca.nextInt(),i=0;
while(it.hasNext()){
i++;
if(it.next().equals(n)){
System.out.println("元素"+n+"的位置是:"+i);
}
}
//将重复的元素删除
Iterator<Integer> iterator = list.iterator();
while (iterator.hasNext()){
int num = iterator.next();//得到当前元素
int count = 0;//计数
//遍历集合,找到相同元素增加计数
for(i = 0;i< list.size();i++){
if(list.get(i).equals(num)){
count++;
}
}
//利用iterator的remove()把当前元素删除
if(count>1){
iterator.remove();
}
}
System.out.println("去除重复元素后: "+list);
}
}
3、分别利用Arraylist和Set随机生成十个不重复的随机整数,随机整数范围为350到450。
import java.util.*;
public class S6_3 {
public static void main(String[] args) {
/*分别利用Arraylist和Set随机生成十个不重复的随机整数,随机整数范围为350到450。*/
Random random = new Random();
//Arraylist随机生成十个不重复的随机整数
ArrayList<Integer> list = new ArrayList<>();
while (list.size()<10){
int num = random.nextInt(350,450);
if(!list.contains(num)){
list.add(num);
}
}
System.out.println("Arraylist的十个随机数:"+list);
//Set随机生成十个不重复的随机整数
Set<Integer> set = new HashSet<>();
while (set.size()<10){
int num = random.nextInt(350,450);
set.add(num);
}
System.out.println("Set的十个随机数: "+set);
}
}
4、集合中不容许有重复的对象,对于多个重复对象只能添加一次。例如在HashSet集合中添加三个Person对象,把姓名相同的人当做同一个人,虽然可以添加多次但集合里只保留一个,但是这对类的设计是有要求的,假设Person类中只包含name和age属性,则需要重写hashCode()方法和equals()方法,如果两个对象的name相同,则hashCode()方法的返回值相同,equals()方法返回true。
import java.util.*;
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;
}
public String toString() {
return "{" + "name=" + name + ", age=" + age + "}";
}
public int hashCode() {
//返回name的hashcode
return name.hashCode();
}
public boolean equals(Object obj) {
//首先直接判断传入的obj与this当前对象是否相等
//相等则直接return true
if (this == obj) {
return true;
}
//判断传入的obj是否为Person类,不同则肯定不相等false
if (!(obj instanceof Person)) {
return false;
}
//将传入的obj转为Person类,再判断名字是否相等
Person p = (Person) obj;
return this.name.equals(p.name);
}
}
5、编写程序将一组学生对象的姓名和成绩存入到一个树集(TreeSet)中,完成以下要求:
• 使得按照成绩自动降序排列,并输出排序的结果。
import java.util.*;
public class S6_5 {
public static void main(String[] args) {
Student stu1 = new Student("张三", 78);
Student stu2 = new Student("李四", 96);
Student stu3 = new Student("王五", 65);
TreeSet tset = new TreeSet();
tset.add(stu1);
tset.add(stu2);
tset.add(stu3);
System.out.println(tset);
}
}
class Student implements Comparable <Student>{
String name;
int score;
public Student(String name, int score) {
this.name = name;
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", score=" + score +
'}';
}
@Override
public int hashCode() {
return super.hashCode();
}
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
@Override
public 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都应该输出。
提示:可以利用集合的元素不能重复这一特性。
import java.util.*;
public class S6_6 {
public static void main(String[] args)
{
Scanner sca=new Scanner(System.in);
//hashMap用来存放数字以及它对应的频率
HashMap<Integer,Integer> hashMap=new HashMap<Integer,Integer>();
System.out.println("请输入数据");
while (true)
{
int num = sca.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;
//得到频率最高为多少即max
Collection<Integer> values = hashMap.values();
for(int num : values){
//先将第一个value赋给max
if(count==0){
max = num;
count++;
}
if(num>max){
max = num;
}
}
System.out.println("频率最高的数字为:");
//得到hashMap对应key的集合
Set<Integer> key = hashMap.keySet();
//遍历key的值再比较其对应的value值,如果value=key,输出key
for(int num : key){
if(hashMap.get(num)==max){
System.out.println(num);
}
}
}
}
7、选择合适的Map集合保存5个用户的用户名和密码,然后将这些键值对打印出来。
import java.util.*;
public class S6_7 {
public static void main(String[] args) {
HashMap hashMap = new HashMap();
hashMap.put("001","111");
hashMap.put("002","222");
hashMap.put("003","333");
hashMap.put("004","444");
hashMap.put("005","555");
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.,;:!?()]")方法进行分词。
import java.util.*;
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;i<split.length;i++){
if(!(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)实现。
import java.util.*;
//集合
//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<Integer> loop = new LinkedList<Integer>() ;//保存元素用于计算
//n个元素入队loop
for(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);
}
}