diff --git a/code/src/main/java/com/raorao/interview/xiecheng/t1/Main.java b/code/src/main/java/com/raorao/interview/xiecheng/t1/Main.java new file mode 100644 index 00000000..66215535 --- /dev/null +++ b/code/src/main/java/com/raorao/interview/xiecheng/t1/Main.java @@ -0,0 +1,24 @@ +package com.raorao.interview.xiecheng.t1; + +import java.util.Scanner; + +/** + * . + * + * @author Xiong Raorao + * @since 2018-09-04-19:40 + */ +public class Main { + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + long input = scanner.nextLong(); + + int count = 0; + while (input != 0) { + input = input & (input - 1); + count++; + } + System.out.println(count); + } +} diff --git a/code/src/main/java/com/raorao/interview/xiecheng/t2/Main.java b/code/src/main/java/com/raorao/interview/xiecheng/t2/Main.java new file mode 100644 index 00000000..c0dbd101 --- /dev/null +++ b/code/src/main/java/com/raorao/interview/xiecheng/t2/Main.java @@ -0,0 +1,59 @@ +package com.raorao.interview.xiecheng.t2; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.Scanner; + +/** + * . + * + * @author Xiong Raorao + * @since 2018-09-04-19:43 + */ +public class Main { + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + int target = scanner.nextInt(); + scanner.nextLine(); + D[] dd = new D[n]; + List ret = new ArrayList<>(); + for (int i = 0; i < n; i++) { + String[] temp = scanner.nextLine().split(" "); + int id = Integer.parseInt(temp[0]); + int start = Integer.parseInt(temp[1]); + int end = Integer.parseInt(temp[2]); + dd[i] = new D(id, start, end); + } + for (int i = 0; i < n; i++) { + if (target >= dd[i].start && target <= dd[i].end) { + ret.add(dd[i]); + } + } + ret.sort(Comparator.comparingInt(e -> e.id)); + if (ret.size() == 0) { + System.out.println("null"); + return; + } + ret.forEach(e -> System.out.println(e.id)); + + } + + private static class D { + + int id; + int start; + int end; + + public D(int id, int start, int end) { + this.id = id; + this.start = start; + this.end = end; + } + + public D() { + } + } +} diff --git a/code/src/main/java/com/raorao/interview/xiecheng/t3/Main.java b/code/src/main/java/com/raorao/interview/xiecheng/t3/Main.java new file mode 100644 index 00000000..1c6e6668 --- /dev/null +++ b/code/src/main/java/com/raorao/interview/xiecheng/t3/Main.java @@ -0,0 +1,92 @@ +package com.raorao.interview.xiecheng.t3; + +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Map; +import java.util.Queue; +import java.util.Scanner; + +/** + * 输入: 2 p 1 1 p 2 2 g 1 p 2 102 p 3 3 g 1 g 2 g 3 + * + * 输出: 1 1 -1 3 . + * + * @author Xiong Raorao + * @since 2018-09-04-20:04 + */ +public class Main { + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + scanner.nextLine(); + Op ops; + D d = new D(n); + while (scanner.hasNextLine()) { + String[] tmp = scanner.nextLine().split(" "); + char op = tmp[0].charAt(0); + int key = Integer.parseInt(tmp[1]); + int val = 0; + if (tmp.length > 2) { + val = Integer.parseInt(tmp[2]); + } + ops = new Op(op, key, val); + if (ops.op == 'p') { + d.put(ops.key, ops.val); + } else if (ops.op == 'g') { + int res = d.get(ops.key); + System.out.println(res); + } + } + } + + static class D { + + int size; + Map map; + Queue queue = new LinkedList<>(); + + public D(int size) { + this.size = size; + map = new HashMap<>(); + } + + public void put(int key, int val) { + if (map.size() <= size) { + map.put(key, val); + queue.add(key); + } else { + int k = queue.poll(); + map.remove(k); + map.put(key, val); + queue.remove(key); + queue.add(key); + } + } + + public int get(int key) { + int res = map.getOrDefault(key, -1); + queue.remove(key); + queue.add(key); + return res; + } + } + + private static class Op { + + char op; + int key; + int val; + + public Op(char op, int key, int val) { + this.op = op; + this.key = key; + this.val = val; + } + + public Op() { + } + } + + +} diff --git a/code/src/main/java/com/raorao/java/swing/HelloSwing.java b/code/src/main/java/com/raorao/java/swing/HelloSwing.java new file mode 100644 index 00000000..f9ebf0db --- /dev/null +++ b/code/src/main/java/com/raorao/java/swing/HelloSwing.java @@ -0,0 +1,20 @@ +package com.raorao.java.swing; + +import javax.swing.JFrame; + +/** + * . + * + * @author Xiong Raorao + * @since 2018-09-04-9:40 + */ +public class HelloSwing { + + public static void main(String[] args) { + JFrame frame = new JFrame("hello swing"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setSize(300, 100); + frame.setVisible(true); + + } +} diff --git a/code/src/main/java/com/raorao/java/thread/SemaphoreTest.java b/code/src/main/java/com/raorao/java/thread/SemaphoreTest.java new file mode 100644 index 00000000..38e73cce --- /dev/null +++ b/code/src/main/java/com/raorao/java/thread/SemaphoreTest.java @@ -0,0 +1,32 @@ +package com.raorao.java.thread; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Semaphore; + +/** + * . + * + * @author Xiong Raorao + * @since 2018-09-04-17:30 + */ +public class SemaphoreTest { + + public static void main(String[] args) { + Semaphore semaphore = new Semaphore(3); + ExecutorService service = Executors.newCachedThreadPool(); + for (int i = 0; i < 100; i++) { + service.submit(() -> { + try { + semaphore.acquire(); + System.out.println("可用线程数:" + semaphore.availablePermits()); + } catch (InterruptedException e1) { + e1.printStackTrace(); + } finally { + semaphore.release(); + } + }); + } + service.shutdown(); + } +} diff --git a/interview/java/base.md b/interview/java/base.md index 11187944..ee9d16d8 100644 --- a/interview/java/base.md +++ b/interview/java/base.md @@ -11,13 +11,14 @@ - [super和this的异同:](#super和this的异同) - [抽象类和接口](#抽象类和接口) - [java 重写和重载的区别](#java-重写和重载的区别) + - [总结](#总结) - [Synchronized 和 volitate区别](#synchronized-和-volitate区别) - [Synchronized 的内部实现](#synchronized-的内部实现) - [异常](#异常) - [String StringBuffer StringBuilder的区别](#string-stringbuffer-stringbuilder的区别) - [运行速度:](#运行速度) - [线程安全](#线程安全) - - [总结](#总结) + - [总结](#总结-1) - [java 如何实现序列化](#java-如何实现序列化) - [什么是cookie?Session和cookie有什么区别?](#什么是cookiesession和cookie有什么区别) - [死锁的产生以及如何避免](#死锁的产生以及如何避免) @@ -44,6 +45,8 @@ - [.class 和 getclass的区别](#class-和-getclass的区别) - [加载时机不同](#加载时机不同) - [静态内部类](#静态内部类) +- [java 代码块](#java-代码块) + - [代码块执行顺序](#代码块执行顺序) - [参考文档](#参考文档) @@ -372,6 +375,17 @@ java 类可以继承一个抽象类,实现多个接口,都不能被实例化 - 父类的一个方法只能被子类重写一次,而一个方法可以在所有的类中可以被重载多次。 - 重载是编译时多态,重写是运行时多态。 +## 总结 + +重写: +两同,两小,一大 +两同:方法名,方法参数列表相同。 +两小:抛出的异常类型要小于等于父类,返回值类型要小于等于父类 +一大:访问权限大于等于父类。 + +重载: +在同一个类中,方法名和方法参数列表不同,其他的(访问权限、返回值)随意。 + # Synchronized 和 volitate区别 [volatile与synchronized的区别](https://www.cnblogs.com/tf-Y/p/5266710.html) @@ -935,6 +949,21 @@ Iterator it = s.iterator();得到的it的真正类型是KeyIterator,是Iterato 从上面的那些例子上也能看出,除内部类外的其他类的应用上.class功能完全等于.getClass()!只是一个是用类直接获得的,一个是用实例获得的。 +# java 代码块 + +代码块:在Java中,使用{}括起来的代码被称为代码块。 +根据其位置和声明的不同,可以分为 + +- 局部代码块:局部位置,用于限定变量的生命周期。 +- 构造代码块:在类中的成员位置,用{}括起来的代码。每次调用构造方法执行前,都会先执行构造代码块。 +作用:可以把多个构造方法中的共同代码放到一起,对对象进行初始化。 +- 静态代码块:在类中的成员位置,用{}括起来的代码,只不过它用static修饰了。 +作用:一般是对类进行初始化。 + +## 代码块执行顺序 + +静态代码块 > 构造代码块 > 构造函数 + # 参考文档 diff --git a/interview/note.md b/interview/note.md index 87641d5e..95cb9ab3 100644 --- a/interview/note.md +++ b/interview/note.md @@ -50,6 +50,7 @@ Intel | 8.23 | 8.23(新投递) | 电面 | 8.24一面 商汤 | 8.31 | 8.31(新投递) | Keep | 8.31 | 8.31(新投递) 小红书 | 8.31 | 8.31(新投递) +中国银联 | 9.3 | 9.3(官网新投递) # 2 复习内容