2026年3月30日 星期一

印象是在墾丁小灣,就是有停車場、土地公廟那邊。傍晚天色漸暗,看到景色拍了下來,很幸福的感覺。天高地闊兩個人能走在一起,是件不容易的事。

十八歲那年,認識了一個新來的同事。開放式的廚房,旁邊就是收銀檯,上班時還能聊幾句,就這樣開始交往。那個年紀誰不是腦袋空空,只是當時不自覺而已。有天我脫口而出,問她願不願意嫁給我,我沒別的意思就是想到就脫口而出。過了兩三天她就答覆了,於是過了一段甜蜜的日子。

倆人黏得太緊,我只是想有點自己的空間,發發呆做做白日夢什麼的。她覺得我冷落她,有點情緒化,當時我是真的腦袋一片空白啊。看她難過我手足無措,同時不覺得自己有什麼錯。算是冷戰了一陣子,於是我提出了分手,倆人很平靜但她默默的流下了眼淚。後來和我姊談到她,我姊告訴我我是真的傷了她的心。很多年後才明白自己所表達的並不是自己的想法,還只是個小大人那時。

退伍後我專程去找她,我們聊了一早上,聽她數落我毛病一堆,她原來也是有她的理想的,算是被我給打亂了。聽到這裡才稍稍釋懷,兩顆青澀的蘋果,如果重來的話把愛情拿掉也許會一起走下去。從十八歲到我退伍差不多有四年了,對她的歉疚大於感情。剛退伍只想著銜接社會,也體認到對待感情是要很慎重看待的。現在回顧這段往事,那個時間點還是很自私的,她總是很溺愛我。

聊聊塵封已久的往事,人生真正要面對的是自己。坦然說出這段故事,要是當時有點概念就好了,致敬青春年少。

2019年3月22日 星期五

CHCP

CHange Code Page

Change the active console Code Page. The default code page is determined by the Windows Locale. View the current code page:
chcp Change the code page to Unicode/65001: chcp 65001


Code pageCountry/ Region/ Language
437United States美國
708阿拉伯文(ASMO 708)
720阿拉伯文(DOS)
850Multilingual (Latin I)多語言(拉丁文 I)
852Slavic (Latin II)中歐(DOS) - 斯拉夫語(拉丁文II)
855Cyrillic (Russian)西里爾文(俄語)
857Turkish土耳其語
860Portuguese葡萄牙語
861Icelandic冰島語
862希伯來文(DOS)
863Canadian-French加拿大 - 法語
865Nordic日耳曼語
866Russian俄語 - 西里爾文(DOS)
869Modern Greek現代希臘語
874泰文(Windows)
932日文(Shift-JIS)
936中國 - 簡體中文(GB2312)
949韓文
950繁體中文(Big5)
1200Unicode
1201Unicode (Big-Endian)
1250中歐(Windows)
1251西里爾文(Windows)
1252West European Latin西歐(Windows)
1253希臘文(Windows)
1254土耳其文(Windows)
1255希伯來文(Windows)
1256阿拉伯文(Windows)
1257波羅的海文(Windows)
1258越南文(Windows)
20866西里爾文(KOI8-R)
21866西里爾文(KOI8-U)
28592中歐(ISO)
28593拉丁文 3 (ISO)
28594波羅的海文(ISO)
28595西里爾文(ISO)
28596阿拉伯文(ISO)
28597希臘文(ISO)
28598希伯來文(ISO-Visual)
38598希伯來文(ISO-Logical)
50000用戶定義的
50001自動選擇
50220日文(JIS)
50221日文(JIS-允許一個字節的片假名)
50222日文(JIS-允許一個字節的片假名- SO/SI)
50225韓文(ISO)
50932日文(自動選擇)
50949韓文(自動選擇)
51932日文(EUC)
51949韓文(EUC)
52936簡體中文(HZ)
65000UTF-7 *Unicode (UTF-7)
65001UTF-8 *Unicode (UTF-8)

2018年3月17日 星期六

Searching Arrays and Collections

The Collections class and the Arrays class both provide methods that allow you to search for a specific element. When searching through collections or arrays, the following rules apply:
  • Searches are performed using the binarySearch() method.
  • Successful searches return the int index of the element being searched.
  • Unsuccessful searches return an int index that represents the insertion point. The insertion point is the place in the collection/array where the clement would be inserted to keep the collection/array properly sorted. Because positive return values and 0 indicate successful searches, the binarysearch() method uses negative numbers to indicate insertion points. Since 0 is a valid result for a successful search, the first available insertion point is -1. Therefore, the actual insertion point is represented as (-(insertion point) -1). For instance, if the insertion point of a search is at element 2, the actual insertion point returned will be -3.
  • The collection/array being searched must be sorted before you can search it.
  • If you attempt to search an array or collection that has not already been sorted, the results of the search will not be predictable.
  • If the collection/array you want to search was sorted in natural order, it must be searched in natural order. (This is accomplished by NOT sending a Comparator as an argument to the binarysearch() method.)
  • If the collection/array you want to search was sorted using a Comparator, it must be searched using the same Comparator, which is passed as the second argument to the binarysearch() method. Remember that Comparators cannot be used when searching arrays of primitives.

