前言:有家有公司联系上了我,然后要在牛客网参加一场笔试,之前搞ACM的时候都是用的C++,现在做后端开发都用java了,想用java答题了,虽然java麻烦点,但是从长远看,更有利于我java水平的提高,所以总结了一下java在编程题中常用的API,到时候好查,不保证一定没问题,只是我自己的理解,事关面试很重大还是要自己敲一遍的,出问题别找我

参考:

https://blog.csdn.net/weixin_43217942/article/details/83043120

https://www.cnblogs.com/-maji/p/7123248.html

https://blog.csdn.net/weixin_42696192/article/details/102453797

https://blog.csdn.net/daoshen1314/article/details/100581136

https://blog.csdn.net/qq997404392/article/details/78085783

https://www.jianshu.com/p/53cc95a4eea7

https://blog.csdn.net/q610376681/article/details/89220585#2.5%20TreeSet

https://blog.csdn.net/qq_39326472/article/details/90751089

目录

第一章 输入输出

第二章 数组

第三章 字符串操作

3.1 StringBulder

3.2 其它

第四章 排序

4.1 普通排序

4.2 自定义排序

4.2.1 Arrays.sort

4.2.2 Collection.sort

第五章 大数与浮点数

5.1 概述

5.2 相关API

第六章 map、set、list的使用

6.1 list

6.2 set

6.2.1 遍历

6.2.2 排序

6.3 map

6.3.1 常用API

6.3.2 遍历map

6.3.3 排序

6.4 stack

第七章 数学知识

7.1 数字取整

7.2 math部分API

7.3 排列组合


第一章 输入输出

输入输出是最基本的了,我印象中的输入输出有两种:

一种是给个数字然后不断输入数字:

Scanner input=new Scanner(System.in);
while(input.hasNextInt()){int n = input.nextInt();
}

一种是输入个数字,然后一行行,不断输入字符串:

Scanner input=new Scanner(System.in);
while(input.hasNext()){String str = input.next();
}

next()与nextLine()区别很明确:

next() 方法遇见第一个有效字符(非空格,非换行符)时,开始扫描,当遇见第一个分隔符或结束符(空格或换行符)时,结束扫描,获取扫描到的内容,即获得第一个扫描到的不含空格、换行符的单个字符串。

使用nextLine()时,则可以扫描到一行内容并作为一个字符串而被获取到。

最后注意要引入工具类

import java.util.Scanner;

第二章 数组

java创建数组需要new,数组分为基本类型的数组与字符串数组

创建基本类型的数组:

int[] arr2 = {10,20,30};arr4 = new char[] {'a','b','c'};int[] arr = new int[10];

字符串数组:

String[] strArray={"1","2","3"};
String[] strArray = new String[10];
String[] strArray1 = new String[]{"aaa","aa","aa"};

二维数组:

int[][] arr = {{1,2,3},{4,5,6},{7,8,9}};
int[][] arr = new int[m][n];

遍历:

最简单的,直接都for循环,上限是数组名.length即可

第三章 字符串操作

3.1 StringBulder

Java里的String是不能更改的,所以修改字符串得用StringBuilder,注意StringBuilder不是线程安全的。

public class Copy3 {public static void main(String[] args) {//构造实例化StringBuffer strbu = new StringBuffer("hello world\t");char[] a = {'l','o','y','o','u'};//调用方法System.out.println(1+"\t"+strbu.append(true));            //append(boolean b);System.out.println(2+"\t"+strbu.append('a'));            //append(char b);System.out.println(3+"\t"+strbu.append(a));                //append(char[] b);System.out.println(4+"\t"+strbu.capacity());            //capacity();System.out.println(5+"\t"+strbu.charAt(10));            //charAt(int index);System.out.println(6+"\t"+strbu.delete(3, 9));            //delete(int start, int end);System.out.println(7+"\t"+strbu.insert(5, false));        //insert(int offset, boolean b);System.out.println(8+"\t"+strbu.substring(7));            //substring(int start)System.out.println(9+"\t"+strbu.reverse() );            //reverse()}}

3.2 其它

字符串替换:

//起始位置、结束位置
stringBuilder.replace(1,2,"aaaaaaa");
System.out.println(stringBuilder);

注:String有字符串模式匹配替换

字符串大小比较:

