- 代码改错
- 写代码输出结果
- 画内存模型
- 补充代码填空
- 网络编程
- 面向对象
- 文件流
- 容器
- 多线程
- 异常
- 递归
- 常用类,特别是日期处理类
- 面向对象
- 容器
- 多线程
- 文件
随机生成150个1到100的数,请找出出现次数最多的那个数,并打印出该数在这些随机生成的100个数中排第几
说明:
- 相同的数排名相同
- 这100个数中最大的数排第一,以10个数为示例:1,3,3,3,3,6,7,8,8,9。其中出现最多的是3,排第5。
思路:
- 使用Map容器,Key为数值,Value为其所对应的次数。把随机数添加到此Map容器中。
- 把Map中的Key装到Set容器里面,然后把Map容器中Value最大的那个Key给找出来,这个Key对应的Value就是出现次数最多的那个数。
- 然后在Set集合里面把所有的值与找到的这个出现次数最多的数进行比较,定义一个遍历count,如果比这个数大,那么count加一,即可把这个数的排名得出。
在步骤2中,这里可以用Map中的KeySet方法把容器中的Key装到一个Set容器里面,然后遍历Set容器得到Key,用Map里的get()方法,通过Key所对应的Value,用Key把最大的那个Value找到,这个Key就是出现次数最多的那个数。
使用到的一些方法:
- getOrDefault(num1,num2)
此方法为Map容器中的方法。如果Map容器中存在num1这个Key,那么就返回这个Key所对应的Value,要不然就返回num2。
- KeySet()
此方法为Map容器中的方法,获得的是一个Set容器,内容为Map容器中的Key值。
public class number {
public static void main(String[] args) {
HashMap data=new HashMap();//Map中的key不可重复,key是数字,value是数字对应的次数
Random random= new Random();
for (int i=0;i<150;i++){
int number=1+random.nextInt(100);//1到101的数,不包括101,包括1
data.put(number,data.getOrDefault(number,0)+1);//加入数字,如果存在相同的key值,那么key的value+1,要不然就是0+1
}
Setkeyset=data.keySet();//把随机获取的那150个数装到Set容器里面,有重复的数出现,所以可能没有150个数
int maxCount=0;//出现次数最多的数所对应的次数
int targerNumber=0;//出现次数最多的那个数
for (Integer k:keyset){
System.out.println(k);
int v=data.get(k);//由key获取对应的value(次数)
if (v>maxCount){
maxCount=v;
targerNumber=k;
}
}
int count=1;
for (Integer k:keyset){
if (k>targerNumber)
count++;
}
System.out.println("最多的数是:"+targerNumber+"排名为:"+count);
}
}
例题2
计算祖国建国那天到现在有多少天
思路:
- 使用SimpleDateFormat类里面的parse()方法,把建国那天的String类型日期转换为Date。
- 创建一个Date,为现在的时间。
- 将今天距离基准时间的天数减去建国当天距离基准时间的天数,即可得到二者相差的天数。
使用到的方法:
- parse()
这是SimpleDateFormat类中的方法,作用是将String类型的数据转换为Date类型的数据。
注意,使用此方法要在main方法后加上throws ParseException
- getTime()
这是SimpleDateFormat类中的方法,返回的是距离基准时间( 1970年1月1日 00:00:00)的毫秒数,且是double类型的值。
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//创建转换日期格式类
Date nowDate=new Date();//创建一个现在的时间
String birthString="1949-10-01";//建国时间字符串
Date birthDate=sdf.parse(birthString);//把建国时间转换成Date类
System.out.println((int)((nowDate.getTime()-birthDate.getTime())*1.0/1000/60/60/24));
}
例题3
实现两个文件的内容交换,输入:待交换文件1的路径filePath,待交换文件2的路径otherPath,编写代码完成两个文件内容交换。
public class changeFile {
public static void change(String filePath1,String filePath2){
File file=new File(filePath1);//创建文件1 file
String fileName=file.getName();//获取文件1的名字
File other=new File(filePath2);//创建文件2 other
File tempFile=new File(fileName+"temp");//创建一个tempFile文件
System.out.println(file.toPath());//打印文件1的绝对路径
if (file.exists()&&other.exists()){
try{
Files.move(file.toPath(),tempFile.toPath());//先把文件1移到临时文件
Files.move(other.toPath(),file.toPath());//把文件2移动到文件1中
Files.move(tempFile.toPath(),other.toPath());//把临时性文件的内容移到文件2中
tempFile.deleteOnExit();
}catch (Exception e){
e.printStackTrace();
}
}
}
public static void main(String[] args) {
changeFile c1=new changeFile();
c1.change("d:/fileone.txt","d:/filetwo.txt");
}
}
使用到的方法:
- move(String path1,String path2)
此方法为Files类下的方法,作用是把path1路径文件中的内容移到path2路径的文件中
- toPath()
此方法为File类的方法,作用是获取文件或目录的绝对路径
- deleteOnExit()
Java的File类中有两个delete方法:delete和deleteOnExit。
delete无需解释,为直接删除,deleteOnExit文档解释为:在虚拟机终止时,请求删除此抽象路径名表示的文件或目录。也就是说,程序运行deleteOnExit成功后,File并没有直接删除,而是在虚拟机正常运行结束后才会删除。
3,4班的题 1.冒泡排序public class BubbleSort {
public static void sort(int[]arr){
int len=arr.length;
for (int i=0;iarr[k]){
int c=arr[i];
arr[i]=arr[k];
arr[k]=c;
}
}
}
for (int num:arr){
System.out.println(num);
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
BubbleSort bs=new BubbleSort();
String str=sc.nextLine();//输入数组的内容,可以读取一整行,包括空格
String arr[]=str.split(",");//拆分字符串成字符串数组
int []aw=new int [arr.length];//定义一个长度和arr数组相同的int型的数组
for(int i=0;i
2.
3.
4.
5.打印某个月份的可视化日历
6.
7.
8.
9.
10.



