[关闭]
@XQF 2016-11-29T22:18:57.000000Z 字数 6642 阅读 1478

第三次Java上机

java作业


第一题

在JDK文档中查阅Stack接口,尝试用封装LinkedList的方式实现一个Stack容器

思路: Stack类中大致有这么些方法,所以我要使用LinkedList在里面假装一下。

QQ图片20161126210448.png-7.3kB

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

QQ图片20161126210755.png-7.4kB

于是就开始写了:

  1. /**
  2. * Created by XQF on 2016/11/26.
  3. */
  4. public class MyStack<T> {
  5. LinkedList list;
  6. public MyStack() {
  7. list = new LinkedList();
  8. }
  9. public void push(T item) {
  10. list.push(item);
  11. }
  12. public T pop() {
  13. return (T) list.pop();
  14. }
  15. public T peek() {
  16. return (T) list.peek();
  17. }
  18. public boolean empty() {
  19. int size = list.size();
  20. if (size != 0) {
  21. return false;
  22. }
  23. return true;
  24. }
  25. public int search(T item) {
  26. Object[] array = list.toArray();
  27. for (int i = 0; i < array.length; i++) {
  28. if (item.equals((T) array[i])) {
  29. return i + 1;
  30. }
  31. }
  32. return 0;
  33. }
  34. public static void main(String[] args) {
  35. MyStack<Integer> myStack = new MyStack<>();
  36. //往栈里压数据,简单起见只是压入几个整数就可以了
  37. myStack.push(1);
  38. myStack.push(4);
  39. myStack.push(100);
  40. System.out.println("栈是否为空: " + myStack.empty());
  41. System.out.println("栈顶元素:" + myStack.peek());
  42. System.out.println("在栈里面查找4 在第" + myStack.search(4)+"个位置上。");
  43. //弹出所有数据
  44. System.out.println(myStack.pop());
  45. System.out.println(myStack.pop());
  46. System.out.println(myStack.pop());
  47. System.out.println("栈是否为空 " + myStack.empty());
  48. }
  49. }

测试结果:

  1. 栈是否为空: false
  2. 栈顶元素:100
  3. 在栈里面查找4 在第2个位置上。
  4. 100
  5. 4
  6. 1
  7. 栈是否为空 true
  8. Process finished with exit code 0

总结

这里方法不是难,主要是一个泛型使用,还有就是放进List后的元素在取出来的时候是Object类型的,需要向下转型一下。M

第二题

使用,。,。,。,。。,。,。观察输出

  1. /**
  2. * Created by XQF on 2016/11/26.
  3. */
  4. public class MyLinkedList {
  5. public static final char PLUS = '+';
  6. public static final char MINOR = '-';
  7. LinkedList list;
  8. MyLinkedList() {
  9. list = new LinkedList();
  10. }
  11. public void abMethod(String string) {
  12. for (int i = 0; i < string.length(); i++) {
  13. if (string.charAt(i) == MyLinkedList.PLUS) {
  14. i++;
  15. char c = string.charAt(i);
  16. list.push(c);
  17. continue;
  18. } else if (string.charAt(i) == MyLinkedList.MINOR) {
  19. System.out.print(list.poll());
  20. }
  21. }
  22. }
  23. public static void main(String[] args) {
  24. MyLinkedList list = new MyLinkedList();
  25. String string = "+U+n+c---+e+r+t---+a-+i-+n+t+y---+-+r+u--+l+e+s---";
  26. list.abMethod(string);
  27. System.out.println();
  28. }
  29. }

测试结果:

  1. cnUtreaiytnursel
  2. Process finished with exit code 0

第三题

学习第,。,。,。,Set

