Swap Two Numbers without Temp or Third variable

int a = 10;
int b = 20;
System.out.println("value of a and b before swapping, a: " + a +" b: " + b);

//swapping value of two numbers without using temp variable
a = a+ b; //now a is 30 and b is 20
b = a -b; //now a is 30 but b is 10 (original value of a)
a = a -b; //now a is 20 and b is 10, numbers are swapped
System.out.println("value of a and b after swapping, a: " + a +" b: " + b);
int a = 6;
int b = 3;
System.out.println("value of a and b before swapping, a: " + a +" b: " + b);

//swapping value of two numbers without using temp variable using multiplication and division
a = a*b; //now a is 18 and b is 3
b = a/b; //now a is 18 but b is 6 (original value of a)
a = a/b; //now a is 3 and b is 6, numbers are swapped
System.out.println("value of a and b after swapping using multiplication and division, a: " + a +" b: " + b);
Swap Two Numbers without Temp or Third variable

Example to sort an arraylist of objects by a property

Comparable

A comparable object is capable of comparing itself with another object. The class itself must implements the java.lang.Comparable interface in order to be able to compare its instances.

Comparator

A comparator object is capable of comparing two different objects. The class is not comparing its instances, but some other class’s instances. This comparator class must implement the java.util.Comparator interface.

 

class HockeyPlayer {
    public final int goalsScored;
    // ...
};

List<HockeyPlayer> players = // ...

Collections.sort(players, new Comparator<HockeyPlayer>() {
    @Override 
    public int compare(HockeyPlayer p1, HockeyPlayer p2) {
        return p1.goalsScored - p2.goalsScored; // Ascending
    }
});
class PlayerComparatorByRating implements Comparator<Player> {
    @Override
    public int compare(Player o1, Player o2) {
        return o1.getRating() - o2.getRating();
    }
}

class PlayerComparatorByName implements Comparator<Player> {
    @Override
    public int compare(Player o1, Player o2) {
        return o1.getName().compareTo(o2.getName());
    }
}
Collections.sort(list, new PlayerComparatorByRating());
System.out.println(list);
Collections.sort(list, new PlayerComparatorByName());
System.out.println(list);
List<Book> books = new ArrayList<Book>();
Collections.sort(books, new Comparator<Book>(){
    public int compare(Book o1, Book o2) {
        return o1.name.compareTo(o2.name);
    }
});
Example to sort an arraylist of objects by a property

Adding an existing project to GitHub

1.Create a new repository on GitHub

2.Initialize the local directory as a Git repository

git init

3.Add the files in your new local repository

git add .

4.Commit the files that you’ve staged in your local repository

git commit -m "First commit"

5.In the Command prompt, add the URL for the remote repository where your local repository will be pushed

git remote add origin [remote repository URL]
git remote -v

6.Push the changes in your local repository to GitHub

git push origin master

Adding an existing project to GitHub

Angular – Routing reload 404 error

目前是利用繼承 OncePerRequestFilter 並覆寫 doFilterInternal 的方式來修正:

public class Html5ModeUrlSupportFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
            FilterChain filterChain) throws ServletException, IOException {
        if (isStatic(request) || isApi(request)) {
            filterChain.doFilter(request, response);
        } else {
            request.getRequestDispatcher("/index.html").forward(request, response);
        }
    }

    private boolean isApi(HttpServletRequest request) {
        return request.getRequestURI().indexOf("/api/") >= 0;
    }

    private boolean isStatic(HttpServletRequest request) {
        return Pattern.matches(".+\\.((html)|(css)|(js)|(map))$", request.getRequestURI());
    }
}

若非 api request 也非靜態資源 request,則皆導到 index.html 來處理

 

 

 

Angular – Routing reload 404 error

Angular – Component Lifecycle

Angular 會管理 component 的 整個 lifecycle,也提供了 component lifecycle hook 讓使用者可以在這些時間點加入我們自己的作業。

  • Directives 和 components 皆有的 hook:ngOnInit、ngOnChanges、ngDoCheck、ngOnDestroy
  • 只有 components 有的 hook:ngAfterContentInit、ngAfterContentChecked、ngAfterViewInit、ngAfterViewChecked

Component life cycle hook 的順序如下:

1.ngOnChanges – 在 ngOnInit 之前,當綁定的輸入屬性的值發生變化時

2.ngOnInit – 在首次 ngOnChanges 後

3.ngDoCheck – 每次 change detection cycle 時

4.ngAfterContentInit

5.ngAfterContentInit

6.ngAfterContentChecked

7.ngAfterViewInit

8.ngAfterViewChecked

9.ngOnDestroy

參考資料來源

Angular – Component Lifecycle

Angular – ngOnInit vs constructor

在 angular component 中,可以使用 ngOnInit 和 constructor 來做初始化的動作,兩者的差別在於:

constructor 是 class 初始化時 default 執行的 method,而 ngOnInit 是 angular life cycle hook 的其中一部分,執行的時間是 constructor 和首次 ngOnChanges 之後

EX :

export class App implements OnInit {
  
  constructor(){
     //called first time before the ngOnInit()
  }

  ngOnInit(){
     //called after the constructor and called  after the first ngOnChanges() 
  }
}

所以建議的方式為:使用 constructor 來做 dependency injection 的設定,其他實際要執行的初始化作業則都放在 ngOnInit 中執行。

 

 

Angular – ngOnInit vs constructor