1、这一次,彻底解决Java的值传递和引用传递
https://juejin.im/post/5bce68226fb9a05ce46a0476
public class Person {
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public static void PersonCrossTest(Person person) {
System.out.println("传入person的name: " + person.getName());
person.setName("我是张小龙");
System.out.println("方法内重新赋值后的name: " + person.getName());
}
public static void main(String[] args) {
Person p = new Person();
p.setName("我是马化腾");
p.setAge(45);
PersonCrossTest(p);
System.out.println("方法执行后的name: " + p.getName());
}
}
2、使用PriorityQueue
https://www.liaoxuefeng.com/wiki/1252599548343744/1265120632401152
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;
public class PriorityQueueTest {
public static void main(String[] args) {
Queue<User> q = new PriorityQueue<>(new UserComparator());
q.offer(new User("Bob", "A10"));
q.offer(new User("Alice", "A2"));
q.offer(new User("Boss", "V1"));
System.out.println(q.poll());
System.out.println(q.poll());
System.out.println(q.poll());
System.out.println(q.poll());
}
}
class UserComparator implements Comparator<User> {
public int compare(User u1, User u2) {
if(u1.number.charAt(0) == u2.number.charAt(0)) {
int number1 = Integer.parseInt(u1.number.substring(1));
int number2 = Integer.parseInt(u2.number.substring(1));
return (number1 - number2);
//return u1.number.compareTo(u2.number);
}
if(u1.number.charAt(0) == 'V') {
return -1;
} else {
return 1;
}
}
}
class User {
public final String name;
public final String number;
public User(String name, String number) {
this.name = name;
this.number = number;
}
public String toString() {
return name + "/" + number;
}
}
3、大白话说Java反射:入门、使用、原理
https://www.cnblogs.com/chanshuyi/p/head_first_of_reflection.html
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
public class Apple {
private int price;
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public static void main(String[] args) throws Exception{
//正常的调用
Apple apple = new Apple();
apple.setPrice(5);
System.out.println("Apple Price:" + apple.getPrice());
//使用反射调用
Class clz = Class.forName("Apple");
Method setPriceMethod = clz.getMethod("setPrice", int.class);
Constructor appleConstructor = clz.getConstructor();
Object appleObj = appleConstructor.newInstance();
setPriceMethod.invoke(appleObj, 14);
Method getPriceMethod = clz.getMethod("getPrice");
System.out.println("Apple Price:" + getPriceMethod.invoke(appleObj));
}
}
4、Java 序列化之 Externalizable
https://www.jianshu.com/p/411e18ceaa55
import java.io.*;
public class ExternalizeTest implements Externalizable {
private static final long serialVersionUID = 1318824539146791009L;
private String userName;
private transient String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [userName=" + userName + ", password=" + password + "]";
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(this.userName);
out.writeObject(this.password);
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
this.userName = in.readObject().toString();
this.password = in.readObject().toString();
}
public static void main(String[] args) throws Exception {
File file = new File("d:\\a.user");
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
ExternalizeTest user1 = new ExternalizeTest();
user1.setUserName("zhangsan");
user1.setPassword("123456");
oos.writeObject(user1);
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
ExternalizeTest user2 = (ExternalizeTest) ois.readObject();
System.out.println(user2);
}
}
5、一文让你彻底理解 Java NIO 核心组件
https://segmentfault.com/a/1190000017040893
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.StandardCharsets;
public class HelloWorld {
public static void main(String[] args) throws IOException {
FileChannel channel = new RandomAccessFile("D:/test.txt", "rw").getChannel();
System.out.println("channel.size() = " + channel.size());
channel.position(channel.size()); // 移动文件指针到末尾(追加写入)
ByteBuffer byteBuffer = ByteBuffer.allocate(20);
// 数据写入Buffer
byteBuffer.put("你好,世界!\n".getBytes(StandardCharsets.UTF_8));
// Buffer -> Channel
byteBuffer.flip();
while (byteBuffer.hasRemaining()) {
channel.write(byteBuffer);
}
channel.position(0); // 移动文件指针到开头(从头读取)
CharBuffer charBuffer = CharBuffer.allocate(10);
CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
// 读出所有数据
byteBuffer.clear();
while (channel.read(byteBuffer) != -1 || byteBuffer.position() > 0) {
byteBuffer.flip();
// 使用UTF-8解码器解码
charBuffer.clear();
decoder.decode(byteBuffer, charBuffer, false);
System.out.print(charBuffer.flip().toString());
byteBuffer.compact(); // 数据可能有剩余
}
channel.close();
}
}
6、字符串的全部子序列(递归)
https://blog.csdn.net/qq_34115899/article/details/79732050
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.util.Scanner;
public class SubSequence {
public static void printAllSub(char[] str, int i, String res) {
if(str.length ==i) {
System.out.println(res);
return;
} else {
printAllSub(str, i+1, res);
printAllSub(str, i+1, res+str[i]);
}
}
public static void main(String[] args) {
Scanner cin = new Scanner(new BufferedInputStream(System.in));
String str = cin.next();
printAllSub(str.toCharArray(),0,"");
cin.close();
}
}
7、Java 集合系列10之 HashMap详细介绍(源码解析)和使用示例
https://www.cnblogs.com/skywang12345/p/3310835.html
8、二分查找算法(递归与非递归两种方式)
https://blog.csdn.net/lovesummerforever/article/details/24588989
public class BinarySearch {
public static void main(String[] args) {
int srcArray[] = {3,5,11,17,21,23,28,30,32,50,64,78,81,95,101};
System.out.println(binSearch(srcArray, 0, srcArray.length - 1, 222));
System.out.println(binSearch(srcArray,81));
}
/**
* 二分查找普通实现。
* @param srcArray 有序数组
* @param key 查找元素
* @return 不存在返回-1
*/
public static int binSearch(int[] srcArray, int key) {
int mid;
int start = 0;
int end = srcArray.length - 1;
while (start <= end) {
mid = (end - start) / 2 + start;
if(key == srcArray[mid]) {
return mid;
} else if(key < srcArray[mid]) {
end = mid - 1;
} else if(key > srcArray[mid]) {
start = mid + 1;
}
}
return -1;
}
/**
* 二分查找递归实现。
* @param srcArray 有序数组
* @param start 数组低地址下标
* @param end 数组高地址下标
* @param key 查找元素
* @return 查找元素不存在返回-1
*/
public static int binSearch(int srcArray[], int start, int end, int key) {
if(start > end) {
return -1;
}
int mid = (end - start) / 2 + start;
if (key == srcArray[mid]) {
return mid;
} else if(key < srcArray[mid]) {
return binSearch(srcArray, mid + 1, end, key);
} else {
return binSearch(srcArray, start, mid -1, key);
}
}
}
9、973. 最接近原点的 K 个点
https://leetcode-cn.com/problems/k-closest-points-to-origin/
class Solution {
public int[][] kClosest(int[][] points, int k) {
if(points.equals(null) || points.length == 0 || k <= 0)
return null;
int N = points.length;
int[] dest = new int[N];
for (int i = 0; i < N; i++) {
dest[i] = destCal(points[i]);
}
Arrays.sort(dest);
int val = dest[k-1];
int j = 0;
int[][] ret = new int[k][2];
for (int i = 0; i < N; i++) {
if(destCal(points[i]) <= val && j < k) {
ret[j++] = points[i];
}
}
return ret;
}
private int destCal(int[] point) {
return (int) (Math.pow(point[0],2) + Math.pow(point[1],2));
}
}
10、正则表达式 - 匹配 IP 地址 --简了个书1993 2018.05.31
https://www.jianshu.com/p/82886d77440c、
求一个有[0-9].英文字母组成的字符串中,包含有效IP的个数。
例如:1.1.1.1.2,包含2个有效IP:1.1.1.1和1.1.1.2
import java.util.regex.Pattern;
/*
获取字符串中有效IP的个数
* */
public class GetIPNumber {
public static void main(String[] args) {
String str;
int num = 0;
Solution solution = new Solution();
//str = "1.1.1111.16..172.16.254.1.1";
//str = "1.1.1.1.2";
// str = "1.1.1.1";
str = "255.255.255.255";
System.out.println(str.substring(0,str.length()));
// str = "1.1.1.1";
// str = "0.00.1.1";
num = solution.validNum(str);
System.out.println("IP numbers is:"+ num);
//System.out.println("IP numbers is:"+ solution.isValidIP(str));
}
static class Solution {
public int validNum(String str) {
//最短的有效ip长度为7,例如:0.0.0.0,最长的有效ip长度为15,例如:255.255.255.255
int IP_MIN = 7;
int IP_MAX = 15;
if(str == null || str.length() < IP_MIN) {
return 0;
}
int num = 0;
String subStr;
for (int i = 0; i <= str.length() - IP_MIN ; i++) {
for (int j = IP_MIN; j < IP_MAX && i+j <= str.length(); j++) {
subStr = str.substring(i,i+j);
if(isValidIP(subStr)) {
//System.out.println(subStr);
num++;
}
}
}
return num;
}
private boolean isValidIP(String str) {
String pattern = "^(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}";
//String pattern = "((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}";
boolean isMatch = Pattern.matches(pattern, str);
return isMatch;
}
}
}
11、字符串处理
/*从字符串buttons中逐步查找word中个各个字符,计算查找到全部字符需要移动的步数。
* 约束:(1)word中的各个字符一定存在于buttons中,且buttons中没有重复字符;
* (2)一个字符的长度记为1步。
* */
public class StepsCount {
public static void main(String[] args) {
String buttons;
String word;
Solution solution = new Solution();
int count;
buttons = "zyxwvutsrqponmlkjihgfedcba";
word = "xyz";
count = solution.calculateTime(buttons, word);
System.out.println("count = " + count);
buttons = "pqrstuvwxyzabcdefghijklmno";
word = "phone";
count = solution.calculateTime(buttons, word);
System.out.println("count = " + count);
}
static class Solution {
public int calculateTime(String keyboard, String word) {
if(keyboard == null || word == null) {
return 0;
}
int count = 0;
int KL = keyboard.length();
int WL = word.length();
int start = 0;
int dist = 0;
for (int i = 0; i < WL; i++) {
for (int j = 0; j < KL; j++) {
if(keyboard.charAt(j) == word.charAt(i)) {
dist = Math.abs(j - start);
count += dist;
start = j;
break;
}
}
}
return count;
}
}
}