首页 > 其他分享 >常用工具类API

常用工具类API

时间:2022-08-23 13:23:14浏览次数:48  
标签:set String int System API println 常用工具 out

常用工具类API

//判断闰年
public static int is_leap_year(int year) {
if(year%4000||(year%100!=0&&year%40)) {
return 1;
}
return 0;
}
//判断某天是星期几
public static void weeks() {//这里举例2021年11月18日是星期四,使用是改变参数
Calendar calendar=Calendar.getInstance();
//Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.YEAR, 2021);
calendar.set(Calendar.MONTH, 10);//月份下标从0开始
calendar.set(Calendar.DAY_OF_MONTH, 18);
System.out.print(calendar.get(Calendar.DAY_OF_WEEK));//sunday是1

 }

//求最大公约数
public static int gcd(int a,int b) {
if(a%b==0)
return b;
return gcd(b,a%b);
}
//求N的所有约数,用list存储
public static List account1() {
long N=2004;
List list = new ArrayList();
for (long i = 1; i*i<=N; i++) {
if (N%i == 0) {
list.add(i);
if (N/i != i) {
list.add((N/i));
}
}
}
return list;

}


//Math里面的常用方法:Max,Min,abs,Random
public void show1() {
int i=2;
int j=3;
int k1=Math.max(i, j);
int k2=Math.min(i,j);
int k3=(int)Math.random();//[0,1);
int k4=Math.abs(-4);//取绝对值
int k5=(int)Math.sqrt(9);//求平方根
}
//map接口常用实现类HashMap集合、LinkedHashMap集合
/*1. HashMap<K,V>:存储数据采用的哈希表结构,元素的存取顺序不能保证一致。
由于要保证键的唯一、不重复,需要重写键的hashCode()方法、equals()方法。

  2.LinkedHashMap<K,V>:HashMap下有个子类LinkedHashMap,
  存储数据采用的哈希表结构+链表结构。通过链表结构可以保证元素的存取顺序一致;
  通过哈希表结构可以保证的键的唯一、不重复,需要重写键的hashCode()方法、equals()方法。

  注意:Map接口中的集合都有两个泛型变量<K,V>,在使用时,要为两个泛型变量赋予数据类型。
  两个泛型变量<K,V>的数据类型可以相同,也可以不同。
 
*/
  public static void show2() {
	  //创建一个hashmap
	  Map<Integer,String>hashmap=new HashMap<Integer,String>();
	  //put方法放入数据
	  hashmap.put(1, "jack");
	  hashmap.put(2, "李四");
	  hashmap.put(3, "金四");
	  //get方法获取
	  String name1=hashmap.get(1);
	  //remove方法移除
	 // hashmap.remove(1);
	  /*重点:遍历
	   * 第一种方法
	  1.使用set集合存储所有的键值(key)
	  2.普通迭代器或者增强for
	  */

// Setkeyset=hashmap.keySet();
// Iterator it=keyset.iterator();
// while(it.hasNext()) {
// //得到key
// int key=it.next();
// //通过key调用hashmap的get方法
// String name=hashmap.get(key);
// System.out.print(key+" "+name);
// System.out.println();
// }
//第二种方法:增强for,使用For entry
for(Entry<Integer,String> entry:hashmap.entrySet()) {
int key=entry.getKey();
String name=entry.getValue();
System.out.print(key+" "+name);
System.out.println();

		 }

}
//set集合的使用,常用HashSet、 LinkedHashSet、TreeSet
/*
Set接口中存放的元素是无序的并且是不可重复的,因此被称为数据集

1.HashSet中是不能出现重复数据的; 可以写入空数据。数据是无序的。
2.LinkedHashSet 不能出现重复数据的; 可以写入空数据。 数据是有序的
3.TreeSet 不能出现重复数据的;不能写入空数据。数据是有序的
*/
public static void show4() {
Set set = new HashSet();
for(int i= 0;i<6;i++){
set.add(i+"");
}
//添加方法都是add,使用迭代器遍历
set.add("3"); //HashSet重复数据,不会写入
set.add(null); //HashSet可以写入空数据
Iterator it = set.iterator();
while(it.hasNext()){
System.out.println(it.next()); //HashSet输出是无序的

    	}		  
  }

//基本类型之间的转换,常用类型转String 和 String转化成char[]
public static void show3() {
//
String number="12345";
int a1=Integer.parseInt(number);
System.out.println(a1);
//
String name="jack";
char[]ch1=null;
ch1=name.toCharArray();//使用toCharArray()方法转化成字符数组
for(int i=0;i<ch1.length;i++) {
System.out.print(" "+ch1[i]);
}
System.out.println();
//
char[]ch2= {'a','b','c'};
String str=null;
str=String.valueOf(ch2);//使用String.valueOf()方法转化成字符串
System.out.println(str);
//
int a2=ch2[2]-'a';
System.out.println(a2);
char ch3= (char) (a2+'a');
System.out.println(ch3);

}
//String 的常用方法
public static void show7() {
String str="abcdefghijk";
//取该下标的元素
str.charAt(0);
//截取字符串:第一个参数是起始下标,第二个是结束下标(方法已重载,可以省)
String str1=str.substring(0,str.length());//
System.out.println(str1);
}