贴贴TestMap.java文件:

  1. import java.util.*;
  2. public class TestMap{
  3. static String[] s=new String[4];
  4. static {
  5. s[0]="I have a dream that one day this nation will rise up, "
  6. + "live up to the true meaning of its creed: "
  7. + "\"We hold these truths to be self-evident; "
  8. + "that all men are created equal.\"";
  9. s[1]="I have a dream that one day on the red hills of Georgia "
  10. + "the sons of former slaves and the sons of former slave-owners "
  11. + "will be able to sit down together at the table of brotherhood.";
  12. s[2]="I have a dream that one day even the state of Mississippi, "
  13. + "a state sweltering with the heat of injustice, "
  14. + "sweltering with the heat of oppression, "
  15. + "will be transformed into an oasis of freedom and justice";
  16. s[3]="I have a dream that my four children will one day live in a nation "
  17. + "where they will not be judged by the color if their skin "
  18. + "but by the content of their character.";
  19. }
  20. public static void main(String[] args){
  21. Map<String,Integer> statistics=new HashMap<String,Integer>();
  22. for(int i=0;i<s.length;i++){
  23. StringTokenizer st=new StringTokenizer(s[i],",.;:-\" ");
  24. while(st.hasMoreTokens()){
  25. String key=st.nextToken();
  26. if(statistics.get(key)!=null){
  27. statistics.put(key, statistics.get(key)+1);
  28. }
  29. else
  30. statistics.put(key, 1);
  31. }
  32. }
  33. System.out.println(statistics);
  34. }
  35. }

我把它放在目录:

  1. D:\workspace\JavaCode\Assigment\src\com\xqf\third\TestMap.java

我没有使用StringTokenizer,使用的是String的正则表达式,通过\\w+来匹配单词:

  1. /**
  2. * Created by XQF on 2016/11/27.
  3. */
  4. public class AnalyzeTestMap {
  5. public static Set<String> keyWordSet;
  6. 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 ";
  7. public AnalyzeTestMap() {
  8. keyWordSet = new HashSet<String>();
  9. String[] keyWordArray = keyWords.split(" ");
  10. for (int i = 0; i < keyWordArray.length; i++) {
  11. keyWordSet.add(keyWordArray[i]);
  12. }
  13. }
  14. //读取文件内容并处理成格式化的字符串
  15. private String contentReadFromFile() {
  16. File file = new File("D:\\workspace\\JavaCode\\Assigment\\src\\com\\xqf\\third\\TestMap.java");
  17. BufferedReader br = null;
  18. StringBuilder sb = new StringBuilder();
  19. try {
  20. br = new BufferedReader(new FileReader(file));
  21. String string;
  22. while ((string = br.readLine()) != null) {
  23. sb.append(string.trim() + " ");
  24. }
  25. System.out.println(sb.toString());
  26. } catch (Exception e) {
  27. e.printStackTrace();
  28. }
  29. return sb.toString();
  30. }
  31. //根据字符串分割,将单词放进map中统计
  32. private Map<String, Integer> countWordsTimes(String string) {
  33. Map<String, Integer> myMap = new HashMap<String, Integer>();
  34. Matcher m = Pattern.compile("\\w+").matcher(string);
  35. while (m.find()) {
  36. String wordString = m.group();
  37. if (myMap.containsKey(wordString)) {
  38. myMap.put(wordString, myMap.get(wordString) + 1);
  39. } else {
  40. myMap.put(wordString, 1);
  41. }
  42. }
  43. for (String wordKey : myMap.keySet()) {
  44. System.out.println(wordKey + " " + myMap.get(wordKey));
  45. }
  46. return myMap;
  47. }
  48. //根据前面统计好的单词库查询关键字的单词出现字数
  49. private Map<String, Integer> countJavaKeyWordsTimes(Map<String, Integer> wordsMap) {
  50. Map<String, Integer> javaKeyWordsMap = new HashMap<String, Integer>();
  51. for (String wordKey : wordsMap.keySet()) {
  52. if (keyWordSet.contains(wordKey)) {
  53. javaKeyWordsMap.put(wordKey, wordsMap.get(wordKey));
  54. }
  55. }
  56. for (String wordKey : javaKeyWordsMap.keySet()) {
  57. System.out.println(wordKey + " " + wordsMap.get(wordKey));
  58. }
  59. return javaKeyWordsMap;
  60. }
  61. public static void main(String[] args) {
  62. AnalyzeTestMap testMap = new AnalyzeTestMap();
  63. Map<String, Integer> wordsMap = new HashMap<String, Integer>();
  64. Map<String, Integer> keyWordsMap = new HashMap<String, Integer>();
  65. String contentString = testMap.contentReadFromFile();
  66. System.out.println("----------------------------------------------------------");
  67. System.out.println("所有单词出现统计:");
  68. wordsMap = testMap.countWordsTimes(contentString);
  69. System.out.println("----------------------------------------------------------");
  70. System.out.println("关键字单词出现统计:");
  71. keyWordsMap = testMap.countJavaKeyWordsTimes(wordsMap);
  72. System.out.println("----------------------------------------------------------");
  73. }
  74. }

