1. 程式人生 > >java學習筆記第18天2018.09.20-System類、集合

java學習筆記第18天2018.09.20-System類、集合

複習: String: 常量,不可改變,可以改變的引用 常量池: 如果字串存在,則不開闢新空間,直接使用。 常用方法: StringBuffer/StringBuilder: 可以改變的字元序列。 append():追加

包裝類:
	基本型別---》包裝類,Integer  Character
	字串,包裝類,基本型別間的轉換
	自動裝箱和自動拆箱
	Integer in = 123;
	int num = in;

正則表示式:
	由 字母  數字  符號 組成的有規則的字串,可以去校驗指定的字串。
	1.匹配	matches
	2.切割  split
	3.替換	replaceAll
	4.獲取	
		1.將正則規則封裝到正則物件      Pattern  p = Pattern.compile(regex);
		2.將正則物件與要操作的字串相結合,得到匹配器物件   Mathter m = p.matcher(str);
		3.使用匹配器物件,獲取符合規則內容,	先判斷後獲取     m.find(),m.group()

System: 該類final類,繼承Object,不能例項化。 欄位: out: 標準的輸出裝置 err: 標準的錯誤輸出裝置 in: 標準的輸入裝置 方法: void exit(0) :系統退出 long currentTimeMills():獲取當前系統時間毫秒值 Property getproperties():獲取系統所有的屬性名和屬性值 String getProperty(name):根據name屬性返回屬性值

Math: 該類final類,繼承Object,類中方法和屬性都是靜態的,不需要物件。 屬性: E :自然底數 PI:圓周率 方法: ceil(double a):>= a 的最小整數 floor(double a): <= a 的最大整數 round(double a): 四捨五入 pow(a,b): a的b次冪 random(): 隨機數 Random: java.util nextInt(num) :返回的是 [0,num)間的隨機整數 jdk5.0特性:

1.靜態匯入
	import  包名.類名;  匯入指定的類
	靜態匯入:匯入的是靜態成員(變數和方法)
	語法格式:
		import static  包名.類名.靜態成員名;
		import static  包名.類名.*;
	好處:
		簡化了程式碼的編寫
	弊端:
		閱讀性變差
2.可變引數
	格式:  陣列型別...   變數名
		eg: int... num 
	底層原理是:一個數組,可以按照陣列的方式進行操作
	注意:可變引數在方法引數列表上,只能出現一次,並且放在引數列表的最後邊

3.for-each語句
	for(資料型別  變數名 : 被遍歷的物件(陣列/集合)){
		
	}
	作用:遍歷操作

java.util包 Date類: 日期類,可以精確到毫秒。該類大部分方法已過時,用Calendar類替代。 構造方法: new Date() new Date(long time) DateFormat: 抽象類 static getInstance():獲取DateFormat的例項物件 String format(Date date):可以將date進行預設格式化

--SimpleDateFormat:
	new SimpleDateFormat(模式):當呼叫format方法時,可以按照指定的模式去進行格式化。
	
	eg:
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	String date = sdf.format(new Date());

	eg:
	可以將一段表示時間日期的字串,按照模式進行解析,解析為Date物件
	String  str = "2018-09-20 11:22:55";
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	Date date = sdf.parse(str);

Calendar: 日曆類,抽象類 static getInstance():可以獲取Calendar例項物件 int get(Field):根據Field獲取對應的資料 eg: Calendar c = Calendar.getInstance(); int year = c.get(Calendar.YEAR);

集合: 陣列: 1.元素統一 2.長度固定 3.即可以儲存引用型別也可以儲存基本型別 集合: 1.不同型別的資料 2.長度可變 3.儲存引用型別

 集合框架:
 java.util包:

 Collection:
 	---List
	    ---ArrayList
	    ---LinkedList
	    ---Vector
	---Set
	    ---HashSet
	    ---TreeSet

 Map:
 	---HashMap
	---TreeMap
