(有任何问题欢迎留言或私聊 && 欢迎交流讨论哦
常规操作
/**
* Copyright (C), 2018-2018, csust
* FileName: hhd
* Author: Cwolf9
* Date: 2018/13/32 25:61
*/
import java.io.*;
import java.text.DecimalFormat;
import java.util.*;
import java.math.*;
import java.util.Arrays;
public class hhd{
static int N = (int)1e5+7;
static PrintStream putOut = System.out;//可以换一种写法~
public static void main(String[] args){
Scanner cin = new Scanner(System.in);//读入
StringBuilder sb;//String与StringBuilder
String tmp = new String();
tmp = cin.nextLine();
sb = new StringBuilder(tmp);
sb.setCharAt(1,'3');//首位是第0位,改变第1位为'3'
tmp = sb.toString();
System.out.println(tmp+" "+sb);
BigInteger bigNum = new BigInteger(tmp);
System.out.println(bigNum.multiply(BigInteger.valueOf(2)));//大整数运算
String a = bigNum.toString();//两种进制转化的方法
//a = new BigInteger(a,10).toString(2);
a=bigNum.toString(2);
System.out.println(a);
int AA = 199;//控制输出格式
double b = 9.9999;
System.out.printf("%04d %.5f\n",AA,b);
System.out.format("%04d %.5f\n",AA,b);
System.out.printf("%.3f\n",(double)AA);
DecimalFormat p3 = new DecimalFormat("#.000#");
System.out.println(p3.format(AA));
bigNum = cin.nextBigInteger();
System.out.printf("%d\n",bigNum);
String str;//进制转化
int A=8,B=2;
str = Integer.toString(A,2);//把int型数据转换成X进制数并转换成String型
System.out.println(str);
A = Integer.parseInt(str,2);//把字符串当作X进制数转换成10进制int型
System.out.println(A);
str = "aryawu";//String与字符串
char[] ch = str.toCharArray();
System.out.println(ch.length+str.substring(2,4));
int temp = -1;//位运算
System.out.println((temp>>1)+" "+(temp>>>1));
System.out.println(Integer.toString(31,16));
//数组的一些操作
int[] ar = {2,5,9,3,34,1,2};
boolean[] vis = new boolean[100];
boolean[] Vis = new boolean[100];
Arrays.sort(ar);
Arrays.fill(vis,true);
putOut.println("相同?:"+Arrays.equals(vis,Vis));
putOut.println(Arrays.binarySearch(ar,5));
//数据结构
Map map=new HashMap();//以下的俩种使用方式都是可以的
map.put("a",1);
map.put(11,"abc");
Map<String,Integer> mp=new HashMap<String,Integer>();
mp.put("a",1);mp.put("as",2);mp.put("b",3);
mp.put("a",5);
mp.remove("a");
mp.get("as");
//获取所有的key和value
for(Map.Entry<String, Integer> entry : mp.entrySet()){
System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
}
//获取所有的key
Set<String> keys = mp.keySet();
for(String key: keys){
System.out.println(key);
}
//获取所有的value
Collection<Integer> values = mp.values();
for(Integer value: values){
System.out.println(value);
}
putOut.println("===============");
Queue<Integer> que = new LinkedList<Integer>();
/*
* 建议使用offer而不使用add
* 建议使用poll而不使用remove
* 建议使用peek而不使用element
* 因为前者不会抛出异常,会有返回值
* */
que.offer(5);que.offer(2);
putOut.println(que.poll());
que.peek();
for(int i:que){//遍历队列
putOut.println(i);
}
putOut.println("===============");
Stack<Integer>st = new Stack<>();
st.push(12);st.push(23);
st.peek();
st.pop();
st.empty();
putOut.println(st.search(23));
putOut.println(st.search(12));
//数组和List的转换
List list = new ArrayList();
list.add("a");
list.add("12");
int len = list.size();
String[] arr_tmp = new String[len+1];
for(int i=0;i<len;++i){
arr_tmp[i]=(String)list.get(i);
//arr_tmp[i]=list.get(i).toString();
}
List<String> list1 = new ArrayList<String>();
list1.add("a");
list1.add("12");
String[] arr_tmp1 = (String[])list1.toArray(new String[len]);
List<String>list2 = Arrays.asList(arr_tmp);
for(int i=0;i<list2.size();++i){
putOut.println(list2.get(i));
}
List<String> list3 = Arrays.asList("12","34","56");
for(int i=0;i<list3.size();i++){
System.out.println(list3.get(i));
}
while (cin.hasNext()){//多组输入
int n = cin.nextInt();
System.out.println(Math.pow(n,4));
}
}
}
- java的long比int慢好多好多啊
自定义set排序
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
/**
* set的自定义排序操作
* @author admin
*
*/
public class SetSorting {
public static Set<Object> sortByValue(Set<Object> set){
List<Object> setList= new ArrayList<Object>(set);
Collections.sort(setList, new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
// TODO Auto-generated method stub
return o1.toString().compareTo(o2.toString());
}
});
set = new LinkedHashSet<Object>(setList);//这里注意使用LinkedHashSet
return set;
}
public static void main(String[] args) {
Set<Object> set = new HashSet<Object>();
set.add("aaa");
set.add("ccc");
set.add("bbb");
System.out.println(sortByValue(set));
}
}
自定义优先队列
/**
* Copyright (C), 2018-2018, csust
* FileName: hhd
* Author: Cwolf9
* Date: 2018/13/32 25:61
*/
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;
public class fun {
private String name;
private int population;
public fun(String name, int population) {
this.name = name;
this.population = population;
}
public String getName() {
return this.name;
}
public int getPopulation() {
return this.population;
}
public String toString() {
return getName() + "-" + getPopulation();
}
public static void main(String[] args) {
Comparator<fun> OrderIsdn = new Comparator<fun>() {
public int compare(fun o1, fun o2) {
int numbera = o1.getPopulation();
int numberb = o2.getPopulation();
if (numberb > numbera) {
return 1;
}else if (numberb < numbera) {
return -1;
} else {
return 0;
}
}
};
Queue<fun> priorityQueue=new PriorityQueue<fun>(11,OrderIsdn);
fun t1=new fun("t1",1);
fun t2=new fun("t2",2);
fun t3=new fun("t3",3);
fun t4=new fun("t4",0);
priorityQueue.add(t1);
priorityQueue.add(t2);
priorityQueue.add(t3);
priorityQueue.add(t4);
System.out.println(priorityQueue.poll().toString());
}
}
IDEA全选:
ctrl+alt+shift+J
Java判断素数:
public static void main(String[] args){
int count = input.nextInt();
for(int i=0;i<=count-1;i++){
String str = input.next();
long b = Long.valueOf(str);
b /= 2;
while(true){
int get = (int)(b%10);
if(get==1||get==3||get==5||get==7||get==9){
if(BigInteger.valueOf(b).isProbablePrime(2)&&BigInteger.valueOf(Long.valueOf(str)-b).isProbablePrime(2)){
break;
}else{
b+=2;
}
}else{
b++;
}
}
out.println(b+" "+(Long.valueOf(str)-b));
}
out.flush();
}
自定义排序
class lp{
public int u,v;
public int w;
lp(int a, int b, int c){
this.u=a;this.v=b;this.w=c;
}
}
List<lp> cw = new ArrayList<lp>();
Collections.sort(cw, new Comparator<lp>() {
public int compare(lp s1,lp s2){
return s1.w-s2.w;
}
});
---
static class lp implements Comparable<lp>{
public int u,v;
public int w;
lp(int a, int b, int c){
this.u=a;this.v=b;this.w=c;
}
@Override
public int compareTo(lp a) {//this.w和w都可以
return this.w - a.w;//正值o1在o2后面
}
}
Collections.sort(cw);
---
class lp{
public int u,v;
public int w;
lp(int a, int b, int c){
this.u=a;this.v=b;this.w=c;
}
}
Collections.sort(cw, C);
public class Main {
static Comparator<lp> C = new Comparator() {
public int compare(Object o1, Object o2) {
lp x1 = (lp)o1;
lp x2 = (lp)o2;
return x1.w-x2.w;
}
};
}
知识点
1:Set集合(理解)
(1)Set集合的特点
无序,唯一
(2)HashSet集合(掌握)
A:底层数据结构是哈希表(是一个元素为链表的数组)
B:哈希表底层依赖两个方法:hashCode()和equals()
执行顺序:
首先比较哈希值是否相同
相同:继续执行equals()方法
返回true:元素重复了,不添加
返回false:直接把元素添加到集合
不同:就直接把元素添加到集合
C:如何保证元素唯一性的呢?
由hashCode()和equals()保证的
D:开发的时候,代码非常的简单,自动生成即可。
E:HashSet存储字符串并遍历
F:HashSet存储自定义对象并遍历(对象的成员变量值相同即为同一个元素)
(3)TreeSet集合
A:底层数据结构是红黑树(是一个自平衡的二叉树)
B:保证元素的排序方式
a:自然排序(这种排序方式可以理解成元素本身具备比较性)
让元素所属的类实现Comparable接口
b:比较器排序(这种排序可以理解成集合类具备比较性)
让集合构造方法接收Comparator的实现类对象,实现方式可以用匿名类来实现。
C:把我们讲过的代码看一遍即可
(4)案例:
A:获取无重复的随机数
B:键盘录入学生按照总分从高到底输出
2:Collection集合总结(掌握)
Collection
|--List 有序,可重复
|--ArrayList
底层数据结构是数组,查询快,增删慢。
线程不安全,效率高
|--Vector
底层数据结构是数组,查询快,增删慢。
线程安全,效率低
|--LinkedList
底层数据结构是链表,查询慢,增删快。
线程不安全,效率高
|--Set 无序,唯一
|--HashSet
底层数据结构是哈希表。
如何保证元素唯一性的呢?
依赖两个方法:hashCode()和equals()
开发中自动生成这两个方法即可
|--LinkedHashSet
底层数据结构是链表和哈希表
由链表保证元素有序
由哈希表保证元素唯一
|--TreeSet
底层数据结构是红黑树。
如何保证元素排序的呢?
自然排序
比较器排序
如何保证元素唯一性的呢?
根据比较的返回值是否是0来决定
3:针对Collection集合我们到底使用谁呢?(掌握)
唯一吗?
是:Set
排序吗?
是:TreeSet
否:HashSet
如果你知道是Set,但是不知道是哪个Set,就用HashSet。
否:List
要安全吗?
是:Vector
否:ArrayList或者LinkedList
查询多:ArrayList
增删多:LinkedList
如果你知道是List,但是不知道是哪个List,就用ArrayList。
如果你知道是Collection集合,但是不知道使用谁,就用ArrayList。
如果你知道用集合,就用ArrayList。
4:在集合中常见的数据结构(掌握)
ArrayXxx:底层数据结构是数组,查询快,增删慢
LinkedXxx:底层数据结构是链表,查询慢,增删快
HashXxx:底层数据结构是哈希表。依赖两个方法:hashCode()和equals()
TreeXxx:底层数据结构是二叉树。两种方式排序:自然排序和比较器排序
其他
https://www.cnblogs.com/franson-2016/p/5593080.html
https://www.yiibai.com/java/util/java_util_arraydeque.html
import java.io.*;
import java.util.*;
import java.math.*;
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.Comparator;
import java.io.InputStream;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Task solver = new Task();
solver.solve(1, in, out);
out.close();
}
static class Task {
public void solve(int testNumber, InputReader cin, PrintWriter out) {
pair[] a = new pair[10];
Arrays.sort(a);
Task.Contestant[] contestants = new Task.Contestant[10];
Task.Contestant[] sorted = contestants.clone();
Arrays.sort(sorted, new Comparator<Task.Contestant>() {
public int compare(Task.Contestant o1, Task.Contestant o2) {
long s1 = o1.x - o1.y;
long s2 = o2.x - o2.y;
return Long.compare(s1, s2);
}
});
}
static class Contestant {
long x;
long y;
long answer;
}
static class pair implements Comparable<pair> {
int idx, x, y;
public pair(int idx, int x, int y) {
this.idx = idx;
this.x = x;
this.y = y;
}
@Override
public int compareTo(pair p) {
return Integer.compare(x - y, p.x - p.y);
}
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public boolean hasNext() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {//hasMoreElements
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (Exception e) {//IOException
return false;
//throw new RuntimeException(e);
}
}
return true;
}
public String next() {
if(hasNext()) return tokenizer.nextToken();
return null;
}
public int nextInt() {
return Integer.parseInt(next());
}
public Long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public BigInteger nextBigInteger() {
return new BigInteger(next());
}
public BigDecimal nextBigDecimal() {
return new BigDecimal(next());
}
String nextLine() {
String str = "";
try {
str = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}