This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Component | |
| @Qualifier("bubble") | |
| public class BubbleSortAlgorithm implements SortAlgorithm{ | |
| public int[] sort(int[] numbers) { | |
| // Logic for Bubble Sort | |
| return numbers; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Component | |
| public class BinarySearchImpl { | |
| @Autowired | |
| @Qualifier("bubble") | |
| private SortAlgorithm sortAlgorithm; | |
| public int binarySearch(int [] numbers,int numberToSearchFor) { | |
| int[] sortedNumbers = sortAlgorithm.sort(numbers); | |
| System.out.println(sortAlgorithm); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @SpringBootApplication | |
| public class SpringIn5StepsApplication { | |
| public static void main(String[] args) { | |
| ApplicationContext applicationContext = | |
| SpringApplication.run(SpringIn5StepsApplication.class, args); | |
| BinarySearchImpl binarySearch = applicationContext.getBean(BinarySearchImpl.class); | |
| BinarySearchImpl binarySearch1 = applicationContext.getBean(BinarySearchImpl.class); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Component | |
| @Scope("prototype") | |
| public class BinarySearchImpl { | |
| @Autowired | |
| @Qualifier("bubble") | |
| private SortAlgorithm sortAlgorithm; | |
| public int binarySearch(int [] numbers,int numberToSearchFor) { | |
| int[] sortedNumbers = sortAlgorithm.sort(numbers); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Test2DimArrayEx1 { | |
| public static void main(String[] args) { | |
| int [][] data= new int[2][3]; | |
| data[0][0]=1; | |
| data[0][1]=2; | |
| data[0][2]=3; | |
| data[1][0]=4; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class TestVarArgsEx1 { | |
| public void sum(int... data) { | |
| int total = 0; | |
| for (int i = 0; i < data.length; i++) { | |
| total = total + data[i]; | |
| } | |
| System.out.println("total=" + total); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class TestStringEx1 { | |
| public static void main(String[] args) { | |
| String name = "John"; | |
| System.out.println("John+1+2="+name+1+2); | |
| String name2="abcdefgh"; | |
| System.out.println(name2.length()); //顯示字元長度 | |
| System.out.println(name2.charAt(4)); //呼叫出由0開始的第4個字元 | |
| System.out.println(name2.substring(4, 6)); //呼叫出4~6不包含6的字串 | |
| System.out.println(name2.replaceAll("ab", "gg")); //將含有ab的字元取代為gg |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class TestStringEx2 { | |
| public static void main(String[] args) { | |
| String name1 = "abc"; | |
| boolean flag1=name1.matches("[a-z]"); | |
| //後面沒有數字指定個數,即表示指定一個,但是abc為三個字母 | |
| System.out.println("flag1="+flag1); | |
| boolean flag2=name1.matches("[a-z]{3}"); | |
| //於a-z中包含3個字元 | |
| System.out.println("flag2="+flag2); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class TestWrapperClassEx1 { | |
| public static void main(String[] args) { | |
| String message = "12345"; | |
| System.out.println("message+1="+message+1); | |
| int num1 = Integer.parseInt(message); | |
| System.out.println("num1+1="+(num1+1));//有括號,執行數字運算 | |
| System.out.println("num1+1="+num1+1);//沒括號,不執行數字運算 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class TestWrapperClassEx2 { | |
| public static void main(String[] args) { | |
| String value1 = args[0]; //假設args[0]帶450 | |
| Integer i1=new Integer(value1); | |
| //Integer i2=8 | |
| int number=i1; | |
| //int number=i1.value(); 等同上一行 | |
| System.out.println("number開根號="+Math.sqrt(number)); |