Let's take a look at a code sample that exercises the binarysearch() method:

import java.util.*;
class SearchObjArray {
    public static void main(String [] args) {
        String [] sa = {"one", "two", "three", "four"};
        Arrays.sort(sa);                                // #1
        for(String s : sa)
            System.out.print(s + " "};
        System.out.println("\none = "
                + Arrays.binarysearch(sa,"one"));            // #2
        System.out.println("now reverse sort");
        ReSortComparator rs = new ReSortComparator();    // #3
        Arrays.sort(sa,rs);
        for(String s : sa)
            System.out.print(s + " ");
        System.out.println("\none = "
                + Arrays.binarysearch(sa,"one"));            // #4
        System.out.println("one = "
                + Arrays.binarysearch(sa,"one",rs));        // #5
    }
    static class ReSortComparator
            implements Comparator {                // #6
        public int compare(String a, String b) {
            return b.compareTo(a);                        // #7
        }
    }
}

which produces something like this:

four one three two
one = 1
now reverse sort
two three one four
one = -1
one = 2

Here's what happened:

Line 1: Sort the sa array, alphabetically (the natural order).
Line 2: Search for the location of element "one", which is 1.
Line 3: Make a Comparator instance. On the next line we re-sort the array using the Comparator.
Line 4: Attempt to search the array. We didn't pass the binarySearch () method the Comparator we used to sort the array, so we got an incorrect (undefined) answer.
Line 5: Search again, passing the Comparator to binarysearch(). This time we get the correct answer, 2
Line 6: We define the Comparator; it's okay for this to be an inner class.
Line 7: By switching the use of the arguments in the invocation of compareTo(), we get an inverted sort.

Given:

1. import java.util.*;
2. public class Quest{
3.     public static void main(String[] args){
4.         String[] colors = 
5.                 {"blue","red","green","yellow","orange"};
6.         Arrays.sort(colors);
7.         int s2 = Arrays.binarySearch(colors, "orange");
8.         int s3 = Arrays.binarySearch(colors, "violet");
9.         System.out.print(s2 + "" + s3);
10.     }
11. }

What is the result?

A. 2-1
B. 2-4
C. 2-5
D. 3-1
E. 3-4
F. 3-5
G. Compilation fails.
H. An exception is thrown at runtime.

Conditional Operator

The conditional operator is a ternary operator (it has three operands) and is used to evaluate boolean expressions, much like an if statement except instead of executing a block of code if the test is true, a conditional operator will assign a value to a variable. In other words, the goal of the conditional operator is to decide which of two values to assign to a variable. This operator is constructed using a ? (question mark) and a : (colon). The parentheses are optional. Its structure is:

x = (boolean expression) ? value to assign if true : value to assign if false

Let's take a look at a conditional operator in code:

class Salary  {
    public static void main(String [] args) {
        int numofPets = 3;
        String status = (numofPets<4) ? "Pet limit not exceeded"
                : "too many pets";
        System.out.println("This pet status is " + status);
    }
}

You can read the preceding code as

Set numofPets equal to 3. Next we're going to assign a String to the status variable. If numofPets is less than 4, assign "Pet limit not exceeded" to the status variable; otherwise, assign "too many pets" to the status variable.

A conditional operator starts with a boolean operation, followed by two possible values for the variable to the left of the assignment ( = ) operator. The first value (the one to the left of the colon) is assigned if the conditional (boolean) test is true, and the second value is assigned if the conditional test is false. You can even nest conditional operators into one statement:

class AssignmentOps {
    public static void main(String [] args) {
        int sizeOfYard = 10;
        int numOfPets = 3;
        String status = (numOfPets<4)?"Pet count OK"
                :(sizeOfYard > 8)? "Pet limit on the edge"
                :"too many pets";
        System.out.println("Pet status is " + status);
    }
}

Don't expect many questions using conditional operators, but remember that conditional operators are sometimes confused with assertion statements, so be certain you can tell the difference.

Given:

11. String[] elements = {"for", "tea", "too"};
12. String first = (elements.length>0) ? elements[0] : null;

What is the result?

A. Compilation fails.
B. An exception is thrown at runtime.
C. The variable first is set to null.
D. The variable first is set to elements[0].

Logical Operators exclusive-OR (^) and Boolean invert (!)

The last two logical operators on the exam are
  • ^ exclusive-OR (XOR)
  • ! Boolean invert

The ^ (exclusive-OR) operator evaluates only boolean values. The ^ operator is related to the non-short-circuit operators we just reviewed, in that it always evaluates both the left and right operands in an expression. For an exclusive-OR (^) expression to be true, EXACTLY one operand must be true—for example,

System.out.printIn ("xor " + ((2<3) ^ (4>3)));

produces the output: xor false

The preceding expression evaluates to false because BOTH operand one (2 < 3) and operand two (4 > 3) evaluate to true.

The ! (boolean invert) operator returns the opposite of a boolean's current value:

if (!(7 == 5)) { system.out.println ("not equal"); }

