Popular Posts

이은한. Powered by Blogger.

2022년 1월 31일 월요일

what is comparator


Hierarchy

package: java.util.Comparator

Definition

Sorting interface that designed for mutiple special conditions by creating compare method. For example, you can sort list in ascending order of size and descending order of letters.

about the compare() method

if compare() method return positive number, swap the input parameters
else will be remain same

if First parameter < second parameter, negative
if First parameter == second parameter, 0
if First parameter > second parameter, positive

How to use

MyComparator implements Comparator<>

MyComparator myComparator = new MyComparator();
Arrays.sort(array, myComparator);
Collections.sort(list, myComparator);

Example

import java.awt.*;
import java.util.Comparator;

public class MyComparator implements Comparator<Point> {

    public int compare(Point p1, Point p2) {
        if (p1.x > p2.x) {
            return 1; // x is in ascending order
        } else if (p1.x == p2.x) {
            if (p1.y < p2.y) { // y is in descending order
                return 1;
            }
        }
        return -1;
    }
}
import java.awt.*;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;


public class Main {
    public static void main(String[] args) {

        List<Point> pointList = new ArrayList<>();
        pointList.add(new Point(10, 10));
        pointList.add(new Point(1, 8));
        pointList.add(new Point(5, 2));
        pointList.add(new Point(1, 2));
        pointList.add(new Point(5, 5));
        pointList.add(new Point(10, 1));

        System.out.println("current list");
        for (Point temp : pointList) {
            System.out.println("x: " + temp.x + "  " + "y: " + temp.y);
        }

        MyComparator myComparator = new MyComparator();
        Collections.sort(pointList, myComparator);

        System.out.println("sorted list");
        for (Point temp : pointList) {
            System.out.println("x: " + temp.x + "  " + "y: " + temp.y);
        }

    }

}

Result

current list
x: 10  y: 10
x: 1  y: 8
x: 5  y: 2
x: 1  y: 2
x: 5  y: 5
x: 10  y: 1
sorted list
x: 1  y: 8
x: 1  y: 2
x: 5  y: 5
x: 5  y: 2
x: 10  y: 10
x: 10  y: 1

what is comparable


Hierarchy

package: java.lang.Comparable

Definition

Sorting interface that designed for a condition by overriding compareTo method. For example, you can sort list in ascending order of size.

about the compareTo() method

if compareTo() method return positive number, swap the input parameters
else will be remain same

if First parameter < second parameter, negative
if First parameter == second parameter, 0
if First parameter > second parameter, positive

How to use

customObject implements Comparable<>

Arrays.sort(customObject);
Collections.sort(customObject);

Example


import java.lang.Comparable;

public class customPoint implements Comparable<customPoint> {

    private int x = 0;
    private int y = 0;

    public customPoint(int x, int y) {
        this.x = x;
        this.y = y;

    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    @Override
    public int compareTo(customPoint p2) {
        if (this.y > p2.y) {
            return 1; // y is in ascending order
        }
        return -1;
    }
}

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

public class Main {
    public static void main(String[] args) {

        List<customPoint> pointList2 = new ArrayList<>();
        pointList2.add(new customPoint(10, 10));
        pointList2.add(new customPoint(1, 8));
        pointList2.add(new customPoint(5, 2));
        pointList2.add(new customPoint(1, 2));
        pointList2.add(new customPoint(5, 5));
        pointList2.add(new customPoint(10, 1));

        System.out.println("current list");
        for (customPoint temp : pointList2) {
            System.out.println("x: " + temp.getX() + "  " + "y: " + temp.getY());
        }

        System.out.println("sorted list");

        Collections.sort(pointList2);

        for (customPoint temp : pointList2) {
            System.out.println("x: " + temp.getX() + "  " + "y: " + temp.getY());
        }

    }

}

Result

current list
x: 10  y: 10
x: 1  y: 8
x: 5  y: 2
x: 1  y: 2
x: 5  y: 5
x: 10  y: 1
sorted list
x: 10  y: 1
x: 1  y: 2
x: 5  y: 2
x: 5  y: 5
x: 1  y: 8
x: 10  y: 10

what is exclusive


Definition

Exclude the last number in array

Explanation

If a function will compute 2i2^i where i=1,2,...,ni = 1, 2, ..., n
i can have values from 1 up to and excluding the value n.
We says, n is exclude in exclusive

Example

1 through 10 (exclusive) = [1, 10)
1 2 3 4 5 6 7 8 9

what is inclusive


Definition

Include the last number in array

Explanation

If a function will compute 2i2^i where i=1,2,...,ni = 1, 2, ..., n.
i can have values from 1 up to and including the value n.
We says, n is Include in Inclusive

Example

1 through 10 (inclusive) = [1, 10]
1 2 3 4 5 6 7 8 9 10

2022년 1월 30일 일요일

For, While, Recursion - Which loop statement is best?


Purpose

Most programming languages provide for, while, recursion loop statements. Which one is best?

for

