@XQF
2016-11-29T22:18:57.000000Z
字数 6642
阅读 1478
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个位置上。
100
4
1
栈是否为空 true
Process 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();
}
}
测试结果:
cnUtreaiytnursel
Process 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);
}
else
statistics.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 1
nation 2
truths 1
owners 1
String 6
put 2
these 1
character 1
java 1
sons 2
children 1
meaning 1
four 1
else 1
state 2
if 2
they 1
slaves 1
TestMap 1
in 1
an 1
former 2
0 2
1 3
2 1
Mississippi 1
at 1
3 1
util 1
4 1
even 1
nextToken 1
Georgia 1
Map 1
be 4
hasMoreTokens 1
skin 1
I 4
out 1
slave 1
into 1
freedom 1
are 1
by 2
get 2
have 4
where 1
creed 1
together 1
table 1
key 5
a 6
st 3
one 4
i 4
the 10
oppression 1
StringTokenizer 2
s 7
able 1
self 1
to 3
statistics 6
but 1
oasis 1
HashMap 1
main 1
while 1
down 1
hold 1
red 1
that 5
brotherhood 1
sweltering 2
justice 1
up 2
day 4
all 1
new 3
static 3
void 1
created 1
this 1
its 1
my 1
judged 1
null 1
dream 4
true 1
hills 1
heat 2
import 1
color 1
for 1
their 2
content 1
System 1
not 1
println 1
public 2
and 2
of 10
men 1
transformed 1
class 1
live 2
on 1
sit 1
will 5
length 1
We 1
int 1
equal 1
args 1
Integer 2
with 2
evident 1
rise 1
----------------------------------------------------------
关键字单词出现统计:
new 3
static 3
void 1
import 1
public 2
else 1
this 1
while 1
if 2
class 1
int 1
----------------------------------------------------------
老实说这些都是翻开书做的,比如遍历Set,,记不住呀,平时没事的时候也是根本就没有怎么用过,正则表达式也不是很熟,是看着书上的例子来的,。,不过我喜欢这样呀,一个人在图书馆就这样做些有意思的事情,一天就没有了,。有意思。。。下午动土大作业!!!!