From e5c0da0533f6347a953b9becc631f49712bf8bf7 Mon Sep 17 00:00:00 2001
From: fengyuxia <474772605@qq.com>
Date: Sun, 5 Mar 2017 21:39:10 +0800
Subject: [PATCH 001/102] no message
---
group04/474772605/.classpath | 4 +
.../com/coderising/action/LoginAction.java | 40 +++++
.../src/com/coderising/action/Struts.java | 167 ++++++++++++++++++
.../src/com/coderising/action/StrutsTest.java | 43 +++++
.../src/com/coderising/action/View.java | 23 +++
.../src/com/coderising/action/struts.xml | 11 ++
.../src/com/coderising/array/ArrayUtil.java | 146 +++++++++++++++
.../src/com/coding/basic/LinkedList.java | 1 +
.../474772605/src/com/coding/basic/List.java | 2 +-
.../474772605/src/com/coding/basic/Node.java | 12 ++
.../474772605/src/com/coding/basic/Stack.java | 34 ++--
.../com/coding/basic/Testclassextends.java | 17 ++
.../src/com/coding/basic/testarraylist.java | 7 +-
.../src/com/coding/iostreams/inteface.java | 11 ++
.../src/com/coding/iostreams/readfile.java | 67 +++++++
.../src/com/coding/iostreams/test.java | 14 ++
group04/474772605/src/struts.xml | 11 ++
group04/474772605/test/Test.java | 50 ++++++
.../test/com/coding/basic/Heros.java | 51 ++++++
.../474772605/test/com/coding/basic/Test.java | 43 +++++
.../test/com/coding/basic/TestStack.java | 35 ++++
.../test/com/coding/basic/Testarray.java | 22 +++
.../test/com/coding/basic/teest.java | 24 +++
23 files changed, 817 insertions(+), 18 deletions(-)
create mode 100644 group04/474772605/src/com/coderising/action/LoginAction.java
create mode 100644 group04/474772605/src/com/coderising/action/Struts.java
create mode 100644 group04/474772605/src/com/coderising/action/StrutsTest.java
create mode 100644 group04/474772605/src/com/coderising/action/View.java
create mode 100644 group04/474772605/src/com/coderising/action/struts.xml
create mode 100644 group04/474772605/src/com/coderising/array/ArrayUtil.java
create mode 100644 group04/474772605/src/com/coding/basic/Node.java
create mode 100644 group04/474772605/src/com/coding/basic/Testclassextends.java
create mode 100644 group04/474772605/src/com/coding/iostreams/inteface.java
create mode 100644 group04/474772605/src/com/coding/iostreams/readfile.java
create mode 100644 group04/474772605/src/com/coding/iostreams/test.java
create mode 100644 group04/474772605/src/struts.xml
create mode 100644 group04/474772605/test/Test.java
create mode 100644 group04/474772605/test/com/coding/basic/Heros.java
create mode 100644 group04/474772605/test/com/coding/basic/Test.java
create mode 100644 group04/474772605/test/com/coding/basic/TestStack.java
create mode 100644 group04/474772605/test/com/coding/basic/Testarray.java
create mode 100644 group04/474772605/test/com/coding/basic/teest.java
diff --git a/group04/474772605/.classpath b/group04/474772605/.classpath
index 28e2b79383..8d7ead9fe8 100644
--- a/group04/474772605/.classpath
+++ b/group04/474772605/.classpath
@@ -1,7 +1,11 @@
+
+
+
+
diff --git a/group04/474772605/src/com/coderising/action/LoginAction.java b/group04/474772605/src/com/coderising/action/LoginAction.java
new file mode 100644
index 0000000000..69ad2750c0
--- /dev/null
+++ b/group04/474772605/src/com/coderising/action/LoginAction.java
@@ -0,0 +1,40 @@
+package com.coderising.action;
+
+/**
+ * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。
+ * @author liuxin
+ *
+ */
+public class LoginAction{
+ private String name ;
+ private String password;
+ private String message;
+
+ public String getName() {
+ return name;
+ }
+
+ public String getPassword() {
+ return password;
+ }
+
+ public String execute(){
+ if("test".equals(name) && "1234".equals(password)){
+ this.message = "login successful";
+ return "success";
+ }
+ this.message = "login failed,please check your user/pwd";
+ return "fail";
+ }
+
+ public void setName(String name){
+
+ this.name = name;
+ }
+ public void setPassword(String password){
+ this.password = password;
+ }
+ public String getMessage(){
+ return this.message;
+ }
+}
diff --git a/group04/474772605/src/com/coderising/action/Struts.java b/group04/474772605/src/com/coderising/action/Struts.java
new file mode 100644
index 0000000000..e2098dff74
--- /dev/null
+++ b/group04/474772605/src/com/coderising/action/Struts.java
@@ -0,0 +1,167 @@
+package com.coderising.action;
+
+import java.io.File;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.dom4j.Attribute;
+import org.dom4j.DocumentException;
+import org.dom4j.Element;
+import org.dom4j.io.SAXReader;
+
+
+
+public class Struts {
+
+
+
+ public static void main(String[] args) throws DocumentException {
+String actionName = "com.coderising.action.LoginAction";
+
+ Map params = new HashMap();
+ params.put("name","test");
+ params.put("password","1234");
+
+
+ View view = Struts.runAction(actionName,params);
+ }
+
+
+
+/* //遍历当前节点下的所有节点
+ public static void listNodes(Element node){
+ System.out.println("当前节点的名称:" + node.getName());
+ //首先获取当前节点的所有属性节点
+ List list = node.attributes();
+ //遍历属性节点
+ for(Attribute attribute : list){
+ System.out.println("属性"+attribute.getName() +":" + attribute.getValue());
+ }
+ //如果当前节点内容不为空,则输出
+ if(!(node.getTextTrim().equals(""))){
+ System.out.println( node.getName() + ":" + node.getText());
+ }
+ //同时迭代当前节点下面的所有子节点
+ //使用递归
+ Iterator iterator = node.elementIterator();
+ while(iterator.hasNext()){
+ Element e = iterator.next();
+ listNodes(e);
+ }
+ } */
+
+ public static View runAction(String actionName, Map parameters) {
+
+ SAXReader reader = new SAXReader();
+ //读取文件 转换成Document
+ org.dom4j.Document document;
+ try {
+ document = reader.read(new File("src/struts.xml"));
+ Element root = document.getRootElement();
+ List childList2 = root.elements("action");
+ Iterator it = childList2.iterator();
+ /* while (it.hasNext()){
+ Element element = it.next();
+ System.out.println("节点的名称" + element.getName() + "节点的值" + element.getText());
+
+ }*/
+ for (int i = 0; i < childList2.size(); i++) {
+ List list = childList2.get(i).attributes();
+ Iterator it1 = list.iterator();
+ while(it1.hasNext()){
+
+ Attribute attribute1 = it1.next();
+ System.out.println(attribute1.getName());
+ System.out.println(attribute1.getValue());
+ System.out.println("==============");
+
+ }
+
+ for(Attribute attribute : list){
+
+ if(actionName.equals(attribute.getValue())){
+
+ String clazz = null;
+
+ clazz = attribute.getValue();
+ try {
+ Object o = Class.forName(clazz).newInstance();
+ try {
+ Method m = o.getClass().getMethod("setName", String.class);
+ Method m1 = o.getClass().getMethod("setPassword", String.class);
+ Method m3 = o.getClass().getMethod("execute");
+ try {
+ m.invoke(o, parameters.get("name"));
+ m1.invoke(o, parameters.get("password"));
+ String result = (String) m3.invoke(o);
+ System.out.println(result);
+ } catch (IllegalArgumentException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (InvocationTargetException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ } catch (SecurityException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (NoSuchMethodException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+
+ } catch (InstantiationException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (IllegalAccessException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (ClassNotFoundException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } //根据class生成实例
+ }
+
+
+ }
+
+ }
+ } catch (DocumentException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ //获取根节点元素对象
+
+
+
+
+ /*
+
+ 0. 读取配置文件struts.xml
+
+ 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象)
+ 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是
+ ("name"="test" , "password"="1234") ,
+ 那就应该调用 setName和setPassword方法
+
+ 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success"
+
+ 3. 通过反射找到对象的所有getter方法(例如 getMessage),
+ 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} ,
+ 放到View对象的parameters
+
+ 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp,
+ 放到View对象的jsp字段中。
+
+ */
+
+ return null;
+ }
+
+}
diff --git a/group04/474772605/src/com/coderising/action/StrutsTest.java b/group04/474772605/src/com/coderising/action/StrutsTest.java
new file mode 100644
index 0000000000..c161c0f932
--- /dev/null
+++ b/group04/474772605/src/com/coderising/action/StrutsTest.java
@@ -0,0 +1,43 @@
+package com.coderising.action;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+
+
+
+
+public class StrutsTest {
+
+ @Test
+ public void testLoginActionSuccess() {
+
+ String actionName = "com.coderising.action.LoginAction";
+
+ Map params = new HashMap();
+ params.put("name","test");
+ params.put("password","1234");
+
+
+ View view = Struts.runAction(actionName,params);
+
+ Assert.assertEquals("/jsp/homepage.jsp", view.getJsp());
+ Assert.assertEquals("login successful", view.getParameters().get("message"));
+ }
+
+ @Test
+ public void testLoginActionFailed() {
+ String actionName = "login";
+ Map params = new HashMap();
+ params.put("name","test");
+ params.put("password","123456"); //密码和预设的不一致
+
+ View view = Struts.runAction(actionName,params);
+
+ Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp());
+ Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message"));
+ }
+}
diff --git a/group04/474772605/src/com/coderising/action/View.java b/group04/474772605/src/com/coderising/action/View.java
new file mode 100644
index 0000000000..11cb1872e5
--- /dev/null
+++ b/group04/474772605/src/com/coderising/action/View.java
@@ -0,0 +1,23 @@
+package com.coderising.action;
+
+import java.util.Map;
+
+public class View {
+ private String jsp;
+ private Map parameters;
+
+ public String getJsp() {
+ return jsp;
+ }
+ public View setJsp(String jsp) {
+ this.jsp = jsp;
+ return this;
+ }
+ public Map getParameters() {
+ return parameters;
+ }
+ public View setParameters(Map parameters) {
+ this.parameters = parameters;
+ return this;
+ }
+}
diff --git a/group04/474772605/src/com/coderising/action/struts.xml b/group04/474772605/src/com/coderising/action/struts.xml
new file mode 100644
index 0000000000..a6cfe43e6c
--- /dev/null
+++ b/group04/474772605/src/com/coderising/action/struts.xml
@@ -0,0 +1,11 @@
+
+
+
+ /jsp/homepage.jsp
+ /jsp/showLogin.jsp
+
+
+ /jsp/welcome.jsp
+ /jsp/error.jsp
+
+
\ No newline at end of file
diff --git a/group04/474772605/src/com/coderising/array/ArrayUtil.java b/group04/474772605/src/com/coderising/array/ArrayUtil.java
new file mode 100644
index 0000000000..ddb00c17b6
--- /dev/null
+++ b/group04/474772605/src/com/coderising/array/ArrayUtil.java
@@ -0,0 +1,146 @@
+package com.coderising.array;
+
+import java.util.ArrayList;
+import java.util.Stack;
+
+import com.coding.basic.LinkedList;
+
+public class ArrayUtil {
+
+ /**
+ * 给定一个整形数组a , 对该数组的值进行置换
+ 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7]
+ 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7]
+ * @param origin
+ * @return
+ */
+ public void reverseArray(int[] origin){
+ Stack stack = new Stack();
+ for (int i = 0; i < origin.length; i++) {
+ stack.add(origin[i]);
+ }
+
+ for (int j = 0; j < stack.size(); j++) {
+ origin[j] = (Integer) stack.pop();
+ }
+
+ }
+
+ /**
+ * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}
+ * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为:
+ * {1,3,4,5,6,6,5,4,7,6,7,5}
+ * @param oldArray
+ * @return
+ */
+
+ public int[] removeZero(int[] oldArray){
+ ArrayList newarray = new ArrayList();
+ for (int i = 0; i < oldArray.length; i++) {
+ if(0!=oldArray[i]){
+ newarray.add(oldArray[i]);
+ }
+ }
+ int result [] = new int [newarray.size()];
+ for (int j = 0; j < result.length; j++) {
+ result[j]=newarray.get(j);
+ }
+
+ return result;
+ }
+
+ /**
+ * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的
+ * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复
+ * @param array1
+ * @param array2
+ * @return
+ */
+
+ public int[] merge(int[] array1, int[] array2){
+ ArrayList newarray = new ArrayList();
+ for (int i = 0; i < array1.length; i++) {
+ newarray.add(i, array1[i]);
+ }
+ for (int j = 0; j < array2.length; j++) {
+ if (newarray.get(j)>array2[j]&&newarray.get(j+1)>array2[j]) {
+ newarray.add(j+1, array2[j]);
+ }
+ }
+
+ int result [] = new int [newarray.size()];
+ for (int z = 0; z < result.length; z++) {
+ result[z]=newarray.get(z);
+ }
+ return result;
+ }
+ /**
+ * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size
+ * 注意,老数组的元素在新数组中需要保持
+ * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为
+ * [2,3,6,0,0,0]
+ * @param oldArray
+ * @param size
+ * @return
+ */
+ public int[] grow(int [] oldArray, int size){
+ ArrayList newarray = new ArrayList();
+ for (int i = 0; i < oldArray.length; i++) {
+ newarray.add(i, oldArray[i]);
+ }
+ while (newarray.size()='A'&&(char)num <='z'){
+ n1++;
+ }
+ if((char)num >='A'&&(char)num <='Z'){
+ n2++;
+ }
+ }
+ }
+ reader.close();
+ System.out.println(n1+""+n2);
+ } catch (FileNotFoundException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+
+
+ public float method(){
+ return 13.21f;
+ }
+
+
+
+
+ }
+
+
+
+
+
+
+
+
+
+
+
diff --git a/group04/474772605/src/com/coding/iostreams/test.java b/group04/474772605/src/com/coding/iostreams/test.java
new file mode 100644
index 0000000000..1bd474ca7b
--- /dev/null
+++ b/group04/474772605/src/com/coding/iostreams/test.java
@@ -0,0 +1,14 @@
+package com.coding.iostreams;
+
+public interface test {
+ public float method();
+
+}
+
+
+
+
+
+
+
+
diff --git a/group04/474772605/src/struts.xml b/group04/474772605/src/struts.xml
new file mode 100644
index 0000000000..90cf18b7da
--- /dev/null
+++ b/group04/474772605/src/struts.xml
@@ -0,0 +1,11 @@
+
+
+
+ /jsp/homepage.jsp
+ /jsp/showLogin.jsp
+
+
+ /jsp/welcome.jsp
+ /jsp/error.jsp
+
+
\ No newline at end of file
diff --git a/group04/474772605/test/Test.java b/group04/474772605/test/Test.java
new file mode 100644
index 0000000000..8c0f62930b
--- /dev/null
+++ b/group04/474772605/test/Test.java
@@ -0,0 +1,50 @@
+
+
+ import java.io.File;
+ import java.io.FileInputStream;
+ import java.io.InputStreamReader;
+ import java.io.Reader;
+
+
+
+
+ public class Test{
+ static int n1 =0 ;
+ static int n2 =0 ;
+
+ public static void main(String[] args) throws Exception{
+
+ Test.readFileByChars("D://Text.txt");
+
+ System.out.println("字母个数为:"+n1+" 字母个数为"+n2);
+
+ }
+
+ public static void readFileByChars(String fileName) {
+ File file = new File(fileName);
+ Reader reader = null;
+ try {
+ System.out.println("以字符为单位读取文件内容,一次读一个字节:");
+ // 一次读一个字符
+ reader = new InputStreamReader(new FileInputStream(file));
+ int tempchar;
+ while ((tempchar = reader.read()) != -1) {
+ // 对于windows下,\r\n这两个字符在一起时,表示一个换行。
+ // 但如果这两个字符分开显示时,会换两次行。
+ // 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。
+ if (((char) tempchar) != '\r') {
+ System.out.print((char) tempchar);
+ n1++;
+ if((char)tempchar >='A'&&(char)tempchar<='Z'){
+ n2++;
+ }
+
+ }
+ }
+ reader.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
diff --git a/group04/474772605/test/com/coding/basic/Heros.java b/group04/474772605/test/com/coding/basic/Heros.java
new file mode 100644
index 0000000000..878204aede
--- /dev/null
+++ b/group04/474772605/test/com/coding/basic/Heros.java
@@ -0,0 +1,51 @@
+package com.coding.basic;
+
+import java.lang.reflect.Method;
+
+public class Heros {
+ private String name;//名字
+ private String type;//类型
+ private int camp;//0,近卫;1,天灾
+ public Heros(){
+
+ }
+ /*
+ public Heros(String name, String type, int camp) {
+
+ super();
+
+ this.name = name;
+ this.type = type;
+ this.camp = camp;
+ }*/
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getType() {
+ return type;
+ }
+
+ public void setType(String type) {
+ this.type = type;
+ }
+
+ public int getCamp() {
+ return camp;
+ }
+
+ public void setCamp(int camp) {
+ this.camp = camp;
+ }
+
+ @Override
+ public String toString() {
+ return "Heros [\n name=" + name + ", \n type=" + type + ", \n camp=" + camp + "\n]";
+ }
+
+}
\ No newline at end of file
diff --git a/group04/474772605/test/com/coding/basic/Test.java b/group04/474772605/test/com/coding/basic/Test.java
new file mode 100644
index 0000000000..97f7254c82
--- /dev/null
+++ b/group04/474772605/test/com/coding/basic/Test.java
@@ -0,0 +1,43 @@
+package com.coding.basic;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+public class Test {
+
+ public static void main(String args[]) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
+ Foo foo = new Foo("这个一个Foo对象!");
+ Class clazz = foo.getClass();
+ Field[] abc =clazz.getDeclaredFields();
+
+ // Object value = getFieldValueByName(key, obj);
+ Method m1 = clazz.getDeclaredMethod("outInfo");
+ Method m2 = clazz.getDeclaredMethod("setMsg", String.class);
+ Method m3 = clazz.getDeclaredMethod("getMsg");
+ m1.invoke(foo);
+ m2.invoke(foo, "重新设置msg信息!");
+ String msg = (String) m3.invoke(foo);
+ System.out.println(msg);
+ }
+}
+
+class Foo {
+ private String msg;
+
+ public Foo(String msg) {
+ this.msg = msg;
+ }
+
+ public void setMsg(String msg) {
+ this.msg = msg;
+ }
+
+ public String getMsg() {
+ return msg;
+ }
+
+ public void outInfo() {
+ System.out.println("这是测试Java反射的测试类");
+ }
+}
diff --git a/group04/474772605/test/com/coding/basic/TestStack.java b/group04/474772605/test/com/coding/basic/TestStack.java
new file mode 100644
index 0000000000..e4caa84d98
--- /dev/null
+++ b/group04/474772605/test/com/coding/basic/TestStack.java
@@ -0,0 +1,35 @@
+package com.coding.basic;
+
+import junit.framework.TestCase;
+
+public class TestStack extends TestCase{
+private Stack stack;
+
+
+public void setUp() throws Exception {
+ stack = new Stack();
+
+}
+
+public void testpop(){
+// Stack stack = new Stack();
+ Object o = null ;
+ try {
+ stack.push(o);
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+
+
+
+}
+
+
+
+
+
+
+
+}
diff --git a/group04/474772605/test/com/coding/basic/Testarray.java b/group04/474772605/test/com/coding/basic/Testarray.java
new file mode 100644
index 0000000000..19edb9f85d
--- /dev/null
+++ b/group04/474772605/test/com/coding/basic/Testarray.java
@@ -0,0 +1,22 @@
+package com.coding.basic;
+
+import junit.framework.TestCase;
+
+public class Testarray extends TestCase{
+
+ public void testararry(){
+ Throwable tx = null;
+ try {
+ ArrayList n = new ArrayList();
+ Object o = null ;
+
+ n.add(o);
+ fail();
+ } catch (Exception e) {
+ tx =e;
+ assertEquals(Exception.class, tx.getClass());
+ assertEquals("对象不能为空", e.getMessage());
+ }
+ }
+
+}
diff --git a/group04/474772605/test/com/coding/basic/teest.java b/group04/474772605/test/com/coding/basic/teest.java
new file mode 100644
index 0000000000..e8004c14b6
--- /dev/null
+++ b/group04/474772605/test/com/coding/basic/teest.java
@@ -0,0 +1,24 @@
+package com.coding.basic;
+
+import java.lang.reflect.Method;
+
+public class teest {
+ public static void main(String[] args) {
+ Class> herosClass = Heros.class;
+ try {
+ Method m1 = herosClass.getMethod("setName",String.class);
+ Method m3 = herosClass.getMethod("setCamp",int.class);
+ Method m2 = herosClass.getMethod("getName");
+
+
+ Object userInfo = herosClass.newInstance();
+ System.out.println("调用构造函数:"+userInfo);
+ m1.invoke(userInfo,"影魔");
+ m3.invoke(userInfo, 1);
+ System.out.println("调用set方法:"+userInfo);
+ System.out.println("调用get方法:"+m2.invoke(userInfo));
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+}
From f67fec97bd72576af1f9bac00419f8ffcfa9f895 Mon Sep 17 00:00:00 2001
From: dudy
Date: Tue, 7 Mar 2017 20:01:06 +0800
Subject: [PATCH 002/102] sortdemo singleton
---
.../com/dudy/learn01/base/MyLinkedList.java | 12 ++
.../singleton/EnumSingleton.java | 40 +++++++
.../singleton/SingletonDemo1.java | 66 +++++++++++
.../singleton/StaticClassInnerSingleton.java | 63 +++++++++++
.../com/dudy/learn01/utils/ArraySortDemo.java | 107 ++++++++++++++++++
5 files changed, 288 insertions(+)
create mode 100644 group04/1796244932/learn01/src/main/java/com/dudy/learn01/designPattern/singleton/EnumSingleton.java
create mode 100644 group04/1796244932/learn01/src/main/java/com/dudy/learn01/designPattern/singleton/SingletonDemo1.java
create mode 100644 group04/1796244932/learn01/src/main/java/com/dudy/learn01/designPattern/singleton/StaticClassInnerSingleton.java
create mode 100644 group04/1796244932/learn01/src/main/java/com/dudy/learn01/utils/ArraySortDemo.java
diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyLinkedList.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyLinkedList.java
index 60254997aa..e0dbe9aae8 100644
--- a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyLinkedList.java
+++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyLinkedList.java
@@ -89,6 +89,17 @@ public Object removeLast() {
return tmp.data;
}
+ /**
+ * 链表逆序
+ */
+ public void reverse(){
+
+ }
+
+
+
+
+
public MyIterator iterator() {
return new MyLinkedListItr();
}
@@ -108,6 +119,7 @@ public Object next() {
}
}
+
private static class Node {
Object data;
Node next;
diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/designPattern/singleton/EnumSingleton.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/designPattern/singleton/EnumSingleton.java
new file mode 100644
index 0000000000..84e9bd5d2c
--- /dev/null
+++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/designPattern/singleton/EnumSingleton.java
@@ -0,0 +1,40 @@
+package com.dudy.learn01.designPattern.singleton;
+
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Created by dudy on 2017/3/6.
+ */
+public enum EnumSingleton {
+
+ SINGLETON;
+ private EnumSingleton(){}
+
+
+ public static void main(String[] args) {
+
+ ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 10, 6000 * 10, TimeUnit.MILLISECONDS,
+ new ArrayBlockingQueue(5));
+
+
+ for (int i = 0; i< 16; i++){
+ executor.execute(new EnumSingletonTest());
+ }
+
+ executor.shutdown();
+
+
+ }
+}
+
+
+class EnumSingletonTest implements Runnable{
+
+ @Override
+ public void run() {
+ EnumSingleton singleton = EnumSingleton.SINGLETON;
+ System.out.println(singleton.hashCode());
+ }
+}
\ No newline at end of file
diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/designPattern/singleton/SingletonDemo1.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/designPattern/singleton/SingletonDemo1.java
new file mode 100644
index 0000000000..8de831c354
--- /dev/null
+++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/designPattern/singleton/SingletonDemo1.java
@@ -0,0 +1,66 @@
+package com.dudy.learn01.designPattern.singleton;
+
+import java.io.Serializable;
+import java.util.concurrent.*;
+
+/**
+ * Created by dudy on 2017/3/6.
+ * 双检索 方式
+ * jdk1.5 以后 其实是线程安全的。
+ * 序列化会 破坏 单利
+ *
+ */
+public class SingletonDemo1 implements Serializable{
+
+ private static volatile SingletonDemo1 singleton = null; // 加 volatile 是为了 可见性,另一个就是 避免重排序
+
+ private SingletonDemo1(){}
+
+ public static SingletonDemo1 getIntance(){
+
+ if (singleton == null){// 第一个避免 在 synchronized 中 一直排队
+ synchronized (SingletonDemo1.class){
+
+ if (singleton == null){// 如果对象为空,才被创建
+ singleton = new SingletonDemo1();
+ }
+
+ }
+ }
+
+ return singleton;
+ }
+
+
+ /**
+ * 解决 反序列化的问题
+ * @return
+ */
+ private Object readResolve() {
+ return singleton;
+ }
+
+ public static void main(String[] args) {
+
+ ExecutorService threadPool = Executors.newFixedThreadPool(10);
+
+ for (int i= 0 ;i < 5; i++){
+ threadPool.execute(new TestRunable());
+ }
+
+ threadPool.shutdown();
+
+ //new ThreadPoolExecutor(10,20,1000*2,new BlockingQueue(),)
+
+
+ }
+
+}
+
+ class TestRunable implements Runnable{
+
+ public void run() {
+ SingletonDemo1 intance = SingletonDemo1.getIntance();
+ System.out.println(intance.hashCode());
+ }
+ }
diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/designPattern/singleton/StaticClassInnerSingleton.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/designPattern/singleton/StaticClassInnerSingleton.java
new file mode 100644
index 0000000000..d8b484cd15
--- /dev/null
+++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/designPattern/singleton/StaticClassInnerSingleton.java
@@ -0,0 +1,63 @@
+package com.dudy.learn01.designPattern.singleton;
+
+import java.util.concurrent.LinkedBlockingDeque;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Created by dudy on 2017/3/6.
+ *
+ * 静态内部类方式 实现
+ *
+ * 这种方式同样利用了classloder的机制来保证初始化instance时只有一个线程,
+ * 它跟饿汉式不同的是(很细微的差别):饿汉式是只要Singleton类被装载了,
+ * 那么instance就会被实例化(没有达到lazy loading效果),而这种方式是Singleton类被装载了,
+ * instance不一定被初始化。因为SingletonHolder类没有被主动使用,
+ * 只有显示通过调用getInstance方法时,才会显示装载SingletonHolder类,
+ * 从而实例化instance。想象一下,如果实例化instance很消耗资源,我想让他延迟加载,
+ * 另外一方面,我不希望在Singleton类加载时就实例化,
+ * 因为我不能确保Singleton类还可能在其他的地方被主动使用从而被加载,
+ * 那么这个时候实例化instance显然是不合适的。这个时候,这种方式相比饿汉式更加合理
+ *
+ */
+public class StaticClassInnerSingleton {
+
+ // 构造器 私有化
+ private StaticClassInnerSingleton(){}
+
+ private static class SingletonHolder{
+ private static final StaticClassInnerSingleton INSTANCE = new StaticClassInnerSingleton();
+ }
+
+
+ public static StaticClassInnerSingleton getInstance(){
+ return SingletonHolder.INSTANCE;
+ }
+
+
+ public static void main(String[] args) {
+
+
+ ThreadPoolExecutor pool = new ThreadPoolExecutor(10, 10,
+ 6000 * 10, TimeUnit.MILLISECONDS,
+ new LinkedBlockingDeque());
+
+ for (int i= 0; i<20; i++){
+ pool.execute(new StaicSingletonTest());
+ //System.out.println(StaticClassInnerSingleton.getInstance().hashCode());
+ }
+
+ pool.shutdown();
+ }
+
+
+
+}
+
+class StaicSingletonTest implements Runnable{
+
+ public void run() {
+ StaticClassInnerSingleton intance = StaticClassInnerSingleton.getInstance();
+ System.out.println(intance.hashCode());
+ }
+}
\ No newline at end of file
diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/utils/ArraySortDemo.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/utils/ArraySortDemo.java
new file mode 100644
index 0000000000..a26790375a
--- /dev/null
+++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/utils/ArraySortDemo.java
@@ -0,0 +1,107 @@
+package com.dudy.learn01.utils;
+
+import java.util.Arrays;
+
+/**
+ * Created by dudy on 2017/3/6.
+ * 练习数组的各种排序
+ * 参考:http://wiki.jikexueyuan.com/project/java-special-topic/sort.html
+ * http://www.cnblogs.com/liuling/p/2013-7-24-01.html
+ *
+ * 内排序有可以分为以下几类:
+
+ (1)、插入排序:直接插入排序、二分法插入排序、希尔排序。
+
+ (2)、选择排序:简单选择排序、堆排序。
+
+ (3)、交换排序:冒泡排序、快速排序。
+
+ (4)、归并排序
+
+ (5)、基数排序
+ *
+ */
+public class ArraySortDemo {
+
+
+ /**
+ * 二分法查找 插入
+ * 和 直接插入排序不同的是: 查找 要插入的位置的方式不同
+ * 二分法前提是有序的**
+ *
+ *
+ *
+ */
+ public static void dichotomySort(int src[]){
+
+ for (int i = 0; i< src.length ; i++){
+ int temp = src[i];
+ int right = i - 1;
+ int mid = 0;
+ int left = 0;
+ while (left <= right){
+ mid = (left + right)/2;
+ if (temp > src[mid]){
+ left = mid + 1;
+ } else {
+ right = mid - 1;
+ }
+ }
+
+ for (int j = i-1;j>=left ; j--){
+ src[j+1] = src[j];
+ }
+
+ System.out.println("left = " + left +" ,mid = " + mid + " ,right = " + right);
+ src[left] = temp;
+
+ }
+ }
+
+
+
+
+ /**
+ * 直接插入排序
+ * 思想:假定前边是有序地部分, 后边无序的插入到前边部分
+ * 可以转变思想: 从后往前遍历, 将有序部分大于当前的值 往后移
+ * @param src
+ */
+ public static void directInsertSort(int[] src){
+
+ for (int i = 1;i < src.length ; i++){
+ // 待插入的元素
+ int temp = src[i];
+ int j;
+ for ( j = i -1; j >= 0; j--){
+ // 大于 temp的往后移动
+ if (src[j] > temp){
+ src[j+1] = src[j];
+ } else {
+ break;
+ }
+ }// 此时遍历完 j+1 为要插入的位置
+ src[j+1] = temp;
+ }
+
+ }
+
+
+
+ public static void main(String[] args) {
+ int a[] = new int[]{46,89,14,44,90,32,25,67,23};
+ // 14,23,25,32,44,46,67,89,90
+ //Arrays.sort(a);
+
+
+ //directInsertSort(a);
+
+ dichotomySort(a);
+
+ for (int i = 0; i< a.length ; i++){
+ System.out.print(a[i] + ",");
+ }
+
+ }
+
+}
From 7d464ac089e796c247ee733a0f8471f02c2fcd66 Mon Sep 17 00:00:00 2001
From: fengyuxia <474772605@qq.com>
Date: Tue, 7 Mar 2017 22:47:59 +0800
Subject: [PATCH 003/102] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E6=AC=A1=E4=BD=9C?=
=?UTF-8?q?=E4=B8=9A?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
group04/474772605/jsp/homepage.jsp | 12 ++
group04/474772605/jsp/showLogin.jsp | 12 ++
.../com/coderising/action/LogoutAction.java | 40 ++++
.../src/com/coderising/action/Struts.java | 181 +++++++-----------
group04/474772605/src/struts.xml | 2 +-
5 files changed, 133 insertions(+), 114 deletions(-)
create mode 100644 group04/474772605/jsp/homepage.jsp
create mode 100644 group04/474772605/jsp/showLogin.jsp
create mode 100644 group04/474772605/src/com/coderising/action/LogoutAction.java
diff --git a/group04/474772605/jsp/homepage.jsp b/group04/474772605/jsp/homepage.jsp
new file mode 100644
index 0000000000..83fa84db7d
--- /dev/null
+++ b/group04/474772605/jsp/homepage.jsp
@@ -0,0 +1,12 @@
+<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
+ pageEncoding="ISO-8859-1"%>
+
+
+
+
+homepage
+
+
+
+
+
\ No newline at end of file
diff --git a/group04/474772605/jsp/showLogin.jsp b/group04/474772605/jsp/showLogin.jsp
new file mode 100644
index 0000000000..1e6cda01b1
--- /dev/null
+++ b/group04/474772605/jsp/showLogin.jsp
@@ -0,0 +1,12 @@
+<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
+ pageEncoding="ISO-8859-1"%>
+
+
+
+
+showLogin
+
+
+
+
+
\ No newline at end of file
diff --git a/group04/474772605/src/com/coderising/action/LogoutAction.java b/group04/474772605/src/com/coderising/action/LogoutAction.java
new file mode 100644
index 0000000000..10e4eb37c7
--- /dev/null
+++ b/group04/474772605/src/com/coderising/action/LogoutAction.java
@@ -0,0 +1,40 @@
+package com.coderising.action;
+
+/**
+ * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。
+ * @author liuxin
+ *
+ */
+public class LogoutAction{
+ private String name ;
+ private String password;
+ private String message;
+
+ public String getName() {
+ return name;
+ }
+
+ public String getPassword() {
+ return password;
+ }
+
+ public String execute(){
+ if("test".equals(name) && "1234".equals(password)){
+ this.message = "login successful";
+ return "success1";
+ }
+ this.message = "login failed,please check your user/pwd";
+ return "fail";
+ }
+
+ public void setName(String name){
+
+ this.name = name;
+ }
+ public void setPassword(String password){
+ this.password = password;
+ }
+ public String getMessage(){
+ return this.message;
+ }
+}
diff --git a/group04/474772605/src/com/coderising/action/Struts.java b/group04/474772605/src/com/coderising/action/Struts.java
index e2098dff74..7db0e4687f 100644
--- a/group04/474772605/src/com/coderising/action/Struts.java
+++ b/group04/474772605/src/com/coderising/action/Struts.java
@@ -21,126 +21,81 @@ public class Struts {
public static void main(String[] args) throws DocumentException {
-String actionName = "com.coderising.action.LoginAction";
+ String actionName = "login";
Map params = new HashMap();
params.put("name","test");
- params.put("password","1234");
-
-
- View view = Struts.runAction(actionName,params);
+ params.put("password","1234");
+ View view = Struts.runAction(actionName,params);
+ System.out.println(view.getJsp());
+ System.out.println(view.getParameters());
}
-
-/* //遍历当前节点下的所有节点
- public static void listNodes(Element node){
- System.out.println("当前节点的名称:" + node.getName());
- //首先获取当前节点的所有属性节点
- List list = node.attributes();
- //遍历属性节点
- for(Attribute attribute : list){
- System.out.println("属性"+attribute.getName() +":" + attribute.getValue());
- }
- //如果当前节点内容不为空,则输出
- if(!(node.getTextTrim().equals(""))){
- System.out.println( node.getName() + ":" + node.getText());
- }
- //同时迭代当前节点下面的所有子节点
- //使用递归
- Iterator iterator = node.elementIterator();
- while(iterator.hasNext()){
- Element e = iterator.next();
- listNodes(e);
- }
- } */
-
public static View runAction(String actionName, Map parameters) {
-
- SAXReader reader = new SAXReader();
+ View view = new View();
+ SAXReader reader = new SAXReader();
//读取文件 转换成Document
- org.dom4j.Document document;
- try {
- document = reader.read(new File("src/struts.xml"));
- Element root = document.getRootElement();
- List childList2 = root.elements("action");
- Iterator it = childList2.iterator();
- /* while (it.hasNext()){
- Element element = it.next();
- System.out.println("节点的名称" + element.getName() + "节点的值" + element.getText());
-
- }*/
- for (int i = 0; i < childList2.size(); i++) {
- List list = childList2.get(i).attributes();
- Iterator it1 = list.iterator();
- while(it1.hasNext()){
-
- Attribute attribute1 = it1.next();
- System.out.println(attribute1.getName());
- System.out.println(attribute1.getValue());
- System.out.println("==============");
-
- }
-
- for(Attribute attribute : list){
-
- if(actionName.equals(attribute.getValue())){
-
- String clazz = null;
-
- clazz = attribute.getValue();
- try {
- Object o = Class.forName(clazz).newInstance();
- try {
- Method m = o.getClass().getMethod("setName", String.class);
- Method m1 = o.getClass().getMethod("setPassword", String.class);
- Method m3 = o.getClass().getMethod("execute");
- try {
- m.invoke(o, parameters.get("name"));
- m1.invoke(o, parameters.get("password"));
- String result = (String) m3.invoke(o);
- System.out.println(result);
- } catch (IllegalArgumentException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- } catch (SecurityException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (NoSuchMethodException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ org.dom4j.Document document;
+ try {
+ document = reader.read(new File("src/struts.xml"));
+ Element root = document.getRootElement();
+ @SuppressWarnings("unchecked")
+ List elements = root.elements();
+ for (Element element : elements) {
+ Attribute actionAttribute = element.attribute("name");
+ Attribute classAttribute = element.attribute("class");
+ if(actionName.equals(actionAttribute.getValue())){
+ String clazz = null;
+ clazz = classAttribute.getValue();
+ Object o = Class.forName(clazz).newInstance();
+ for (Map.Entry entry : parameters.entrySet()) {
+ String name = entry.getKey();
+ String value =entry.getValue();
+ String methodname = "set"+name.substring(0,1).toUpperCase()+name.substring(1);
+ Method m = o.getClass().getMethod(methodname, String.class);
+ m.invoke(o, value);
+
+ }
+ Method m3 = o.getClass().getMethod("execute");
+ String result = (String) m3.invoke(o);
+ String jspPath = null;
+ List element1s = element.elements("result");
+ if(result.equals("success")){
+ for (int i = 0; i < element1s.size(); i++) {
+ Attribute attribute2 = element1s.get(i).attribute("name");
+ if (attribute2.getValue().equals("success")) {
+ jspPath = element1s.get(i).getStringValue();
+ }
+ }
+ }else if(result.equals("fail")){
+ for (int i = 0; i < element1s.size(); i++) {
+ Attribute attribute2 = element1s.get(i).attribute("name");
+ if (attribute2.getValue().equals("fail")) {
+ jspPath = element1s.get(i).getStringValue();
}
-
-
- } catch (InstantiationException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } //根据class生成实例
- }
-
-
- }
-
- }
- } catch (DocumentException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- //获取根节点元素对象
-
-
-
-
+ }
+ }
+ HashMapviewparamterHashMap = new HashMap();
+ Method[]methods = o.getClass().getMethods();
+ String methodname;
+ for (int j = 0; j < o.getClass().getMethods().length; j++) {
+ methodname = methods[j].getName();
+ if(methodname.startsWith("get")&&!methodname.equals("getClass")){
+ String methodname1 = methods[j].getName();
+ methodname1 = methodname.substring(3,4).toUpperCase()+methodname1.substring(4);
+ viewparamterHashMap.put(methodname1, methods[j].invoke(o));
+ }
+ }
+ view.setJsp(jspPath);
+ view.setParameters(viewparamterHashMap);
+ return view;
+ }
+ }
+ } catch (Exception e) {
+ // TODO: handle exception
+ }
+ return null;
/*
0. 读取配置文件struts.xml
@@ -161,7 +116,7 @@ public static View runAction(String actionName, Map parameters)
*/
- return null;
- }
+
+ }
}
diff --git a/group04/474772605/src/struts.xml b/group04/474772605/src/struts.xml
index 90cf18b7da..8bd00bb1f0 100644
--- a/group04/474772605/src/struts.xml
+++ b/group04/474772605/src/struts.xml
@@ -6,6 +6,6 @@
/jsp/welcome.jsp
- /jsp/error.jsp
+ /jsp/error.jsp
\ No newline at end of file
From fb079fa52524dc983f7954bc9a709c8a69ef0545 Mon Sep 17 00:00:00 2001
From: Administrator
Date: Sun, 12 Mar 2017 08:55:30 +0800
Subject: [PATCH 004/102] =?UTF-8?q?add=20=E6=95=B0=E6=8D=AE=E7=BB=93?=
=?UTF-8?q?=E6=9E=84(=E4=B8=89)=20=E5=8D=95=E5=90=91=E9=93=BE=E8=A1=A8?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../one/src/org/coding/one/LinkedList.java | 3 +
.../one/src/org/coding/three/list/List.java | 9 +
.../coding/three/list/impl/LinkedList.java | 422 ++++++++++++++++
.../three/list/impl/LinkedListTest.java | 477 ++++++++++++++++++
4 files changed, 911 insertions(+)
create mode 100644 group04/498654356/one/src/org/coding/three/list/List.java
create mode 100644 group04/498654356/one/src/org/coding/three/list/impl/LinkedList.java
create mode 100644 group04/498654356/one/test/org/coding/three/list/impl/LinkedListTest.java
diff --git a/group04/498654356/one/src/org/coding/one/LinkedList.java b/group04/498654356/one/src/org/coding/one/LinkedList.java
index 183f634418..3f98db326f 100644
--- a/group04/498654356/one/src/org/coding/one/LinkedList.java
+++ b/group04/498654356/one/src/org/coding/one/LinkedList.java
@@ -1,5 +1,8 @@
package org.coding.one;
+/**
+ * 双链表/双向链表
+ */
public class LinkedList implements List {
private Node first;
diff --git a/group04/498654356/one/src/org/coding/three/list/List.java b/group04/498654356/one/src/org/coding/three/list/List.java
new file mode 100644
index 0000000000..d06cf962da
--- /dev/null
+++ b/group04/498654356/one/src/org/coding/three/list/List.java
@@ -0,0 +1,9 @@
+package org.coding.three.list;
+
+public interface List {
+ public void add(Object o);
+ public void add(int index, Object o);
+ public Object get(int index);
+ public Object remove(int index);
+ public int size();
+}
diff --git a/group04/498654356/one/src/org/coding/three/list/impl/LinkedList.java b/group04/498654356/one/src/org/coding/three/list/impl/LinkedList.java
new file mode 100644
index 0000000000..ef38ca811d
--- /dev/null
+++ b/group04/498654356/one/src/org/coding/three/list/impl/LinkedList.java
@@ -0,0 +1,422 @@
+package org.coding.three.list.impl;
+
+import java.util.Arrays;
+import java.util.Iterator;
+
+import org.coding.three.list.List;
+/**
+ * 单链表/单向链表
+ */
+public class LinkedList implements List {
+ /**
+ * 0. head 节点存储数据
+ * 1. 这里的 head 第一次添加之后 "引用" 将不再改变;"值" 可以被修改已表示往首节点插入新的值。
+ * 2. 可以将 head 修改为对 node 引用, 不存储任何数据。
+ */
+ private Node head;
+
+ public void add(Object o){
+ Node node = new Node(o);
+ if(head == null){ //第一次
+ head = node;
+ } else {
+ getNode(size() - 1).next = node;
+ }
+ }
+
+ private Node getNode(int index) {
+ checkIndex(index);
+ Node node = head;
+ for(int i = 0; i < index; i++) {
+ node = node.next;
+ }
+ return node;
+ }
+
+ private void checkIndex(int index) {
+ int size = size();
+ if(index < 0 || index > (size - 1)) {
+ throw new IndexOutOfBoundsException("size = " + size + ", index = " + index);
+ }
+ }
+ public void add(int index , Object o){
+ checkIndex(index);
+ if(index == 0) { //更新 head 的值, 将旧值创建新的Node插入到 head 后
+ Object data = head.data;
+ head.data = o;
+ Node node = new Node(data);
+ node.next = head.next;
+ head.next = node;
+ } else {
+ Node pre = getNode(index - 1);
+ Node node = new Node(o);
+ node.next = pre.next;
+ pre.next = node;
+ }
+ }
+ public Object get(int index){
+ checkIndex(index);
+ return getNode(index).data;
+ }
+ public Object remove(int index){
+ checkIndex(index);
+ Object data = null;
+ if(index == 0) {
+ Node next = head.next;
+ data = head.data;
+ if(next == null) {
+ head = null;
+ } else {
+ head.data = next.data;
+ head.next = next.next;
+ next.next = null;
+ }
+ } else {
+ Node pre = getNode(index - 1);
+ Node node = pre.next;
+ pre.next = node.next;
+ node.next = null;
+ data = node.data;
+ }
+ return data;
+ }
+
+ public int size(){
+ Node temp = head;
+ int size = 0;
+ while(temp != null) {
+ size++;
+ temp = temp.next;
+ }
+ return size;
+ }
+
+ public void addFirst(Object o){
+ add(0, o);
+ }
+ public void addLast(Object o){
+ add(o);
+ }
+ public Object removeFirst(){
+ return remove(0);
+ }
+ public Object removeLast(){
+ return remove(size() - 1);
+ }
+ public Iterator> iterator(){
+ return new LinkedIterator();
+ }
+
+ class LinkedIterator implements Iterator