结果:

  1. 字符串,。,。
  2. ----------------------------------------------------------
  3. 所有单词出现统计:
  4. injustice 1
  5. nation 2
  6. truths 1
  7. owners 1
  8. String 6
  9. put 2
  10. these 1
  11. character 1
  12. java 1
  13. sons 2
  14. children 1
  15. meaning 1
  16. four 1
  17. else 1
  18. state 2
  19. if 2
  20. they 1
  21. slaves 1
  22. TestMap 1
  23. in 1
  24. an 1
  25. former 2
  26. 0 2
  27. 1 3
  28. 2 1
  29. Mississippi 1
  30. at 1
  31. 3 1
  32. util 1
  33. 4 1
  34. even 1
  35. nextToken 1
  36. Georgia 1
  37. Map 1
  38. be 4
  39. hasMoreTokens 1
  40. skin 1
  41. I 4
  42. out 1
  43. slave 1
  44. into 1
  45. freedom 1
  46. are 1
  47. by 2
  48. get 2
  49. have 4
  50. where 1
  51. creed 1
  52. together 1
  53. table 1
  54. key 5
  55. a 6
  56. st 3
  57. one 4
  58. i 4
  59. the 10
  60. oppression 1
  61. StringTokenizer 2
  62. s 7
  63. able 1
  64. self 1
  65. to 3
  66. statistics 6
  67. but 1
  68. oasis 1
  69. HashMap 1
  70. main 1
  71. while 1
  72. down 1
  73. hold 1
  74. red 1
  75. that 5
  76. brotherhood 1
  77. sweltering 2
  78. justice 1
  79. up 2
  80. day 4
  81. all 1
  82. new 3
  83. static 3
  84. void 1
  85. created 1
  86. this 1
  87. its 1
  88. my 1
  89. judged 1
  90. null 1
  91. dream 4
  92. true 1
  93. hills 1
  94. heat 2
  95. import 1
  96. color 1
  97. for 1
  98. their 2
  99. content 1
  100. System 1
  101. not 1
  102. println 1
  103. public 2
  104. and 2
  105. of 10
  106. men 1
  107. transformed 1
  108. class 1
  109. live 2
  110. on 1
  111. sit 1
  112. will 5
  113. length 1
  114. We 1
  115. int 1
  116. equal 1
  117. args 1
  118. Integer 2
  119. with 2
  120. evident 1
  121. rise 1
  122. ----------------------------------------------------------
  123. 关键字单词出现统计:
  124. new 3
  125. static 3
  126. void 1
  127. import 1
  128. public 2
  129. else 1
  130. this 1
  131. while 1
  132. if 2
  133. class 1
  134. int 1
  135. ----------------------------------------------------------

国际惯例:总结

老实说这些都是翻开书做的,比如遍历Set,,记不住呀,平时没事的时候也是根本就没有怎么用过,正则表达式也不是很熟,是看着书上的例子来的,。,不过我喜欢这样呀,一个人在图书馆就这样做些有意思的事情,一天就没有了,。有意思。。。下午动土大作业!!!!

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注