Java基礎回顧(牛客網專案課程)
藉著專案的課程把Java基礎回顧了下,以下為程式碼部分:
package com.howie;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* @Author:HowieLee
* @Date:1/4/2019
* @Description:com.howie
* @version:1.0
*/
public class Main {
/**
* 功能描述:
* @param:
* @return:
* @author:HowieLee
* #date:2019/01/04
*/
public static void print(int index,Object object){
System.out.println(String.format("{%d},%s",index,object.toString()));
}
public static void main(String[] args) {
//print(1,"Hello World!");
//demoOperation();
//demoString();
//demoControlFlow();
//demoList();
//demoMapTable();
//demoSet();
//demoException();
//demoOO();
demoFunction();
}
public static void demoOperation(){
print(1,5 + 2);
print(2,5 - 2);
print(3,5 * 2);
print(4,5 / 2);
print(5,5 % 2);
print(6,5 << 2);
print(7,5 >> 2);
print(8,5 | 2);
print(9,5 ^ 2);
print(10,5 == 2);
print(11,5 != 2);
print(11,5.0 / 2);
print(11,5 / 2.0);
int a = 11;
double b = 2.2f;
a += 2;
print(14,a);
}
public static void demoString(){
String str = "Hello world!";
print(1,str.indexOf('e'));
print(2,str.charAt(3));
print(3,str.compareToIgnoreCase("HELLO WORLD"));
print(4,str.compareTo("hello world"));//看第一個不一樣的字元的差距
print(5,str.contains("hello"));
print(6,str.concat("!!!"));
print(7,str.toUpperCase());
print(8,str.startsWith("hello"));
print(9,str.endsWith("world"));
print(10,str.replace('o','e'));
print(11,str.replaceAll("0|l","a"));//支援正規表示式
print(12,str.replaceAll("Hello","hi"));
print(13,str + str);
StringBuilder sc = new StringBuilder();//StringBuilder為執行緒不安全的,應該用StringBuffer
sc.append("x+");
sc.append('a'+"+");
sc.append(1.2+"+");
sc.append(true);
print(14,sc.toString());
}
public static void demoControlFlow() {
int a = 2;
int target = a == 2 ? 1 : 3;
print(1, target);
if (a == 2) {
target = 1;
} else {
target = 3;
}
print(2, target);
String grade = "B";
switch (grade) {
case "A":
print(3, ">80");
break;//遇到break才可以停止
case "B":
print(3, "60-80");
break;
case "C":
print(3, "<60");
break;
default:
print(3, "未知");
break;
}
for(int i = 0 ; i < 4 ; ++i){
if(i == 0){
continue;
}
print(4,i);
if(i == 2){
break;
}
}
int score = 60;
while(score <= 100){
if(score == 80){
continue;
}
print(5,score);
score += 20;
}
}
public static void demoList(){
List<String> strList = new ArrayList<String>(10);
for(int i = 0 ; i < 4 ; ++i){
strList.add(String.valueOf(i));
}
print(1,strList);
List<String> strList2 = new ArrayList<String>();
for(int i = 0 ; i < 4; i++){
strList2.add(String.valueOf(i * i));
}
strList2.addAll(strList);
print(2,strList2);
strList.remove(0);
print(3,strList);
strList.remove(String.valueOf(1));
print(4,strList);
print(5,strList.get(1));
Collections.reverse(strList2);
print(6,strList2);
Collections.sort(strList2);
print(7,strList2);
Collections.sort(strList2, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o2.compareTo(o1);
}
});
print(8,strList2);
for(String obj : strList2){
print(9,obj);
}
for(int i = 0 ; i < strList2.size() ; i++){
print(10,strList2.get(i));
}
int[] array = new int[]{1,2,3};
for(int j : array){
print(11,j);
}
}
public static void demoMapTable(){
Map<String,String> map = new HashMap<String,String>();
for(int i = 0 ; i < 4 ; ++i){
map.put(String.valueOf(i),String.valueOf(i * i));
}
print(1,map);
for(Map.Entry<String,String> entry : map.entrySet()){
print(2,entry.getKey() + " | " + entry.getValue());
}
print(3,map.values());
print(4,map.keySet());
print(5,map.get("3"));
print(6,map.containsKey("A"));
map.replace("3","27");
print(7,map.get("3"));
}
public static void demoSet(){
Set<String> strSet = new HashSet<String>();
for(int i = 0 ; i < 3 ; i++){
strSet.add(String.valueOf(i));
}
print(1,strSet);
strSet.remove(String.valueOf(1));
print(2,strSet);
print(3,strSet.contains(String.valueOf(1)));
print(4,strSet.isEmpty());
print(5,strSet.size());
strSet.addAll(Arrays.asList(new String[]{"A","B","C"}));
print(6,strSet);
for(String value : strSet){
print(7,value);
}
}
public static void demoException(){
try{
int k = 2;
//k = k / 0;
if(k == 2){
throw new Exception("我是故意的");
}
}catch (Exception e){
//e.printStackTrace();//顯示報錯
print(2,e.getMessage());
}finally {//要主動關掉一些東西
print(3,"finally");
}
}
/**
* 功能描述:物件導向演示
* @param:
* @return:
* @author:HowieLee
* #date:
*/
public static void demoOO(){
Animal a = new Animal("Jim",1);
a.say();
Animal human = new Human("Lei",11,"China");
human.say();
}
/**
* 功能描述:隨機數
* @param:
* @return:
* @author:HowieLee
* #date:
*/
public static void demoFunction(){
Random random = new Random();//方式為偽隨機數
random.setSeed(1);
print(1,random.nextInt(1000));
print(2,random.nextFloat());//預設是在0到1之間
List<Integer> array = Arrays.asList(new Integer[]{1,2,3,4,5});
Collections.shuffle(array);
print(3,array);
Date date = new Date();
print(4,date);
print(5,date.getTime());
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
print(6,df.format(date));
print(7,UUID.randomUUID());
print(8,Math.log(10));
print(8,Math.min(3,10));
print(8,Math.max(111,10));
print(8,Math.ceil(2.2));
print(8,Math.floor(2.2));
}
}
Animal.java
package com.howie;
/**
* @Author:HowieLee
* @Date:1/4/2019
* @Description:com.howie
* @version:1.0
*/
public class Animal implements Talking{
private String name;
private int age;
public Animal(String name,int age){
this.name = name;
this.age = age;
}
@Override
public void say() {
System.out.println(name + " Animal Say");
}
}
Human.java
package com.howie;
/**
* @Author:HowieLee
* @Date:1/4/2019
* @Description:com.howie
* @version:1.0
*/
public class Human extends Animal{
private String country;
public Human(String name,int age,String contry){
super(name,age);
this.country = contry;
}
@Override
public void say() {
System.out.println("This is Human from" + country);
}
}
Talking.java
package com.howie;
/**
* @Author:HowieLee
* @Date:1/4/2019
* @Description:com.howie
* @version:1.0
*/
public interface Talking {
void say();
}
相關文章
- Java基礎知識回顧 -SQLJavaSQL
- Java基礎知識回顧之六 —– IO流Java
- Java基礎知識回顧之六 ----- IO流Java
- 牛客網初級專案筆記(一)筆記
- 牛客刷題筆記--(java基礎301-400)筆記Java
- 回顧JavaScript基礎——函式JavaScript函式
- Java基礎知識回顧之七 —– 總結篇Java
- Java基礎知識回顧之七 ----- 總結篇Java
- 課程回顧 | 如何認識六西格瑪
- Java基礎知識回顧之五 ----- 多執行緒Java執行緒
- HarmonyOS 第一課:基礎課程
- day001|python基礎回顧Python
- C#基礎委託回顧C#
- 牛客網Java評估題Java
- 某課網「vue.js 入門基礎」課程札記Vue.js
- 零基礎創作專業wordpress網站01-課程介紹網站
- 回顧專案測試全過程,測試如何回答 “測完了嗎?”
- 我的樹莓派專案回顧樹莓派
- 人工智慧-數學基礎視訊課程-唐宇迪-專題視訊課程人工智慧
- Python3從基礎到案例,網站爬蟲案例專案實戰-陳世平-專題視訊課程Python網站爬蟲
- Azure Data Factory(九)基礎知識回顧
- Android知識點回顧之Activity基礎Android
- Android知識點回顧之Service基礎Android
- 實踐JavaWeb課程專案JavaWeb
- Java基礎知識(牛客刷題)記錄..(持續輸出中)Java
- 課程設計:python_網路爬蟲專案Python爬蟲
- Python基礎課程筆記5Python筆記
- Flow-3D基礎課程3D
- 【網路安全基礎課程】DDOS攻擊方式有哪些?
- 區塊鏈專案實戰視訊課程(Java版)區塊鏈Java
- wordpress建站基礎教程-黃棒清-專題視訊課程
- 閱讀原始碼,通過LinkedList回顧基礎原始碼
- C++基礎回顧4——動態陣列C++陣列
- Linux支援的檔案系統有哪些?Linux基礎課程Linux
- 專案基礎
- 3、Pico Robot 基礎開發課程
- 尚觀課程-1.Shell基礎
- 開源 | Python基礎入門課程Python