import java.util.Random;
/*
 * math類練習
 */
public class Demo {
	public static void main (String [] args) {
//		System.out.println(Math.sqrt(16));//開平方
//		System.out.println(Math.pow(2,3));//第一個數為底數,第二個數為指數,
//		System.out.println(Math.random());//[0.0-1.0)之間的隨機數
//		System.out.println(Math.E);//自然底數
//		System.out.println(Math.PI);//常量
//		System.out.println(Math.ceil(4.56));//大於當前數的最小整數
//		System.out.println(Math.floor(4.56));//小於當前數的最大整數
//		System.out.println(Math.round(4.67));//四捨五入
//		
//		Random r = new Random();//建立產生隨機數的那個物件
////		System.out.println(r.nextInt(6));//產生隨機數
//////		for(int i = 0;i<6;i++) {
////			System.out.println(r.nextInt(6)+1);
////		}
//		System.out.println(r.nextDouble());//。[0.0-1.0)
		
		
		
	}
}

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.junit.Test;

/*
 * 日期時間類
 */
public class Demo02 {
  @Test
  public void test() throws ParseException {
//	  Date d = new Date();//把當前時間封裝成Date物件
//	  long s = d.getTime();//獲取DATE物件所表示的當前時間對應的毫秒值
//	  System.out.println(d);//毫秒值
//	  System.out.println(s);//當前時間
	  
//	  Date s2 = new Date(999900001231L);//給定一毫秒值
//	  System.out.println(s2);//列印輸出的是對應的時間資訊
	  
	  
//	  DateFormat format = DateFormat.getInstance();//獲取到DateFormat物件
//	  Date d = new Date();
//	  String s = format.format(d);//按照預設格式,顯示日期和時間
//	  System.out.println(s);
	  
//	  Date d = new Date();
	  SimpleDateFormat sdf = new SimpleDateFormat("yyyy 年 MM 月 dd 日 HH:mm: ss");
//	  String Date1= sdf.format(d);
//	  System.out.println(Date1);//按照自定義模式解析
	  
	  
	  String  str = "2018 年 09 月 20 日 19:47: 06";
	  Date date2 = sdf.parse(str);
	  System.out.println(date2);
  }
}

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import org.junit.Test;

/*
 * 集合Collection
 */
public class Demo03 {
	@Test
	public void test() {
		Collection coll = new ArrayList();
		coll.add("我愛學程式設計");// 將一個物件新增到集合容器中
////	System.out.println(coll);
		coll.add("0903Java");
		coll.add("123");
////		System.out.println(coll);
////		coll.remove("0903Java");//從集合中移除指定的單個例項
//		System.out.println(coll);
		
//		boolean b = coll.contains("123");// 判斷集合中是否包含此元素,包含返回true,反之返回false.
//		System.out.println(b);
		
//		boolean cs = coll.isEmpty();// 判斷此集合是否為空,空為true,非空為false。
//		System.out.println(cs);
		
//		System.out.println(coll.size());// 獲取此集合的元素個數,返回值為int型別
		Collection coll1 = new ArrayList();
		coll1.add("123");
		coll1.add("567");
//		coll.addAll(coll1);// 將指定集合中的所有元素新增到另一個集合中
//		System.out.println(coll);
		
//		boolean c = coll.containsAll(coll1);// 此集合中是否包含指定集合的所有元素,包含返回true,反之為false。
//		System.out.println(c);
		
//		coll.retainAll(coll1);//保留了此集合中指定集合包含的元素。
////	System.out.println(coll);
		
//		coll.removeAll(coll1);//移除此集合中指定集合包含的元素。
//		System.out.println(coll);
		
//		coll.clear();//移除此集合中的所有元素
//		System.out.println(coll);
		
		Iterator it =coll.iterator();//迭代器的使用
		while(it.hasNext()) {
			System.out.println(it.next());		
		}
	}
}

/*
 * 可變引數
 */