//杨辉三角练习
public static void show1(int m) {
int n=10;
Listlist=new ArrayList();
int arr[][] = new int[n][];
for (int i = 0; i < n; i++) {
arr[i] = new int[i + 1];
for (int j = 0; j <= i; j++) {
if (j == 0 || j == i) {
arr[i][j] = 1;
}else {
arr[i][j] = arr[i-1][j] + arr[i-1][j-1];
}
//System.out.print(arr[i][j] + " ");
list.add(arr[i][j]);
}
//System.out.println();
}
//System.out.println();
//取出list中第一个值为6的下标,+1表示第几个
System.out.println(list.indexOf(6)+1);

}

//子串数目问题,set集合自动去重,
public static void show2() {
//Scanner sc=new Scanner(System.in);
// String str=sc.next();//得不到中间空格
//String str="0100110001010001";
String str="ababc";
char[]ch=str.toCharArray();
long ans=0;
for (int i = 0; i < ch.length; i++) {
HashSet set=new HashSet();
for (int j = i; j < ch.length; j++) {
set.add(ch[j]);
ans+=set.size();
Iterator it = set.iterator();
while(it.hasNext()){
System.out.print(it.next()+" "); //HashSet输出是无序的
}
System.out.println();
}
System.out.println();
}
System.out.println(ans);
}

//子串数目问题2,只求所有子串
public static void show3() {
String str="0100110001010001";
char[]ch=str.toCharArray();
Set set1=new LinkedHashSet();
for (int i = 0; i < ch.length; i++) {

		for (int j = i+1; j < ch.length+1; j++) {
			String str1=str.substring(i,j);
			set1.add(str1);
		}
		Iterator<Character> it = set1.iterator();
    	while(it.hasNext()){
		System.out.print(it.next()+" ");		
    	}
    	System.out.println();
		
	}
	System.out.println(set1.size());
}

//时间显示
public static void show4() {
Scanner cin = new Scanner(System.in);
//题目测试的数值最大不超过10的18次方,所以存储输入的数据类型用long
long n = cin.nextLong();
//先转化成秒
n /= 1000;
//超过24小时的时间舍去,只保留最后一天的时间即可
n %= (24 * 60 * 60);
//输出对应时间即可
System.out.printf("%02d:", n / 3600);
System.out.printf("%02d:", n / 60 % 60);
System.out.printf("%02d\n", n % 60);
cin.close();

}

//
public void show5() {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int sum=0;
int []arr=new int [n];
for(int i=0;i<n;i++) {
arr[i]=sc.nextInt();
sum+=arr[i];
}
}
//队列的使用
public static void show6() {
//LinkedList实现了Queue,
// Queuea = new LinkedList();
// a.offer(1);
// a.add(2);
// for(int i:a) {
// System.out.println(i);
// }
// System.out.println(a.element());
// System.out.println(a.poll());
// for(int i:a) {
// System.out.println(i);
// }
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
Queuequeue=new LinkedList();
for(int i=1;i<=n;i++) {
queue.offer(i);
}
int cur=1;//当前报的数字
while(queue.size()>1) {
int x=queue.element();
queue.poll();
if(cur==m)
cur=1;
else {
queue.offer(x);
cur++;
}
}
System.out.println(queue.element());
}

标签:set,String,int,System,API,println,常用工具,out
From: https://www.cnblogs.com/cn-acheng/p/16615798.html

相关文章

  • String 常用Api
    packagecom.itheima;publicclassstring{publicstaticvoidmain(String[]args){Strings1="qwertyuio";Strings3="QWErtyuio";......
  • Java-常用api01
    1.API1.1API概述【理解】什么是API​ API(ApplicationProgrammingInterface):应用程序编程接口java中的API​ 指的就是JDK中提供的各种功能的Java类,这些类......
  • Java-常用api-匿名内部类
    1.参数传递1.1类名作为形参和返回值(应用)1、类名作为方法的形参方法的形参是类名,其实需要的是该类的对象实际传递的是该对象的【地址值】2、类名作为方法的返回......
  • uniapp 使用Vue3 setup组合式API 引入 uniapp 的 页面生命周期
    uniapp使用Vue3setup组合式API引入uniapp的页面生命周期<template><viewclass="content"><imageclass="logo"@click="handleFei"src="/static/logo.pn......
  • Implementing a Key-Value Store – Part 4: API Design
    ImplementingaKey-ValueStore–Part4:APIDesignThisisPart4oftheIKVSseries,“ImplementingaKey-ValueStore”.YoucanalsochecktheTableofCon......
  • ArcGIS API for JavaScript Editor Widget 选中多个要素不显示名称
    背景 最近升级时遇到了之前碰到的问题,但是忘记解决方法了。又重新对比了旧代码才找到,所以记录下。FeatureLayer的构建方式不是url,而是用的source环境 Vu......
  • 使用@arcgis/core本地部署ArcGIS API for JS
    一、简介@arcgis/core官方文档ArcGISAPIforJavaScript是WebGIS开发中非常重要的前端JS库,是浏览器端调用ArcGIS功能所使用的库。如ArcmapPro中所使用的创建要素,编辑......
  • asp .net api 接收数组
    publicclassQuestiondetailsDto  {    //publicstring?name{get;set;}    publicList<QuestionDto>list{get;set;}=newList<QuestionD......
  • API 应用程序编程接口 (Unity)
    Unity5.6.0f31,如何查看APIHelp——> UnityManual——>ScriptingReference 模块化下载安装2,什么是事件函数MonoBehaviour继承自Behaviour继承自Compon......
  • Guava常用工具类总结
    Guava常用工具类总结-"我想写得更优雅,可是没人告诉我怎么写得更优雅"-"Null的含糊语义让人很不舒服。Null很少可以明确地表示某种语义,例如,Map.get(key)返回Null时,可能表......