@XQF
2016-11-29T14:18:57.000000Z
字数 6642
阅读 1612
java作业
在JDK文档中查阅Stack接口,尝试用封装LinkedList的方式实现一个Stack容器
思路: Stack类中大致有这么些方法,所以我要使用LinkedList在里面假装一下。

然后我查了一下LinkedList,部分方法:

于是就开始写了:
/*** Created by XQF on 2016/11/26.*/public class MyStack<T> {LinkedList list;public MyStack() {list = new LinkedList();}public void push(T item) {list.push(item);}public T pop() {return (T) list.pop();}public T peek() {return (T) list.peek();}public boolean empty() {int size = list.size();if (size != 0) {return false;}return true;}public int search(T item) {Object[] array = list.toArray();for (int i = 0; i < array.length; i++) {if (item.equals((T) array[i])) {return i + 1;}}return 0;}public static void main(String[] args) {MyStack<Integer> myStack = new MyStack<>();//往栈里压数据,简单起见只是压入几个整数就可以了myStack.push(1);myStack.push(4);myStack.push(100);System.out.println("栈是否为空: " + myStack.empty());System.out.println("栈顶元素:" + myStack.peek());System.out.println("在栈里面查找4 在第" + myStack.search(4)+"个位置上。");//弹出所有数据System.out.println(myStack.pop());System.out.println(myStack.pop());System.out.println(myStack.pop());System.out.println("栈是否为空 " + myStack.empty());}}
测试结果:
栈是否为空: false栈顶元素:100在栈里面查找4 在第2个位置上。10041栈是否为空 trueProcess finished with exit code 0
这里方法不是难,主要是一个泛型使用,还有就是放进List后的元素在取出来的时候是Object类型的,需要向下转型一下。M
使用,。,。,。,。。,。,。观察输出
/*** Created by XQF on 2016/11/26.*/public class MyLinkedList {public static final char PLUS = '+';public static final char MINOR = '-';LinkedList list;MyLinkedList() {list = new LinkedList();}public void abMethod(String string) {for (int i = 0; i < string.length(); i++) {if (string.charAt(i) == MyLinkedList.PLUS) {i++;char c = string.charAt(i);list.push(c);continue;} else if (string.charAt(i) == MyLinkedList.MINOR) {System.out.print(list.poll());}}}public static void main(String[] args) {MyLinkedList list = new MyLinkedList();String string = "+U+n+c---+e+r+t---+a-+i-+n+t+y---+-+r+u--+l+e+s---";list.abMethod(string);System.out.println();}}
测试结果:
cnUtreaiytnurselProcess finished with exit code 0
学习第,。,。,。,Set
贴贴TestMap.java文件:
import java.util.*;public class TestMap{static String[] s=new String[4];static {s[0]="I have a dream that one day this nation will rise up, "+ "live up to the true meaning of its creed: "+ "\"We hold these truths to be self-evident; "+ "that all men are created equal.\"";s[1]="I have a dream that one day on the red hills of Georgia "+ "the sons of former slaves and the sons of former slave-owners "+ "will be able to sit down together at the table of brotherhood.";s[2]="I have a dream that one day even the state of Mississippi, "+ "a state sweltering with the heat of injustice, "+ "sweltering with the heat of oppression, "+ "will be transformed into an oasis of freedom and justice";s[3]="I have a dream that my four children will one day live in a nation "+ "where they will not be judged by the color if their skin "+ "but by the content of their character.";}public static void main(String[] args){Map<String,Integer> statistics=new HashMap<String,Integer>();for(int i=0;i<s.length;i++){StringTokenizer st=new StringTokenizer(s[i],",.;:-\" ");while(st.hasMoreTokens()){String key=st.nextToken();if(statistics.get(key)!=null){statistics.put(key, statistics.get(key)+1);}elsestatistics.put(key, 1);}}System.out.println(statistics);}}
我把它放在目录:
D:\workspace\JavaCode\Assigment\src\com\xqf\third\TestMap.java
我没有使用StringTokenizer,使用的是String的正则表达式,通过\\w+来匹配单词:
/*** Created by XQF on 2016/11/27.*/public class AnalyzeTestMap {public static Set<String> keyWordSet;public static final String keyWords = "boolean byte short int long double char float double import package class extends implements interface if else switch do while case break continue return default while try catch finally throw throws abstract final native private protected public static synchronized transient volatile new instanceof this super void assert const enum goto strictfp ";public AnalyzeTestMap() {keyWordSet = new HashSet<String>();String[] keyWordArray = keyWords.split(" ");for (int i = 0; i < keyWordArray.length; i++) {keyWordSet.add(keyWordArray[i]);}}//读取文件内容并处理成格式化的字符串private String contentReadFromFile() {File file = new File("D:\\workspace\\JavaCode\\Assigment\\src\\com\\xqf\\third\\TestMap.java");BufferedReader br = null;StringBuilder sb = new StringBuilder();try {br = new BufferedReader(new FileReader(file));String string;while ((string = br.readLine()) != null) {sb.append(string.trim() + " ");}System.out.println(sb.toString());} catch (Exception e) {e.printStackTrace();}return sb.toString();}//根据字符串分割,将单词放进map中统计private Map<String, Integer> countWordsTimes(String string) {Map<String, Integer> myMap = new HashMap<String, Integer>();Matcher m = Pattern.compile("\\w+").matcher(string);while (m.find()) {String wordString = m.group();if (myMap.containsKey(wordString)) {myMap.put(wordString, myMap.get(wordString) + 1);} else {myMap.put(wordString, 1);}}for (String wordKey : myMap.keySet()) {System.out.println(wordKey + " " + myMap.get(wordKey));}return myMap;}//根据前面统计好的单词库查询关键字的单词出现字数private Map<String, Integer> countJavaKeyWordsTimes(Map<String, Integer> wordsMap) {Map<String, Integer> javaKeyWordsMap = new HashMap<String, Integer>();for (String wordKey : wordsMap.keySet()) {if (keyWordSet.contains(wordKey)) {javaKeyWordsMap.put(wordKey, wordsMap.get(wordKey));}}for (String wordKey : javaKeyWordsMap.keySet()) {System.out.println(wordKey + " " + wordsMap.get(wordKey));}return javaKeyWordsMap;}public static void main(String[] args) {AnalyzeTestMap testMap = new AnalyzeTestMap();Map<String, Integer> wordsMap = new HashMap<String, Integer>();Map<String, Integer> keyWordsMap = new HashMap<String, Integer>();String contentString = testMap.contentReadFromFile();System.out.println("----------------------------------------------------------");System.out.println("所有单词出现统计:");wordsMap = testMap.countWordsTimes(contentString);System.out.println("----------------------------------------------------------");System.out.println("关键字单词出现统计:");keyWordsMap = testMap.countJavaKeyWordsTimes(wordsMap);System.out.println("----------------------------------------------------------");}}
结果:
字符串,。,。----------------------------------------------------------所有单词出现统计:injustice 1nation 2truths 1owners 1String 6put 2these 1character 1java 1sons 2children 1meaning 1four 1else 1state 2if 2they 1slaves 1TestMap 1in 1an 1former 20 21 32 1Mississippi 1at 13 1util 14 1even 1nextToken 1Georgia 1Map 1be 4hasMoreTokens 1skin 1I 4out 1slave 1into 1freedom 1are 1by 2get 2have 4where 1creed 1together 1table 1key 5a 6st 3one 4i 4the 10oppression 1StringTokenizer 2s 7able 1self 1to 3statistics 6but 1oasis 1HashMap 1main 1while 1down 1hold 1red 1that 5brotherhood 1sweltering 2justice 1up 2day 4all 1new 3static 3void 1created 1this 1its 1my 1judged 1null 1dream 4true 1hills 1heat 2import 1color 1for 1their 2content 1System 1not 1println 1public 2and 2of 10men 1transformed 1class 1live 2on 1sit 1will 5length 1We 1int 1equal 1args 1Integer 2with 2evident 1rise 1----------------------------------------------------------关键字单词出现统计:new 3static 3void 1import 1public 2else 1this 1while 1if 2class 1int 1----------------------------------------------------------
老实说这些都是翻开书做的,比如遍历Set,,记不住呀,平时没事的时候也是根本就没有怎么用过,正则表达式也不是很熟,是看着书上的例子来的,。,不过我喜欢这样呀,一个人在图书馆就这样做些有意思的事情,一天就没有了,。有意思。。。下午动土大作业!!!!