 String t1="20131011";String t2="20131030";int result = t1.compareTo(t2);

result大于0,则t1>t2;

result等于0,则t1=t2;

result小于0,则t1<t2;

第四章 排序

4.1 普通排序

注意引入:

mport java.util.Arrays;

数组排序:

int[] arr = {4,3,5,1,7,9,3};Arrays.sort(arr);

部分位置排序:

int[] arr = {4,3,5,1,2,9,3,0};Arrays.sort(arr,1,4);

4.2 自定义排序

Collection.sort是给List<T>进行排序,而Arrays.sort是给数组进行排序

4.2.1 Arrays.sort

两种写法(注意引入包):

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;class node{int x;
}
class cmp implements Comparator<node>{@Overridepublic int compare(node o1, node o2) {//降序if(o1.x<o2.x) return 1;else if(o1.x==o2.x) return 0;else return -1;}
}
public class 自定义排序 {public static void main(String[] args) {Scanner scan=new Scanner(System.in);int n=scan.nextInt();node a[]=new node[15];for(int i=0;i<n;i++){a[i]=new node();//别忘了new空间a[i].x=scan.nextInt();}Arrays.sort(a,0,n,new cmp());for(int i=0;i<n;i++){System.out.println(a[i].x);}}
}

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;class node implements Comparable<node>{int x;@Overridepublic int compareTo(node o) {// TODO Auto-generated method stubif(this.x<o.x) return 1;else if(this.x==o.x) return 0;else return -1;}
}
public class Main{public static void main(String[] args) {Scanner scan=new Scanner(System.in);int n=scan.nextInt();node a[]=new node[15];for(int i=0;i<n;i++){a[i]=new node();//别忘了new空间a[i].x=scan.nextInt();}Arrays.sort(a,0,n);for(int i=0;i<n;i++){System.out.println(a[i].x);}}
}

4.2.2 Collection.sort

public class run {public static void main(String[] args) throws Exception {//类A实现了Comparable接口,所以可以直接使用Collections.sort()方法A a1 = new A(3);A a2 = new A(1);A a3 = new A(2);ArrayList<A> lista = new ArrayList<A>();lista.add(a1);lista.add(a2);lista.add(a3);Collections.sort(lista);for(A a : lista)System.out.println(a.value);//输出123//类B没有实现Comparable接口,通过自定义的比较器Comparator来使用Collections.sort()方法ArrayList<B> listb = new ArrayList<B>();B b1 = new B(5);B b2 = new B(7);B b3 = new B(6);listb.add(b1);listb.add(b2);listb.add(b3);BComparator comparator = new BComparator();Collection.sort(listb);//错误,会报错,缺少比较器Collections.sort(listb, comparator);//正确for(B b : listb)System.out.print(b.value);//输出567}}
//类A实现了Comparble接口
class A implements Comparable<A> {public int value;//升序排列@Overridepublic int compareTo(A o) {if(this.value > o.value)return 1;else if(this.value < o.value)return -1;elsereturn 0;}public A (int i) {value = i;}
}
//类B是一个普通类
class B {public int value;public B(int i) {value = i;}
}
//定义比较器,实现了Comparator接口,可以用来作为输入参数定义比较方法
class BComparator implements Comparator<B> {//升序排列@Overridepublic int compare(B o1, B o2) {if(o1.value > o2.value)return 1;else if(o1.value < o2.value)return -1;elsereturn 0;}}

第五章 大数与浮点数

5.1 概述

import java.math.BigInteger;

BigInteger支持任意精度的整数,在运算中可以准确地表示任何大小的整数值,而不会丢失任何信息。BigDecimal支持任何精度的定点数,可以用它进行精确的货币计算。

赋值:

BigInteger bi = new BigInteger(“100”);
BigInteger bi = BigInteger.valueOf(100); BigDecimal bd = new BigDecimal(100);
BigDecimal bd = BigDecimal.valueOf(100); 

5.2 相关API

BigInteger:

import java.math.BigInteger;
import java.util.Random;
/** 测试BigInteger类的一些函数*/
public class BigIntegerDemo
{public static void main(String[] args){System.out.println("构造两个BigInteger对象");//BigInteger(int numBits,Random rnd)//构造一个随机生成的 BigInteger,它是在 0 到 (2^numBits - 1)(包括)范围内均匀分布的值BigInteger bi1 = new BigInteger(55,new Random());System.out.println("bil = " + bi1);//BigInteger(byte[] val)   //将包含 BigInteger 的二进制补码表示形式的 byte 数组转换为 BigInteger。BigInteger bi2 = new BigInteger(new byte[]{3,2,3});System.out.println("bi2 = " + bi2);//加System.out.println("bi1 + bi2 = " + bi1.add(bi2));//减System.out.println("bi1 - bi2 = " + bi1.subtract(bi2));//乘System.out.println("bi1 * bi2 = " + bi1.multiply(bi2));//指数运算System.out.println("bi1的2次方 = " + bi1.pow(2));//整数商System.out.println("bi1/bi2的整数商: " + bi1.divide(bi2));//余数System.out.println("bi1/bi2的余数: " + bi1.remainder(bi2));//整数商+余数  System.out.println("bi1 / bi2 = " + bi1.divideAndRemainder(bi2)[0] +   "--" + bi1.divideAndRemainder(bi2)[1]);System.out.println("bi1 + bi2 = " + bi1.add(bi2));  //比较大小,也可以用max()和min() if(bi1.compareTo(bi2) > 0){System.out.println("bi1 is greater than bi2");}else if(bi1.compareTo(bi2) == 0){System.out.println("bi1 is equal to bi2");}else if(bi1.compareTo(bi2) < 0) {System.out.println("bd1 is lower than bd2");  }//返回相反数BigInteger bi3 = bi1.negate();System.out.println("bi1的相反数:" + bi3);//返回绝对值System.out.println("bi1的绝对值:" + bi3.abs());}
}

BigDecimal:

import java.math.BigDecimal;
/** 测试BigDecimal类的一些函数*/
public class BigDecimalDemo
{public static void main(String[] args){System.out.println("构造两个BigDecimal对象");//用char[]数组创建BigDecimal对象,第二个参数为位移offset,  //第三个参数指定长度BigDecimal bd1 = new BigDecimal("3464656776868432998434".toCharArray(),2,15);  System.out.println("bd1 = " + bd1);  //用double类型创建BigDecimal对象  BigDecimal bd2 = new BigDecimal(134258767575867.0F);  System.out.println("bd2 = " + bd2);//加  System.out.println("bd1 + bd2 = " + bd1.add(bd2));  //减  System.out.println("bd1 - bd2 = " + bd1.subtract(bd2));  //乘  System.out.println("bd1 * bd2 = " + bd1.multiply(bd2));  //指数运算  System.out.println("bd1的2次方 = " + bd1.pow(2));  //取商的整数部分  System.out.println("bd1/bd2的整数商: " + bd1.divideToIntegralValue(bd2));  //返回余数计算为:this.subtract(this.divideToIntegralValue(divisor).multiply(divisor))  //System.out.println(bd1.subtract(bd1.divideToIntegralValue(bd2).multiply(bd2)));  System.out.println("bd1/bd2的余数: " + bd1.remainder(bd2));  //取商和余,即bd1.divideToIntegralValue(bd2)与bd1.remainder(bd2)   System.out.println("bd1 / bd2 = " + bd1.divideAndRemainder(bd2)[0] +   "--" + bd1.divideAndRemainder(bd2)[1]);  //比较大小,也可以用max()和min()if(bd1.compareTo(bd2) > 0)  System.out.println("bd1 is greater than bd2");  else if(bd1.compareTo(bd2) == 0)  System.out.println("bd1 is equal to bd2");  else if(bd1.compareTo(bd2) < 0)  System.out.println("bd1 is lower than bd2");  //末位数据精度  System.out.println("bd1的末位数据精度:  " + bd1.ulp());}
}

第六章 map、set、list的使用

6.1 list

 添加方法是:.add(e);  获取方法是:.get(index); 删除方法是:.remove(index); 按照索引删除;

.remove(Object o); 按照元素内容删除;包含.contains;置换 .set(index, element);

查看索引.indexOf()和  lastIndexOf();根据索引位置重新生成一个新的list(截取集合) .subList(fromIndex, toIndex);

6.2 set

TreeSet和HashSet区别:

1、TreeSet 是二差树实现的,Treeset中的数据是自动排好序的,不允许放入null值。

2、HashSet 是哈希表实现的,HashSet中的数据是无序的,可以放入null,但只能放入一个null,两者中的值都不能重复,就如数据库中唯一约束。

3、HashSet要求放入的对象必须实现HashCode()方法,放入的对象,是以hashcode码作为标识的,而具有相同内容的 String对象,hashcode是一样,所以放入的内容不能重复。但是同一个类的对象可以放入不同的实例

6.2.1 遍历

迭代器:

Iterator<Integer> iterator = set.iterator();while (iterator.hasNext()) {iterator.next();}

增强型for循环:

for (int item : set) {// System.out.println(item);}

foreach:

set.forEach(item -> {// System.out.println(item);});

6.2.2 排序

有客户化排序和字然排序,这里我比较喜欢客户化排序,就只列出客户化排序了

public class CustomerComparator implements Comparator<Customer> {@Overridepublic int compare(Customer c1, Customer c2) {if(c1.getName().compareTo(c2.getName())>0)return -1;if(c1.getName().compareTo(c2.getName())<0)return 1;return 0;}public static void main(String args[]){Set<Customer> set = new TreeSet<Customer>(new CustomerComparator());Customer customer1= new Customer("Tom",5);Customer customer2= new Customer("Tom",9);Customer customer3= new Customer("Tom",2);set.add(customer1);set.add(customer2);set.add(customer3);Iterator<Customer> it = set.iterator();while(it.hasNext()){Customer customer = it.next();System.out.println(customer.getName()+" "+customer.getAge());}}
}

set转list:

List<String> list1 = new ArrayList<String>(set);
for(int i = 0; i < list1.size(); i++){  System.out.println("list1(" + i + ") --> " + list1.get(i));
}

6.3 map

6.3.1 常用API

6.3.2 遍历map

/**
* 最常见也是大多数情况下用的最多的,一般在键值对都需要使用*/
Map <String,String>map = new HashMap<String,String>();
map.put("熊大", "棕色");
map.put("熊二", "黄色");
for(Map.Entry<String, String> entry : map.entrySet()){String mapKey = entry.getKey();String mapValue = entry.getValue();System.out.println(mapKey+":"+mapValue);
}//key
for(String key : map.keySet()){System.out.println(key);
}
//value
for(String value : map.values()){System.out.println(value);
}Iterator<Entry<String, String>> entries = map.entrySet().iterator();
while(entries.hasNext()){Entry<String, String> entry = entries.next();String key = entry.getKey();String value = entry.getValue();System.out.println(key+":"+value);
}for(String key : map.keySet()){String value = map.get(key);System.out.println(key+":"+value);
}

6.3.3 排序

按键排序:

public class MapSortDemo {public static void main(String[] args) {Map<String, String> map = new TreeMap<String, String>();map.put("KFC", "kfc");map.put("WNBA", "wnba");map.put("NBA", "nba");map.put("CBA", "cba");Map<String, String> resultMap = sortMapByKey(map);    //按Key进行排序for (Map.Entry<String, String> entry : resultMap.entrySet()) {System.out.println(entry.getKey() + " " + entry.getValue());}}public static Map<String, String> sortMapByKey(Map<String, String> map) {if (map == null || map.isEmpty()) {return null;}Map<String, String> sortMap = new TreeMap<String, String>(new MapKeyComparator());sortMap.putAll(map);return sortMap;}}

按值排序:

public class MapSortDemo {public static void main(String[] args) {Map<String, String> map = new TreeMap<String, String>();map.put("KFC", "kfc");map.put("WNBA", "wnba");map.put("NBA", "nba");map.put("CBA", "cba");Map<String, String> resultMap = sortMapByValue(map); //按Value进行排序for (Map.Entry<String, String> entry : resultMap.entrySet()) {System.out.println(entry.getKey() + " " + entry.getValue());}}/*** 使用 Map按value进行排序*/public static Map<String, String> sortMapByValue(Map<String, String> oriMap) {if (oriMap == null || oriMap.isEmpty()) {return null;}Map<String, String> sortedMap = new LinkedHashMap<String, String>();List<Map.Entry<String, String>> entryList = new ArrayList<Map.Entry<String, String>>(oriMap.entrySet());Collections.sort(entryList, new MapValueComparator());Iterator<Map.Entry<String, String>> iter = entryList.iterator();Map.Entry<String, String> tmpEntry = null;while (iter.hasNext()) {tmpEntry = iter.next();sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());}return sortedMap;}
}class MapValueComparator implements Comparator<Map.Entry<String, String>> {@Overridepublic int compare(Entry<String, String> me1, Entry<String, String> me2) {return me1.getValue().compareTo(me2.getValue());}
}

6.4 stack

差点把它忘了

定义

Stack<Integer> stack = new Stack<Integer>();

第七章 数学知识

7.1 数字取整

1.向上bai取整Math.ceil();

举例du:Math.ceil(11.4)=12; Math.ceil(-11.6)=-11;

2.向下取整Math.floor();

举例:Math.floor(11.7)=11;Math.floor(-11.2)=-12;

3.四舍五入Math.round();

顾名思义,zhi四舍五入后取整,其算法为Math.round(x+0.5),即原来的数字加上0.5后再想下取整即可。

7.2 math部分API

double a = -0.5;
double pi = Math.PI;
double e = Math.E;
System.out.println("a的绝对值为:" + Math.abs(a));
System.out.println("pi的反余弦值为:" + Math.acos(pi/4));
System.out.println("a向上取整的值为:" + Math.floor(a));
System.out.println("a向下取整的值为:" + Math.ceil(a));
System.out.println("e的自然对数为" + Math.log(e));
System.out.println("a与0.5中的最大值为:" + Math.max(a,0.5));
System.out.println("a乘以一个[0,1)随机值为" + Math.random()*a);
System.out.println("最接近a的long类型值为:" + Math.round(a));
System.out.println("pi/6的正弦值为:" + Math.sin(pi/6));

7.3 排列组合

大数取模时可能会用到:(a/b)%m = (a % (b*m))/b

Java编程题笔试常用API相关推荐

  1. Java编程题_面向对象和常用API01_B级

    Java编程题_面向对象和常用API01_B级 第1题 面向对象.异常.集合.IO 题干: 请编写程序,完成键盘录入学生信息,并计算总分将学生信息与总分一同写入文本文件 需求:键盘录入3个学生信息(姓 ...

  2. java经典50道_50道经典的JAVA编程题(41-45)

    50道经典的JAVA编程题(41-45),苦逼的程序猿,晚上睡不着了编程吧~今天坚持做10道题!发现编程能是我快乐...O(∩_∩)O哈哈~能平静我烦乱的心,剩下5道题留到考试完了再做吧!该睡觉了.. ...

  3. java 编程题_最新JAVA编程题全集(50题及答案)92862

    <最新JAVA编程题全集(50题及答案)92862>由会员分享,可在线阅读,更多相关<最新JAVA编程题全集(50题及答案)92862(32页珍藏版)>请在人人文库网上搜索. ...

  4. 质量不同的球java编程_荐非常经典的java编程题全集-共50题(1-10)...

    非常经典的java编程题 程序1:斐波那契数列问题 题目概述: 古典问题: 有一对兔子,从出生第三个月起每月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多 ...

  5. java语言编程三角形图形_编程题:编写程序输入三角形的3条边长,计算并输出... 求助一道JAVA编程题:编写一个类似记事本的图形用......

    导航:网站首页 > 编程题:编写程序输入三角形的3条边长,计算并输出... 求助一道JAVA编程题:编写一个类似记事本的图形用... 编程题:编写程序输入三角形的3条边长,计算并输出... 求助 ...

  6. java类求圆的面积周长_java编程 1. 设计一个求圆的面积和周长的类,要求:1计算当半径r,JAVA编程题。编写一个应用程序计算圆的周长和面积,设圆的半...

    问题标题 java编程 1. 设计一个求圆的面积和周长的类,要求:1计算当半径r,JAVA编程题.编写一个应用程序计算圆的周长和面积,设圆的半 2019-5-23来自ip:15.196.194.53的 ...

  7. java设计一个形状shape_编程定义一个图形接口 Shape, 内含2个抽象方法 get... JAVA编程题:编一个程序包含一个接口 shape(该接......

    导航:网站首页 > 编程定义一个图形接口 Shape, 内含2个抽象方法 get... JAVA编程题:编一个程序包含一个接口 shape(该接... 编程定义一个图形接口 Shape, 内含2 ...

  8. java编程50_荐非常经典的java编程题全集-共50题(1-10)...

    非常经典的java编程题 程序1:斐波那契数列问题 题目概述: 古典问题: 有一对兔子,从出生第三个月起每月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多 ...

  9. 一些简单的java编程题(2) ————古典问题(兔子问题)

    版权声明: 本文为博主原创文章,未经博主允许不得转载. /* (程序头部注释开始) 版权声明:保留个人权利. 如程序有不当之处,敬请指正.* 文件名称: <一些简单的java编程题(2) --- ...

最新文章

  1. 0基础学怎么学习python
  2. web前端性能优化总结
  3. pyhanlp 停用词与用户自定义词典
  4. 浏览器事件捕获冒泡以及阻止冒泡
  5. bootstrap五星评分_如何用纯代码实现评分星级显示?
  6. Polygon对象和Polyline对象的组成形式
  7. Linux 命令(5)—— shutdown 命令
  8. 前端项目ip设置成0.0.0.0的原因
  9. 语义分割-ICCV2017 Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks循环一致对抗网
  10. 计算机存储数据时2的20次方,2的20次方是多少
  11. 用DEM制作通用三维地形模型
  12. 阿里面试官没想到一个ArrayList,我都能跟他扯半小时
  13. stm32f407Zgt6 与 hc05蓝牙模块通信
  14. 杂谈:Perl6 树莓Pi Erlang win10 Clojure
  15. Excel中数据透视表的使用(一)
  16. java项目pm_项目中PO、PM的职责区分
  17. Python3.6笔记之腌制泡菜(pickle模块的用法)
  18. CentOS 7 安装好后,无法使用小键盘的解决办法
  19. gis平移至所选要素_ArcGIS中如何实现矢量数据平移
  20. 亲身实践已解决:Mysql Row size too large (> 8126). Changing some columns to TEXT or BLOB or using ROW_FORMAT

热门文章

  1. 每日微信晨报早报新闻获取,哪里来的?
  2. JSON——李炎灰js笔记
  3. 使用AirCode云平台,两步将ChatGPT接入微信公众号
  4. Vue 组件封装之 Search 搜索
  5. 一流程序员靠数学,二流程序员靠算法,网友:我是七流靠复制
  6. 数据分析基础知识之数据收集与分析软件
  7. ajax布林德,丹尼·布林德
  8. Auto reloading enabled
  9. 西班牙列车推出QR码阅读新方式
  10. 螺杆启闭机的安装和调试