can be read "if it's not true that 7 == 5," and the statement produces this output: not equal

Here's another example using booleans:

boolean t = true;
boolean f = false;
System.out.println ("! " + (t & !f) + " " + f);

produces the output: ! true false

In the preceding example, notice that the & test succeeded (printing true), and that the value of the boolean variable f did not change, so it printed false.

Given:

1. public class Spock{
2.     public static void main(String[] args){
3.         Long tail = 2000L;
4.         Long distance = 1999L;
5.         Long story = 1000L;
6.         if((tail>distance) ^ ((story*2)==tail))
7.             System.out.print("1");
8.         if((distance+1 != tail) ^ ((story*2)==distance))
9.             System.out.print("2");
10.     }
11. }

What is the result?

A. 1
B. 2
C. 12
D. Compilation fails.
E. No output is produced.
F. An exception is thrown at runtime.

Using Break and Continue

The break and continue keywords are used to stop either the entire loop (break) or just the current iteration (continue). Typically if you're using break or continue, you'll do an if test within the loop, and if some condition becomes true (or false depending on the program), you want to get out immediately. The difference between them is whether or not you continue with a new iteration or jump to the first statement below the loop and continue from there.

The break statement causes the program to stop execution of the innermost loop and start processing the next line of code after the block.

The continue statement causes only the current iteration of the innermost loop to cease and the next iteration of the same loop to start if the condition of the loop is met. When using a continue statement with a for loop, you need to consider the effects that continue has on the loop iteration. Examine the following code:

for (int i = 0; i < 10; i++) {
    System.out.println("Inside loop");
    continue;
}

The question is, is this an endless loop? The answer is no. When the continue statement is hit, the iteration expression still runs! It runs just as though the current iteration ended "in the natural way." So in the preceding example, i will still increment before the condition (i < 10) is checked again. Most of the time, a continue is used within an if test as follows:

for (int i = 0; i < 10; i++) {
    System.out.println("Inside loop");
    if (foo. doStuff() == 5) {
        continue;
    }
    // more loop code, that won't be reached when the above if
    // test is true
}


Given:

1. public class Breaker2{
2.     static String o = "";
3.     public static void main(String[] args){
4.         z:
5.         for(int x=2; x<7; x++){
6.             if(x == 3) continue;
7.             if(x == 5) break z;
8.             o = o + x;
9.         }
10.         System.out.println(o);
11.     }
12. }

What is the result?

A. 2
B. 24
C. 234
D. 246
E. 2346
F. Compilation fails.

2018年3月16日 星期五

Labeled Statements

Although many statements in a Java program can be labeled, it's most common to use labels with loop statements like for or while, in conjunction with break and continue statements. A label statement must be placed just before the statement being labeled, and it consists of a valid identifier that ends with a colon (:).

You need to understand the difference between labeled and unlabeled break and continue. The labeled varieties are needed only in situations where you have a nested loop, and need to indicate which of the nested loops you want to break from, or from which of the nested loops you want to continue with the next iteration. A break statement will exit out of the labeled loop, as opposed to the innermost loop, if the break keyword is combined with a label. An example of what a label looks like is in the following code:

foo:
    for (int x = 3; x < 20; x++) {
        while(y > 7)    {
            y--;
        }
    }

The label must adhere to the rules for a valid variable name and should adhere to the Java naming convention. The syntax for the use of a label name in conjunction with a break statement is the break keyword, then the label name, followed by a semicolon. A more complete example of the use of a labeled break statement is as follows:

boolean is True = true;
outer:
    for(int i=0; i<5; i++) {
        while (isTrue) {
            System.out.println("Hello");
            break outer;
        } // end of inner while loop
        System.out.println("Outer loop."); // Won't print
    } // end of outer for loop
System.out.println("Good-Bye");

Running this code produces

Hello
Good-Bye

In this example the word Hello will be printed one time. Then, the labeled break statement will be executed, and the flow will exit out of the loop labeled outer. The next line of code will then print out Good-Bye. Let's see what will happen if the continue statement is used instead of the break statement. The following code example is similar to the preceding one, with the exception of substituting continue for break:

outer:
    for (int i=0; i<5; 1++) {
        for (int j=0; j<5; j++) {
            System.out.println("Hello");
            continue outer;
        } // end of inner loop
        System.out.println("outer"); // Never prints
    }
System.out.println("Good-Bye");

Running this code produces

Hello
Hello
Hello
Hello
Hello
Good-Bye

In this example, Hello will be printed five times. After the continue statement is executed, the flow continues with the next iteration of the loop identified with the label. Finally, when the condition in the outer loop evaluates to false, this loop will finish and Good-Bye will be printed.

Given:

1. public class Breaker{
2.     static String o = "";
3.     public static void main(String[] args){
4.         z:
5.         o = o + 2;
6.         for(int x=3; x<8; x++){
7.             if(x == 4) break;
8.             if(x == 6) break z;
9.             o = o + x;
10.         }
11.         System.out.println(o);
12.     }
13. }

What is the result?

A. 23
B. 234
C. 235
D. 2345
E. 2357
F. 23457
G. Compilation fails.