Oh! You closed up the window, so you cannot see raining

[Java] Java 的中 HashMap.comparableClassFor(Object x) 的函式解讀

HashMap.comparableClassFor(Object x) 的函式解讀 原文敘述 Returns x’s Class if it is of the form “class C implements Comparable”, else null. 我的翻譯 當x的類別為Comparable的實作時,返回x的類別;否則返回 null。 藉由這個函式實例的解讀,可以了解一下類別、泛型的相關概念。 Source Code static Class<?> comparableClassFor(Object x) { if (x instanceof Comparable) { Class<?> c; Type[] ts, as; ParameterizedType p; if ((c = x.getClass()) == String.class) // bypass checks return c; if ((ts = c.getGenericInterfaces()) != null) { for (Type t : ts) { if ((t instanceof ParameterizedType) && ((p = (ParameterizedType) t).getRawType() == Comparable.class) && (as = p.getActualTypeArguments()) != null && as.length == 1 && as[0] == c) // type arg is c return c; } } } return null; } instanceof insanceof 可理解成某類別的實作,無論是執行期時的類別,或是父類別,或是它實現的介面,或父類別實現的介面…,總之只要在繼承鏈上有這個類別就可以了。 getClass() 與instanceof相對應的是getClass()函式,無論該物件如果轉型,getClass()都會返回它執行時期的類別,可以簡單理解成實際類別,換言之也就是我們 new 出來物件時使用的類別。 有一種例外情形是匿名物件,當匿名物件調用getClass()時,返回的是依賴它的物件在執行期的類別,並以1,2,3…的index區分。 getGenericInterfaces() getGenericInterfaces()方法返回的是該物件在執行期時直接實作的介面。必然是該類別自己實作的介面,繼承的則不可。 getGenericSuperclass()和getSuperclass() 這兩個函式雖然沒有出現在 comparableClassFor(Object x)中,但也順帶一提。 ...

February 23, 2022 · 2 分鐘 · Rain Hu
Oh! You closed up the window, so you cannot see raining

[Java] List of list of something equality

List of Generics equality Case In leetcode no. 39 Combination Sum gives Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order. The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different. ...

February 18, 2022 · 3 分鐘 · Rain Hu

Hello World

How to say hello to the world? Java class Hello{ public static void main(String[] args){ System.out.println("Hello World!"); } } C #include <stdio.h> int main(){ printf("Hello World!"); return 0; } C++ #include <iostream> int main(){ std::cout << "Hello World!" << std::endl; return 0; } ###C# namespace HelloWorld{ class Hello{ static void Main(String[] args){ System.Console.WriteLine("Hello World!"); } } } Python print("Hello World!") Ruby puts 'Hello World!' TCL Language puts "Hello World!" JavaScript console.log("Hello World!"); TypeScript console.log 'Hello World!' Perl print "Hello World!"; R cat('Hello World!'); Swift println('Hello World!'); Kotlin fun main(args: Array<String>){ println("Hello World!") } Go println('Hello World!'); PHP echo "Hello World!"; VBA msgbox "Hello World" Assembly Language global _main extern _printf section .text _main: push message call _printf add esp, 4 message: db 'Hello World!', 11, 0 Me Hello the fucking world

February 17, 2022 · 1 分鐘 · Rain Hu
Oh! You closed up the window, so you cannot see raining

[Washam] Way to SWE

Coding Interview University John Washam: I originally created this as a short to-do list of study topics for becoming a software engineer, but it grew to the large list you see today. After going through this study plan, I got hired as a Software Development Engineer at Amazon! You probably won’t have to study as much as I did. Anyway, everything you need is here. I studied about 8-12 hours a day, for several months. This is my story: Why I studied full-time for 8 months for a Google interview ...

May 25, 2019 · 50 分鐘 · Rain Hu