JAVA基礎語法(持續更新)

借鑑大神的總結,隨手敲了敲,放到 GitHuble  這裏貼上源碼


/**
 * 
 */
package com.hhx.offline_tools.encode;

import java.awt.RenderingHints.Key;
import java.io.IOException;
import java.net.Socket;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import lombok.Cleanup;
import lombok.AllArgsConstructor.AnyAnnotation;

/**
 * 整理JAVA基礎語法
 * @author 清水賢人
 *
 */
public class JavaGrammar {

	
	public static void main(String[] args) throws IOException {
		
		//定義字符串
		String stringTemp1 = "hello , world";
		String stringTemp2 = "world";
		 
		
		//字符串比較
		boolean comp = stringTemp1.equals(stringTemp2);
		//字符串忽略大小寫比較
		boolean compWithNoCase=stringTemp1.equalsIgnoreCase(stringTemp2);
		
		// 搜索 從0開始算 這裏indexResult爲6 ;如果有值返回對應索引,沒值返回-1
		int indexResult = stringTemp1.indexOf(stringTemp2);
		
		// 搜索 從1開始算 這裏indexResult爲6 ;如果有值返回對應索引,沒值返回-1
		int indexResult2 = stringTemp1.indexOf(stringTemp2,1); 
		
		// 字符串反轉
		StringBuilder builderReverse = new StringBuilder(stringTemp1);
		
		// 反轉後是每個字母均反轉了
		String stringReverse = builderReverse.reverse().toString();
		
		// 轉換大小寫
		String strUpper = stringTemp1.toUpperCase();
		
		String strLower = stringTemp1.toLowerCase();
		
		// 移除收尾空格
		stringTemp1.trim();
		
		//移除所有空格
		stringTemp1.replace(" ","");
		
		// 字符串轉數組,聲明啥樣轉出來就是啥樣(包括空格)
		String arrayTemp = "a, b , c, d   ,e";
		
		String[] arrayResult = arrayTemp.split(",");
		
		
		// 集合遍歷
		HashMap map = new HashMap();
		
		map.put("key1", "111");
		map.put("key2", "222");
		map.put("key3", "333");
		
		for(Iterator iterator = map.entrySet().iterator();iterator.hasNext();) {
			
			Map.Entry entry = (Entry) iterator.next();
			Object key = entry.getKey();
			Object value = entry.getValue();
			
		}
		
		// hashMap 查找key
		 map.containsKey("key1");
		 
		//二分查找.要求是有序數組
		int[] nums = new int[]{7,5,1,3,6,8,9,2};
		// 1 2 3 5 6 7 8 9
		Arrays.sort(nums);
		int index = Arrays.binarySearch(nums, 7);
		
		//arrayList 轉array
		ArrayList arrayList = new ArrayList();
		Object[] objects = arrayList.toArray();
		
		// 將hashmap 轉array
		Object[] obj = map.entrySet().toArray();
		
		
		// 使用 StringBuffer 替換匹配字符串
		Pattern pattern = Pattern.compile("My");
		
		Matcher matcher = pattern.matcher("My a and My b");
		
		StringBuffer sb = new StringBuffer();
		
		boolean found = matcher.find();
		
		while(found) {
			
			matcher.appendReplacement(sb, "Here");
			found = matcher.find();
			
		}
		
		matcher.appendTail(sb);
		System.out.println(sb);//Here a and Here b

		// 判斷字符串是不是數字
		String numString = "12312121";
		
		System.out.println(numString.matches("[0-9]+"));
		System.out.println(numString.matches("[0-9]{1,}"));
		
		Double aDouble = 4.50;
		Double bDouble = 4.50;
		
		// 判斷Double
		boolean resultDouble=aDouble.equals(bDouble);
		
		//System.out.println(resultDouble);
				
		// 十進制轉二進制,八進制,十六進制
		
		int val = 25;
		String binaryStr = Integer.toBinaryString(val);
		
		String octalStr = Integer.toOctalString(val);
		
		String hexStr = Integer.toHexString(val);
		
		//讀取 壓縮包文件(Jar/zip/rar )
		@Cleanup ZipFile file = new ZipFile("D://apache-tomcat-7.0.69 -Spring.zip");
		
		Enumeration entries = file.entries();
		
		while(entries.hasMoreElements()) {
			
			ZipEntry entry =(ZipEntry) entries.nextElement();
			
			if(entry.isDirectory()) {
				//System.out.println(entry.getName()+"-"+entry.getSize());
				//System.out.println("------------------");
			}
		}
		
		 
	}

}


發佈了40 篇原創文章 · 獲贊 29 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章