  • I know how many repeat needed
  • Cannot repeat with certain condition (need to use if and break)
  • Hard to use previous result to next repeated loop
  • Good usage example : print numbers in array.

while

  • I do not know how many repeat needed
  • repeat with certain condition
  • Hard to use previous result to next repeated loop
  • Good usage example : count how may words you typed

recursion

  • I know how many Stack Memory that will use
  • repeat with certain condition
  • Easy to use previous result to next repeated loop
  • Good usage example: find 88th's Fibonacci number

additional considerable conditions

  • Does your programming language provide tail recursion
  • Program environment such as embedded, Android, or Web

Conclusion

2022년 1월 26일 수요일

How to install git on windows 10


purpose

install [git] to [windows 10]

What is git?

commend based program that is for tracking file changes. Origianlly, it invented for programming, thus, git file contains information of when did the file created, changed and moved.

steps

1. downlad git

official link

2. install the program

3. installation settings

  • Additional icons  > On the Desktop :  create shortcut

  • Windows Exporer integration

    • Git Bash Here : Git Bash connection, any folder can connect with git right away
    • Git GUI Here : Git GUI connection, create some GUI on right click menu
  • Git LFS (Large File Support) - origianlly git was for coding. Basically codes are just text files. If you check this, it can support other files too.

  • Associate .git configuration files with the default text editor - you can open git file with default text editor

  • Associate .sh files to be run with Bash - sh files(this file is for script language) is connected with git Bash

  • Use a TrueType font in all console windows 

  •  Check daily for Git for Windows updates 

4. Environment variable) setting

  • Use Git from Git Bash only 
  • Use Git from the Windows Command Prompt 
  • Use Git and optional Unix tools from the Windows Command Prompt 

5. HTTPS transport backend setting

  • Use the OpenSSL library - OpenSSL is open source library for encrypting connection between server and website
  • Use the native Windows Secure Channel library 

6. conversions setting

  • Checkout Windows-style, commit Unix-style line endings 
  • Checkout as-is, commit Unix-style line endings
  • Checkout as-is, commit as-is 
  1. Windows-style line endings is "\r\n"
  2. Unix-style line endings is "\n"

Since git invented under Unix system, git cannot verify the "\r". If you are using windows and check the "Checkout as-is, commit as-is", it may cause line changing issue. If you use window, check the "Checkout Windows-style, commit Unix-style line endings"

7. terminal emulator setting

  • Use MinTTY(the default terminal of MSYS2) - MinTTY terminal emulator
  • Use Windows’ default console window 

8. extra option setting

  • Enable file system caching - file will be saved in cach memory and it will work faster
  • Enable Git Credential Manager -  Git Credential Manager for windows
  • Enable symbolic links 

9. check installed

  • git Bash and other coemmed will be added to right click menu
  • you can cemmend "git --version" on cmd

10. set user name and email

git verify users with email and username. If you do not set, it will cause some issue when you upload online

set user name

git config --global user.name "your id"

set email

git config --global user.email "your email"

checking

you can cemmend "git config --list" on cmd for checking

git config --list