public class Demo04 {
	public static void main(String [] args) {
	   int s = getSum(1,2,3,4,5);
	   System.out.println(s);
	}
	//可變引數列表中可變引數只有一個,且寫在最後面。
	private static int getSum(int x,int...arr) {
		int sum = 0 ;
		for(int i =0;i<arr.length;i++) {
			sum +=arr[i];
		}
	    return sum;
	}
}

import java.util.Properties;
/*
 * System類練習
 */
//import static java.lang.System.out;
public class Demo05 {
	public static void main(String[] args) {
//		System.out.println("我愛學程式設計");
		
//		out.println("我愛java");//靜態匯入包後
		
//		System.err.println("我愛北京天安門");//輸出錯誤的語句,字型為紅色。
		
//		Scanner input = new Scanner(System.in);//標準的輸入裝置,需使用Scanner。
//		int s = input.nextInt();
//		System.out.println("s="+s);
		
//		System.exit(0);//強制退出JVM,系統退出。
		
//		long time = System.currentTimeMillis();//獲取當前時間的毫秒值
//		System.out.println(time);
		
//		String s = System.getProperty("System.year");//根據指定的屬性名獲取屬性值
//		System.out.println(s);
		
		Properties prop = System.getProperties(); //獲取所有的屬性名和屬性值,儲存到Properties物件中
		prop.list(System.out);  //列出所有的屬性名和屬性值
		
	
	}
}

import java.util.Calendar;

/*
 * Calendar日曆類
 */
public class Demo06 {
	public static void main(String[] args) {
		Calendar c = Calendar.getInstance();//獲取Calendar物件
		System.out.println(c);//包含所有資訊
		
		int year =c.get(Calendar.YEAR);
		System.out.println(year);
		int month = c.get(Calendar.MONTH);
		System.out.println(month+1);
		int day = c.get(Calendar.DAY_OF_MONTH);
		System.out.println(day);
		int hour = c.get(Calendar.HOUR);
		System.out.println(hour);
		int minute = c.get(Calendar.MINUTE);
		System.out.println(minute);
		int second = c.get(Calendar.SECOND);
		System.out.println(second);
	}
}

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

/*
 * List介面
 */
public class Demo07 {
	public static void main(String[] args) {
		List list = new ArrayList();
		list.add("ABC01");
		list.add("abc02");//向集合的尾部增加元素
//		System.out.println(list);
//		list.add(1,"bcd03");//向集合指定的位置插入元素
//		System.out.println(list);
//		System.out.println(list.get(0));//根據索引返回元素
//		System.out.println(list.remove(0));//根據索引刪除元素,並返回刪除的元素
//		System.out.println(list.set(1, "HELLO"));//根據索引替換指定內容,並返回原內容
		
		//遍歷
		for(int i =0 ; i<list.size();i++) {
			System.out.println(list.get(i));
		}
		//list特有的迭代器
		System.out.println("--------------------------");
		ListIterator li = list.listIterator();//從前往後遍歷
		while(li.hasNext()) {
			System.out.println(li.next());
		}
		while(li.hasPrevious()) {//從後往前遍歷
			System.out.println(li.previous());
		}
		//增強for迴圈遍歷
		for(Object obj : list) {
			System.out.println(obj);
		}
	}
	
}

import java.util.ArrayList;
import java.util.Collection;

import org.junit.Test;

/*
 * 集合遍歷小細節,型別轉換
 */
public class Demo08 {
	@Test
	public void test() {
		Collection coll =new ArrayList();
		coll.add(new Person("小明1",18));
		coll.add(new Person("小明2",20));
		coll.add(new Person("小明3",22));
		
		for(Object obj:coll) {
			Person p =(Person)obj;
			p.method();
		}
	}
}


class Person{
	String name;
	int age;
	
	public Person(String name,int age) {
		this.name = name;
		this.age = age;
	}
	public void method() {
		System.out.println("name:"+name+"\t"+"age:"+age);
	}
}