一、前言
在學(xué)習(xí)java的時(shí)候,我印象最深的一句話是:程序=數(shù)據(jù)結(jié)構(gòu)+算法,對(duì)于寫java程序來說,這就是java的入門。
二、java基本數(shù)據(jù)結(jié)構(gòu)與算法
1、數(shù)據(jù)類型
java中的數(shù)據(jù)類型8種基本數(shù)據(jù)類型:
整型
byte 、short 、int 、long
浮點(diǎn)型
float 、 double
字符型
char
布爾型
boolean
還有包裝類型。所謂包裝類型可以理解為都是類。
2、java常見數(shù)據(jù)結(jié)構(gòu)
棧、隊(duì)列、數(shù)組、鏈表和紅黑樹
3、java常見算法算法
排序算法:冒泡排序、選擇排序、插入排序、希爾排序、歸并排序、快速排序、堆排序等。
查找算法:順序查找、二分查找、哈希查找等。
字符串匹配算法:暴力匹配、KMP算法、Boyer-Moore算法等。
圖論算法:最短路徑算法、最小生成樹算法、拓?fù)渑判虻取?/p>
動(dòng)態(tài)規(guī)劃算法:背包問題、最長(zhǎng)公共子序列、最長(zhǎng)上升子序列等。
三、如何驗(yàn)證:程序=數(shù)據(jù)結(jié)構(gòu)+算法
/**
* 獲取當(dāng)前時(shí)間,格式為:yyyy-MM-dd HH:mm:ss
* @return
*/
public static String getDateStr() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df.format(new Date());
}
?比如上面這段代碼獲取當(dāng)前時(shí)間,格式為:yyyy-MM-dd HH:mm:ss
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat 這個(gè)首先是個(gè)類型,它的算法就是構(gòu)造函數(shù)
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
和
df.format(new Date());
這兩個(gè)算法
返回的String類型其實(shí)也就是數(shù)據(jù)結(jié)構(gòu)
這一段程序涉及到兩個(gè)數(shù)據(jù)結(jié)構(gòu)和兩個(gè)算法
算法1:
? public SimpleDateFormat(String pattern)
? ? {
? ? ? ? this(pattern, Locale.getDefault(Locale.Category.FORMAT));
? ? }
?將這種格式: yyyy-MM-dd HH:mm:ss 作為參數(shù)加工
加工(算法)1:
public SimpleDateFormat(String pattern, Locale locale)
{
if (pattern == null || locale == null) {
throw new NullPointerException();
}
initializeCalendar(locale);
this.pattern = pattern;
this.formatData = DateFormatSymbols.getInstanceRef(locale);
this.locale = locale;
initialize(locale);
}
加工(算法)2:
/* Initialize compiledPattern and numberFormat fields */
private void initialize(Locale loc) {
// Verify and compile the given pattern.
compiledPattern = compile(pattern);
/* try the cache first */
numberFormat = cachedNumberFormatData.get(loc);
if (numberFormat == null) { /* cache miss */
numberFormat = NumberFormat.getIntegerInstance(loc);
numberFormat.setGroupingUsed(false);
/* update cache */
cachedNumberFormatData.putIfAbsent(loc, numberFormat);
}
numberFormat = (NumberFormat) numberFormat.clone();
initializeDefaultCentury();
}
一層一層下來的算法還是很多的。所以
獲取當(dāng)前時(shí)間,格式為:yyyy-MM-dd HH:mm:ss 涉及的算法其實(shí)很多。但我們最終程序輸出的是字符串類型的?yyyy-MM-dd HH:mm:ss,里面嵌套的函數(shù)是一個(gè)個(gè)算法,當(dāng)然算法了也涉及到其他的數(shù)據(jù)類型和結(jié)構(gòu)
一次類推
/* Initialize the fields we use to disambiguate ambiguous years. Separate
* so we can call it from readObject().
*/
private void initializeDefaultCentury() {
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add( Calendar.YEAR, -80 );
parseAmbiguousDatesAsAfter(calendar.getTime());
}
/* Define one-century window into which to disambiguate dates using
* two-digit years.
*/
private void parseAmbiguousDatesAsAfter(Date startDate) {
defaultCenturyStart = startDate;
calendar.setTime(startDate);
defaultCenturyStartYear = calendar.get(Calendar.YEAR);
}
推到最下層
我們發(fā)現(xiàn)是這樣的文章來源:http://www.zghlxwxcb.cn/news/detail-742550.html
@SuppressWarnings("ProtectedField")
protected int fields[];
就是定義了一個(gè)int類型的數(shù)組,所以底層還是數(shù)據(jù)結(jié)構(gòu)。文章來源地址http://www.zghlxwxcb.cn/news/detail-742550.html
到了這里,關(guān)于java入門,程序=數(shù)據(jù)結(jié)構(gòu)+算法的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!