From 9d80aac9b0090f6d2d5c21e5b32fb8d4a82c7298 Mon Sep 17 00:00:00 2001 From: starlight0405 Date: Mon, 27 Feb 2017 17:16:25 +0800 Subject: [PATCH 1/9] first code --- group16/2816977791/.gitignore | 181 ++++++++++++++++++ .../src/com/coding/basic/ArrayList.java | 94 +++++++++ .../src/com/coding/basic/BinaryTreeNode.java | 67 +++++++ .../src/com/coding/basic/Iterator.java | 7 + .../src/com/coding/basic/LinkedList.java | 140 ++++++++++++++ .../src/com/coding/basic/List.java | 13 ++ .../src/com/coding/basic/Queue.java | 21 ++ .../src/com/coding/basic/Stack.java | 25 +++ 8 files changed, 548 insertions(+) create mode 100644 group16/2816977791/.gitignore create mode 100644 group16/2816977791/firstExercise/src/com/coding/basic/ArrayList.java create mode 100644 group16/2816977791/firstExercise/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group16/2816977791/firstExercise/src/com/coding/basic/Iterator.java create mode 100644 group16/2816977791/firstExercise/src/com/coding/basic/LinkedList.java create mode 100644 group16/2816977791/firstExercise/src/com/coding/basic/List.java create mode 100644 group16/2816977791/firstExercise/src/com/coding/basic/Queue.java create mode 100644 group16/2816977791/firstExercise/src/com/coding/basic/Stack.java diff --git a/group16/2816977791/.gitignore b/group16/2816977791/.gitignore new file mode 100644 index 0000000000..4ede1404d2 --- /dev/null +++ b/group16/2816977791/.gitignore @@ -0,0 +1,181 @@ +# Created by https://www.gitignore.io/api/java,intellij,eclipse,emacs,svn,maven + +### Java ### +*.class + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + + +### Intellij ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/workspace.xml +.idea/tasks.xml +.idea/dictionaries +.idea/vcs.xml +.idea/jsLibraryMappings.xml +*.idea + +# Sensitive or high-churn files: +.idea/dataSources.ids +.idea/dataSources.xml +.idea/dataSources.local.xml +.idea/sqlDataSources.xml +.idea/dynamic.xml +.idea/uiDesigner.xml + +# Gradle: +.idea/gradle.xml +.idea/libraries + +# Mongo Explorer plugin: +.idea/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +/out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +### Intellij Patch ### +*.iml + + +### Eclipse ### + +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.settings/ +.loadpath +.recommenders + +# Eclipse Core +.project + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# PyDev specific (Python IDE for Eclipse) +*.pydevproject + +# CDT-specific (C/C++ Development Tooling) +.cproject + +# JDT-specific (Eclipse Java Development Tools) +.classpath + +# Java annotation processor (APT) +.factorypath + +# PDT-specific (PHP Development Tools) +.buildpath + +# sbteclipse plugin +.target + +# Tern plugin +.tern-project + +# TeXlipse plugin +.texlipse + +# STS (Spring Tool Suite) +.springBeans + +# Code Recommenders +.recommenders/ + + +### Emacs ### +# -*- mode: gitignore; -*- +*~ +\#*\# +/.emacs.desktop +/.emacs.desktop.lock +*.elc +auto-save-list +tramp +.\#* + +# Org-mode +.org-id-locations +*_archive + +# flymake-mode +*_flymake.* + +# eshell files +/eshell/history +/eshell/lastdir + +# elpa packages +/elpa/ + +# reftex files +*.rel + +# AUCTeX auto folder +/auto/ + +# cask packages +.cask/ + +# Flycheck +flycheck_*.el + +# server auth directory +/server/ + +# projectiles files +.projectile + +### SVN ### +.svn/ + + +### Maven ### +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties + +Servers/ diff --git a/group16/2816977791/firstExercise/src/com/coding/basic/ArrayList.java b/group16/2816977791/firstExercise/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..7ac196b700 --- /dev/null +++ b/group16/2816977791/firstExercise/src/com/coding/basic/ArrayList.java @@ -0,0 +1,94 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[10]; + + public void add(Object o) { + ensureCapacity(size + 1); + elementData[size++] = o; + } + + public void add(int index, Object o) { + checkForLength(index); + + ensureCapacity(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + public Object get(int index) { + checkForLength(index); + return elementData[index]; + } + + public Object remove(int index) { + checkForLength(index); + Object oldValue = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + size--; + return oldValue; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return null; + } + + private void checkForLength(int index) { + if (index < 0 || index >= size) { + throw new RuntimeException("out of memory"); + } + } + + private void ensureCapacity(int minCapacity) { + if (minCapacity - elementData.length > 0) { + grow(minCapacity); + } + } + + private void grow(int minCapacity) { + int oldCapacity = elementData.length; + int newCapacity = oldCapacity + (oldCapacity >> 1);//增大容量 + if (newCapacity - minCapacity < 0) { + newCapacity = minCapacity; + } + elementData = copyOf(elementData, newCapacity); + } + + private Object[] copyOf(Object[] src, int newCapacity) { + Object[] target = new Object[newCapacity]; + System.arraycopy(src, 0, target, 0, src.length); + return target; + } + + public static void main(String[] args) { + ArrayList list = new ArrayList(); + String num = "num"; + for (int i = 0; i < 100; i++) { + list.add(num + String.valueOf(i)); + System.out.println(String.valueOf(i) + ":size:" + list.size()); + System.out.println(String.valueOf(i) + ":length:" + list.elementData.length); + } + System.out.println(list.size()); + + for (int i = 0; i < 100; i++) { + list.add(i, num + String.valueOf(i)); + System.out.println(String.valueOf(i) + ":size:" + list.size()); + System.out.println(String.valueOf(i) + ":length:" + list.elementData.length); + } + System.out.println(list.size()); + + for (int i = 0; i < 200; i++) { + System.out.println(list.remove(0)); + } + + System.out.println(list.size()); + } +} diff --git a/group16/2816977791/firstExercise/src/com/coding/basic/BinaryTreeNode.java b/group16/2816977791/firstExercise/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..e34a68b3c2 --- /dev/null +++ b/group16/2816977791/firstExercise/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,67 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private BinaryTreeNode root; + + public BinaryTreeNode(Object data, BinaryTreeNode left, BinaryTreeNode right) { + this.data = data; + this.left = left; + this.right = right; + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o) { + BinaryTreeNode binaryTreeNode = new BinaryTreeNode(o, null, null); + if (root == null) { + root = binaryTreeNode; + } else { + add(o, root); + } + return binaryTreeNode; + } + + private BinaryTreeNode add(Object o, BinaryTreeNode target) { + if (target == null) { + target = new BinaryTreeNode(o, null, null); + } else { + if (compare(o, target.data) > 0) { + target.right = add(o, target.right); + } else if (compare(o, target.data) < 0) { + target.left = add(o, target.left); + } + } + return target; + } + + private int compare(Object src, Object target) { + return ((String) src).compareTo((String) target); + } + +} diff --git a/group16/2816977791/firstExercise/src/com/coding/basic/Iterator.java b/group16/2816977791/firstExercise/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group16/2816977791/firstExercise/src/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group16/2816977791/firstExercise/src/com/coding/basic/LinkedList.java b/group16/2816977791/firstExercise/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..be5b236700 --- /dev/null +++ b/group16/2816977791/firstExercise/src/com/coding/basic/LinkedList.java @@ -0,0 +1,140 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private Node last; + + private int size = 0; + + public void add(Object o) { + addLast(o); + } + + public void add(int index, Object o) { + checkPosition(index); + if (index == size) { + addLast(o); + } else { + addIndex(index, o); + } + } + + public Object get(int index) { + return node(index).data; + } + + public Object remove(int index) { + checkPosition(index); + Object old = get(index); + if (index == 0) { + removeFirst(); + } else if (index == size - 1) { + removeLast(); + } else { + node(index - 1).next = node(index + 1); + size--; + } + return old; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node h = head; + Node newNode = new Node(o, h); + head = newNode; + if (h == null) { + last = newNode; + } + size++; + } + + public void addLast(Object o) { + Node l = last; + Node newNode = new Node(o, null); + last = newNode; + if (l == null) { + head = newNode; + } else { + l.next = newNode; + } + size++; + } + + public Object removeFirst() { + Node old = head; + head = old.next; + size--; + return old.data; + } + + public Object removeLast() { + Node old = last; + Node prev = node(size - 2); + last = prev; + size--; + return old.data; + } + + public Iterator iterator() { + return null; + } + + private void checkPosition(int index) { + if (index < 0 || index >= size) { + throw new RuntimeException("out of memory"); + } + } + + private void addIndex(int index, Object o) { + checkPosition(index); + Node newNode = new Node(o, node(index)); + if (index != 0) { + node(index - 1).next = newNode; + } else { + head = newNode; + } + size++; + } + + private Node node(int index) { + Node x = head; + for (int i = 0; i < index; i++) { + x = x.next; + } + return x; + } + + private static class Node { + Object data; + Node next; + + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + public static void main(String[] args) { + LinkedList list = new LinkedList(); + list.addLast("last"); + System.out.println(list.size()); + list.addFirst("head"); + System.out.println(list.size()); + list.add(0, "0Object"); + System.out.println(list.size()); + list.add(2, "2Object"); + System.out.println(list.size()); + list.removeLast(); + System.out.println(list.size()); + list.removeFirst(); + System.out.println(list.size()); + list.removeLast(); + System.out.println(list.size()); + list.get(0); + System.out.println(list.size()); + } +} diff --git a/group16/2816977791/firstExercise/src/com/coding/basic/List.java b/group16/2816977791/firstExercise/src/com/coding/basic/List.java new file mode 100644 index 0000000000..01398944e6 --- /dev/null +++ b/group16/2816977791/firstExercise/src/com/coding/basic/List.java @@ -0,0 +1,13 @@ +package com.coding.basic; + +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/group16/2816977791/firstExercise/src/com/coding/basic/Queue.java b/group16/2816977791/firstExercise/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..f2475fcfd6 --- /dev/null +++ b/group16/2816977791/firstExercise/src/com/coding/basic/Queue.java @@ -0,0 +1,21 @@ +package com.coding.basic; + +public class Queue { + private ArrayList elementData = new ArrayList(); + + public void enQueue(Object o) { + elementData.add(o); + } + + public Object deQueue() { + return elementData.get(0); + } + + public boolean isEmpty() { + return elementData.size() == 0 ? true : false; + } + + public int size() { + return elementData.size(); + } +} diff --git a/group16/2816977791/firstExercise/src/com/coding/basic/Stack.java b/group16/2816977791/firstExercise/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..12a89eb613 --- /dev/null +++ b/group16/2816977791/firstExercise/src/com/coding/basic/Stack.java @@ -0,0 +1,25 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + elementData.add(0, o); + } + + public Object pop() { + return elementData.remove(0); + } + + public Object peek() { + return elementData.get(0); + } + + public boolean isEmpty() { + return elementData.size() == 0 ? true : false; + } + + public int size() { + return elementData.size(); + } +} From e8f81f5a95f72b465eee77cbd08805c19ba9a7f8 Mon Sep 17 00:00:00 2001 From: wanghongdi56 <313001956@qq.com> Date: Tue, 28 Feb 2017 13:08:30 +0800 Subject: [PATCH 2/9] =?UTF-8?q?0226=E4=BD=9C=E4=B8=9A=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/coding/basic/ArrayList.java | Bin 453 -> 1503 bytes .../src/com/coding/basic/LinkedList.java | Bin 0 -> 2332 bytes .../313001956/src/com/coding/basic/Queue.java | Bin 0 -> 413 bytes .../313001956/src/com/coding/basic/Stack.java | Bin 0 -> 666 bytes 4 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 group16/313001956/src/com/coding/basic/LinkedList.java create mode 100644 group16/313001956/src/com/coding/basic/Queue.java create mode 100644 group16/313001956/src/com/coding/basic/Stack.java diff --git a/group16/313001956/src/com/coding/basic/ArrayList.java b/group16/313001956/src/com/coding/basic/ArrayList.java index eaaa690fa6b76c4f36483531b915bbdd3a22c926..a6421a30fc0461adbe89407b4c18ef5a9edd6984 100644 GIT binary patch delta 1357 zcmV-T1+x0Z1K$e=ViY4~tN>Jz2c>@*7dhU!G;VIOOeTvh;Vg=Q^_LR6%O!>P>sNWJ zvc*Zu3G^Y%`ZkOb-sH5IGQll4m9w%9!~wSbBFCqAeI+od&9dn)|YFug{Y8WuzCJ@)bAp@s0;P9%h#6*g@@by|N$M{B>) zZ1LnDoxe<+m`^YRx~rVUY`-_|@W>&jWTZZAnsNO<9#H=@(=*&fOeb}sjHJT;3k_9f z>@EVURH^USKy>FR(j9|McDQ1K69&39>>V{cOl7?knYlV{>lqi=7Rc!MvQGg(C}4jc z$M>H?ox7nO(S}}Gex_|oZ0djVX?rkO`0CeGm+m{O%D#vJnCm@CUa?1$R;_r4QmQI0 z7V7;I4L=q-A_w!Y5uutrS$ z4HPp#wzD}eA-w`2fjIAT#nrrp3AMl~lKb#XN`cI#L)$`?LZ;JPlpBBF_~ZC4);nR4 zw51%JC$9Mc#E5ccKPQ-QqL8O|yy@Qs)6_qciG!J+Ps~)!4>9-UeNy;OBeg?wPn!HD zzXQipZEd6?|3K-Z$e5>QYTC~pB62nwn>KAE?Xm}uZXcBHR*swsU=8Z&)r&g(m^VQ^ zkUhY5?+!*LyBmFZ_jG@qEVMUi4n3vltkAHlM>r$BGJp9u$AjH%Rn8UkG&nybAwt(B zV!c1$5p`xQEY)qDwl;V7S6$F{ku{?yDotHcJ1I<#{Ddy^)_~lo+*Gz-oCzjHzt+ce zJ%8l-+h2<1JJ^*`%1eyWQ6^*eO^-+HVvKdJA}=5H=QC+FBshOAEyv+VMV%G)yE!TgCZtZ!bp2wjx$0;oLyR!?0 z5xC+$+v0eCG%k)?uD7FesjQF!m6>7YqoJ_J1B z3@$uWNv=eZt~O}%O-ANF%rEssO(0545_|)|v(Z+Sv$CSguS}4BstzS%?fVVT|EnW1 z4x8?b66b|d*-4C-7BzUbK*4&VXpnTODPw%^nh}3V95zz~45z?O>yVdu@d_L&7MQ54 z5(tVjw7T0l$xT4*BBt)&U#RP=fk6@iFJqHvmdZ3w_n*8@KWZvI?r`Qhi@$CiABv}a zv^4TY9#r2T5DhRBw#Vs*A$XHpLP~A^ z#8Ex`+%RDQpo!Nqus#H&nE`3+4GQikiL-xyg)}|uFvzxehTfB;%XgAz17;B5a!RDF Ps5HGzQ;=D zQqXLf1SoyE1II~$3CWr2PH@f2MSm)xoUT}iK9-gMGT8jt!5>5Ffk66 zZ5pdrs=iC5GZ{ss zLsT-eGC${AeIJj{U+{hZ@_M}=kN0oS$Mfw16vF1wxYN?m(9mkZ%$(h^hFJXXMc+tM z?|1w=($=ifCOB}0F|T7DLhPjea=AC7yx9jP(#Q(Oo&*5&@8SQg@PZU6?MPqWdVNtM zY!R9ext|T_92SW=ShEVMX?U$W%)BQ!ax9k!pe#3KLOzQLcV$(wr0O&*sEm^Q2&(eM z@}DAF#2SA#;|}-m>#L{RQTii@en#nz6$1Jr$d-aJ65rlYDy4tKC?9NWno+eCNlcAp z_)t%t#FW@uB|@N{zbKwZ`Zj0Nn4$tBm)4Z@x+U;Q2CI#GWY7@ei#J2{EC;}(+dmst z<%*@R9Zam8q#nfCnx2Bg9|z6n#hiwZwGEG<9j&BT9(jfA$}aGS0F5Q*?zED`EcSx6 z7lR_jy5z+4bsRX{uf^8mif$^1vo9e8i$Z6aMYV=o-(0%x-_#JznY0^l!hxU{cRMk1 zQYC;_^hVi6?pI6RzScNoc|k1_wM(lpvIzP0{mN5#_n%&23&4u@fPg9SLn2xf0)tZ z1zjrmI>BswtAx`c*LWx*2Fpd%dVwV1!o<0=Ji!!`L?;WlmT47N*0J&(ss$kT>bgUV zpnyIPS{rFH_-oiFt8@Kr+m_?erFp0pxT3q+_(E_|>~rO$celL9?^QHzSaLxYw$Lmul4EA> z;>ie1{Oa?K0IAM&mB2c zVo=1hjTqVqAu~NLEF4EnDIjZ_7Fm)2-7T7xeNOy#hKowov5#>jOQ}~gGo8A>Z0{b<2zg>XbrF{{A1uIMoqWt*7~F^5?rg)VLlU2TJF_^<-Q^4GT9oZ3kiGvih}LZ zI(O}=(Fzl@ei18YG~XxOfSPLud+RG+4yfV2RUHv{?fllFA| z`gC*bg{~{TSe<}(x!@G(gN#MVZi3^GM?&c z=JXs-&uo@*3w;v=&BQMa)s}HWk|mp__Tjxu{O#Vg&*yRd#QrsVI2z}h?_E%2z*(C5 z-B1qZBTck@#ktrLEU5N0)qIlf6eN6W3TI;(6#n{I3ROxr+l*P$bNN?}-FcDFp0g-C zQ{rb29ej8w0V!<pjf98$!i654lgx=Q6qce+)Qe3^00VY&G{0 z<*6eW#Fa<(B(4Zg4emDj`*H`SY2YTPG`o21LS0{HhKsn+o_zS4dDT{}Fwf|P=Xb>s zVL^_wxUe4iS~p8`2)vFqktB$3u(Cf4rr7MD4Q^W*&_k{z*A?F^sOcw{_n9p#4?5{0 zS1ZftO)c9=k1l^CUMiyE-)a=5X}AWS@xFBUf{pX+!Mof#Cer!H-L!I&Ag`M5e;9D{M&U}tiF8mC3 z-(>#qzg8GZ{j3mz?keqdZwtFnmSUuQ20^V#{CMvx6CZ>v$N%+zVeQRbvbT|n!$y!} z^uf4eimv3o0O($nkI~&xLpo$Siw|u&>gibfg?>+ffB*mZGxP65Ow6eWz`cesO9uRm zyZ#2E8kL&8Gs9O`W_Y#~;~rGg(xW$h^+;*xSTqwbLp?YYnuj5(k5jlJ*iL@^_8(|3 z14*{5o2+nuu}E~^-eVhE-`t-<$Df)zEwz} zLjD|^oj?9NGCP5i$+K439owINtlT=f6z6r`yM3zPPbK_#^(DHm6LmBqS1!1n^4tF> JkhQ=6|GzfXIXnOW literal 0 HcmV?d00001 diff --git a/group16/313001956/src/com/coding/basic/Queue.java b/group16/313001956/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000000000000000000000000000000000..c78249a5cf0c35c3c6052b867bd922096ac880c8 GIT binary patch literal 413 zcmYciQBG}R5NBjyU|@9B4Ngo;^-C=Qa(w)qy@3=Q7)Ed#24*Ww4v>tWWu*RlqtwzI zcJtt?9P7`#;Qyj6BnnqZ7DKYY^4?C9yu6J*YTddIb-EhAP8ZsJTDA52>+s6en~r*( z=KP{~=9gKI(7h*HI!&)xs{CAdruc2@#TL5@i?fSV)lOG#p1U$>&6%4L{dW^TJ$wFj zM(hgNJ4_|Z7t}hRW>>l5Y9`n+_tic1UPhIwx3*utzFqh7-Wk`#TPLG+Gz@}1D{E@q z+Q+q-wdm(ZzrH!{No#watqSus|3A^LaKgj2t9v&Y@IH4n3BO`IMR#d>>9T-FKM(9( zvn$U`ac6|tBdzOU+q}!?IIc;ww5Zc&dw-Gb_w@D@J0pLuovhP-S!G@iiF}ZE_QSU} zC+0WW2~`qTzD?&k@@Qw<&E+q*mY&Os$f>&Fn8MmNrTKzq+d88)zYLd!e*NL?Cwori FApj8^qxS#+ literal 0 HcmV?d00001 diff --git a/group16/313001956/src/com/coding/basic/Stack.java b/group16/313001956/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000000000000000000000000000000000..b849bc4b5e203e26aafb2fbd6769e53d3697f197 GIT binary patch literal 666 zcmYciQBJ+YpvTC-z`*FL8=RPy>X%vqB&Io6+f!T&{DNEEJcz!{SDmiKm=b-K{*)2glCUx!z&-gMOS zH0Kw^Gr!Dwgzi1r(rt1{U+d?>GsSNuRz9&fysyGoz;I^B;{B^j_AI!{K5s(ehwtY; zHhG+J?BF$gyTHU_4_jSqirBNiJW>C-ZVS1}T@T#qcI0p0)rb79XHJUbrJ0$}H&LnF zd|US)cg4Gtu^rvgNr9_hT%NBu>93z_?vuT1MgH(dv;C6KRNHU5M{Bp1T2#fdz!P(h z-DM~@+fh~i-*n9>Pv4y{Lbj*ub_>(x=~J!PrTD-zv6k!B9iCqyF2b|F=>N7#4Ed?4 z7%LE)#1YuMxo^>zYm0*qE_MDPd7{WO(dqVS?jWXzf3FMo>@~grm+J9o`lqQ-mr2ESRc kZRLC|g@nx)D{rzU{h1syWfGSv*M@0pAKOj(Yh@}A01RsQ Date: Tue, 28 Feb 2017 13:23:50 +0800 Subject: [PATCH 3/9] =?UTF-8?q?0226=E4=BD=9C=E4=B8=9A=E9=87=8D=E6=96=B0?= =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/coding/basic/ArrayList.java | Bin 1503 -> 1503 bytes .../src/com/coding/basic/LinkedList.java | Bin 2332 -> 2332 bytes .../313001956/src/com/coding/basic/Queue.java | Bin 413 -> 413 bytes .../313001956/src/com/coding/basic/Stack.java | Bin 666 -> 666 bytes 4 files changed, 0 insertions(+), 0 deletions(-) diff --git a/group16/313001956/src/com/coding/basic/ArrayList.java b/group16/313001956/src/com/coding/basic/ArrayList.java index a6421a30fc0461adbe89407b4c18ef5a9edd6984..3bec144013a94278e6a7ef25d65622c3430e8e27 100644 GIT binary patch literal 1503 zcmcIky>8nu5S|$z?{K3G%Z@xJDbT;cKmiXO4G%PRcGya!Kv7QIB=6HcL8hYZmIvtC z*XZ1%NZFKKAVtv*0z~q7{O-H&zKV#tV$Ow|EwT zu~$Z4gCaVo?rhfB+3JOER9?Tn5m;HNRRH-nrMcqXZflF>m^+mTU2S82QjaK&g%kE* z2$R(7ZRDelxoXSl$YOh{oEG*(g%+*RCOfL4cKsuQT&V_aV^mzs+(QidJi=tK)EoSt zuo&wUN}7t483Fr~4*70G_jjFZ9U*iGMlkVRa=W8ZEABvw({sNsou70Q9s=Ff5F5?& z)S}^58<62A8Qeyc2dfDW1W$SR{P59H9{j7F+1~JZX9+&C07-M%H?Y0SLJ(_}IOCDPsb&3(#EWO~{uf`I*!=(i literal 1503 zcmcJP`8yK~0LP8wirnSSHsqRN=4v+Ayw*lz=p|uto5$;%N}|Y>mLqe$g*h^jvs_W} zOpZ4yRPMQrA!%gk?fs?y;Qjvc{eGV3``h<w|Uo8&-z{A4>u+??;@%MA^i~0pv zN9*&y?6>qUis<7*fH@cByb2984jbevLIsSS#{}z#R!@G_mX!Hj{2zndDbtxYSXfvC zRy{*^UMD?qFJH1_P%W9ma*Lxk_Sp{#eN`R$4$qXFS!^yac&TfW*VHK53uyfTWK@yk z)u4lMRbSddhHo|zGwzXAnMZr$s3gHuam3t`p+d3p>nY4NE$#{YFOs>a2HyGK2Hbs-&Y-X*A{XpEoK=n9%hvLD!%c_tz%~`D}Cgz>8D> zm4(1uZm`R7eL}wDRHFrvUH>5^ij*}23hdTPlosN($~6&zmpz8MJm*P?3Zc_2 z-V5U;SL>PWuiU-uOimq%N*khDn@sgoyLeR-O|~NY#yXRQT6(my54W+H#Gy*-Nvk}o z%J-LY6=wGr_95fuK9uGP#bS-R13+(Tgg;V)8eT@JitSvR<$wPHnUi*_;GyFXVfdsz zXDQxk-x1Ve9qCxeRqy6wIFm>;;7`#s<9=4g6N6a5<{zZD8$EcqDpj{jf2Dx6|1!)Pw93hpe}=i17pUmX_Cu7 zGGAdPNU*Bagc`96IVTbAud6dgDrtqsa@;&eV(uH2YiQv-@#dP?EbeW{?)c5A>M262 zSFw<~ZTC1M3X!n*ebOs^$^2s;e&BrOJG{Ct2Y1hIgP0jz2h>y9TQLkW0-Hc|8S{TE z-u6D0JY2PogJlMbY9bqus>g!9nmr{@c<{bij@^FE!fy!){QIV6qGj?a;69NkcPTUoGI26;&eDE>_wbpx{2J7vB6O)OFexSLc@nti zh7?GeMfRX>Fr)W0LHpiON_4EATA|`G-Ely;SGG{^i?R5`2s9vZ<@{jyT~2IBvF%yS zqZl`z2lGz+JkubJEVWwmzVyysm+d~)qQm9W(4N6M;sifecj8XiW*qHrMyVk~Q-{;h zB$6!goM|!1BqzWiSsr!I$|E1wQh4(Ue3r{gpECG|r{$vLbr`_TExELGCk=7w! zs(|se&FFTX^kLds7B4t87v&)0ej!Z{8F-it|8M`dn6wflU%tghHFFr|cbt(5S9K(A zSqN{{1PF|#MWyK;`1`rIyuRJf=%B}Fn{r9;^>b8Ky$#<2WK_=53!=~SAT)4Sa_0o( z9KpWM=6)SKa2JPOMh@xi+2GD#u#(sLdYVREe_cY^U_F*{qee{4ck@tmM%Mahrb5mwZarB>HU3Z@%2 zw@;eB!J#&RmF!usY8E}wQj%BCHz)AH0L+o|u+sqttlZP8HQj%`&-E=GdZ-u*z5p4fAP5p4DCrZtPAH%u?`Qe Hcl7uditLtC diff --git a/group16/313001956/src/com/coding/basic/LinkedList.java b/group16/313001956/src/com/coding/basic/LinkedList.java index f8b5e5b535dbd23a5156e04f8056229d5c140c78..de886c9084ccf244f58d8a1cf62b65ce290385c0 100644 GIT binary patch literal 2332 zcmcguOK#gR5M47s?l8zoL24six6YzKTLdubMNdFW6Pt}p3KIQklN_e#IdY5Mq1Py9 z_=zkz1_T7&QRIByy!ok;`Mq2qB(4qAZ+^bg8t3M*TvzD}vz{li6i3^5ji<#~Y&x zkVTQr=07mEU|32BcQuub&CDgaIIKbrl`e4i4m#TE4l;g$k8ll-k6sLJ7+>%p?6Njn z(71(LGBR7++1)3z)(b>h{=jVTaeqS&Of*}OirqE$B0lhBEXwKiUJ)$S` zDuS-Zg|reTU?>*x1Xwm0$Pj|L)b4eGmd^8^49DXqBcv0gaI1-V=h&fcdvV|NvhPU5 zTa7zQN@F@O2I0@%5@~v5g7?0TVu7bLrZ`%u`q zyk7#bSHGy*b!J4=DuOp^i#IBFLc_sx4-P2x7$*;)6NKrYb$XxZ5A(y<$I!;-A{Y>I zh|<@;&+lt_JK9r;%twv_fs~&rZ&JQ#PjKw{~Yozu56)lOx;N!GSYJTPj?yT=Yw$oXHgdO&moF eAkH{dRM&W_vW{_<^?W5x!UAL}oj8%fTK@qj;+JOt literal 2332 zcmb`H_dgVlAIFU{L!CP#CC=)&bDXm}TO4Qaoq56`=iC5GZ{ss zLsT-eGC${AeIJj{U+{hZ@_M}=kN0oS$Mfw16vF1wxYN?m(9mkZ%$(h^hFJXXMc+tM z?|1w=($=ifCOB}0F|T7DLhPjea=AC7yx9jP(#Q(Oo&*5&@8SQg@PZU6?MPqWdVNtM zY!R9ext|T_92SW=ShEVMX?U$W%)BQ!ax9k!pe#3KLOzQLcV$(wr0O&*sEm^Q2&(eM z@}DAF#2SA#;|}-m>#L{RQTii@en#nz6$1Jr$d-aJ65rlYDy4tKC?9NWno+eCNlcAp z_)t%t#FW@uB|@N{zbKwZ`Zj0Nn4$tBm)4Z@x+U;Q2CI#GWY7@ei#J2{EC;}(+dmst z<%*@R9Zam8q#nfCnx2Bg9|z6n#hiwZwGEG<9j&BT9(jfA$}aGS0F5Q*?zED`EcSx6 z7lR_jy5z+4bsRX{uf^8mif$^1vo9e8i$Z6aMYV=o-(0%x-_#JznY0^l!hxU{cRMk1 zQYC;_^hVi6?pI6RzScNoc|k1_wM(lpvIzP0{mN5#_n%&23&4u@fPg9SLn2xf0)tZ z1zjrmI>BswtAx`c*LWx*2Fpd%dVwV1!o<0=Ji!!`L?;WlmT47N*0J&(ss$kT>bgUV zpnyIPS{rFH_-oiFt8@Kr+m_?erFp0pxT3q+_(E_|>~rO$celL9?^QHzSaLxYw$Lmul4EA> z;>ie1{Oa?K0IAM&mB2c zVo=1hjTqVqAu~NLEF4EnDIjZ_7Fm)2-7T7xeNOy#hKowov5#>jOQ}~gGo8A>Z0{b<2zg>XbrF{{A1uIMoqWt*7~F^5?rg)VLlU2TJF_^<-Q^4GT9oZ3kiGvih}LZ zI(O}=(Fzl@ei18YG~XxOfSPLud+RG+4yfV2RUHv{?fllFA| z`gC*bg{~{TSe<}(x!@G(gN#MVZi3^GM?&c z=JXs-&uo@*3w;v=&BQMa)s}HWk|mp__Tjxu{O#Vg&*yRd#QrsVI2z}h?_E%2z*(C5 z-B1qZBTck@#ktrLEU5N0)qIlf6eN6W3TI;(6#n{I3ROxr+l*P$bNN?}-FcDFp0g-C zQ{rb29ej8w0V!<pjf98$!i654lgx=Q6qce+)Qe3^00VY&G{0 z<*6eW#Fa<(B(4Zg4emDj`*H`SY2YTPG`o21LS0{HhKsn+o_zS4dDT{}Fwf|P=Xb>s zVL^_wxUe4iS~p8`2)vFqktB$3u(Cf4rr7MD4Q^W*&_k{z*A?F^sOcw{_n9p#4?5{0 zS1ZftO)c9=k1l^CUMiyE-)a=5X}AWS@xFBUf{pX+!Mof#Cer!H-L!I&Ag`M5e;9D{M&U}tiF8mC3 z-(>#qzg8GZ{j3mz?keqdZwtFnmSUuQ20^V#{CMvx6CZ>v$N%+zVeQRbvbT|n!$y!} z^uf4eimv3o0O($nkI~&xLpo$Siw|u&>gibfg?>+ffB*mZGxP65Ow6eWz`cesO9uRm zyZ#2E8kL&8Gs9O`W_Y#~;~rGg(xW$h^+;*xSTqwbLp?YYnuj5(k5jlJ*iL@^_8(|3 z14*{5o2+nuu}E~^-eVhE-`t-<$Df)zEwz} zLjD|^oj?9NGCP5i$+K439owINtlT=f6z6r`yM3zPPbK_#^(DHm6LmBqS1!1n^4tF> JkhQ=6|GzfXIXnOW diff --git a/group16/313001956/src/com/coding/basic/Queue.java b/group16/313001956/src/com/coding/basic/Queue.java index c78249a5cf0c35c3c6052b867bd922096ac880c8..4b9f311f99f4b68d262b2a84f225a70d7c028c39 100644 GIT binary patch literal 413 zcmZ{fu};M>3`OTw>OXi)m+I}P8!%Nu?-Sg_s?2NSDshVd@$d9CL4*Ox+toeyctv}| ziNMOzS#j*AZp6s;p{bjC-NuqZ;FXH!?lt$}w`f`R>w+`v(RW?IPWBPv1< zp5BVkvtWWu*RlqtwzI zcJtt?9P7`#;Qyj6BnnqZ7DKYY^4?C9yu6J*YTddIb-EhAP8ZsJTDA52>+s6en~r*( z=KP{~=9gKI(7h*HI!&)xs{CAdruc2@#TL5@i?fSV)lOG#p1U$>&6%4L{dW^TJ$wFj zM(hgNJ4_|Z7t}hRW>>l5Y9`n+_tic1UPhIwx3*utzFqh7-Wk`#TPLG+Gz@}1D{E@q z+Q+q-wdm(ZzrH!{No#watqSus|3A^LaKgj2t9v&Y@IH4n3BO`IMR#d>>9T-FKM(9( zvn$U`ac6|tBdzOU+q}!?IIc;ww5Zc&dw-Gb_w@D@J0pLuovhP-S!G@iiF}ZE_QSU} zC+0WW2~`qTzD?&k@@Qw<&E+q*mY&Os$f>&Fn8MmNrTKzq+d88)zYLd!e*NL?Cwori FApj8^qxS#+ diff --git a/group16/313001956/src/com/coding/basic/Stack.java b/group16/313001956/src/com/coding/basic/Stack.java index b849bc4b5e203e26aafb2fbd6769e53d3697f197..6ec157201e0ee3d6b149ac2a8df4b9d5c46c9775 100644 GIT binary patch literal 666 zcmcJMu};G<5Qb+*$~)Yc3{7E187j3KQU-Vd$GHa6#J22nP(i#qj)@^H>c|-9{_p>M zHe8%|k0A6|3EfH6S1or^JmguP8Q%_4fEc)Q@I+x0KJttiD<@6}J8Sv*Q91$~a70CW zP8@1b`1bp+C?kq3tVk)!$YI+pJU9CWcAoU6M};->Ba-5Eq zw>atvuLdlVw+i%4EyUA5c!edfo7Jt>19An@9Y#avpZX&d>-&7~1L_*?%9)pd;h}K8 ZPWnFb!Nr4%Cre9C^u^|H!LpeU{sMa^$E*MV literal 666 zcmYciQBJ+YpvTC-z`*FL8=RPy>X%vqB&Io6+f!T&{DNEEJcz!{SDmiKm=b-K{*)2glCUx!z&-gMOS zH0Kw^Gr!Dwgzi1r(rt1{U+d?>GsSNuRz9&fysyGoz;I^B;{B^j_AI!{K5s(ehwtY; zHhG+J?BF$gyTHU_4_jSqirBNiJW>C-ZVS1}T@T#qcI0p0)rb79XHJUbrJ0$}H&LnF zd|US)cg4Gtu^rvgNr9_hT%NBu>93z_?vuT1MgH(dv;C6KRNHU5M{Bp1T2#fdz!P(h z-DM~@+fh~i-*n9>Pv4y{Lbj*ub_>(x=~J!PrTD-zv6k!B9iCqyF2b|F=>N7#4Ed?4 z7%LE)#1YuMxo^>zYm0*qE_MDPd7{WO(dqVS?jWXzf3FMo>@~grm+J9o`lqQ-mr2ESRc kZRLC|g@nx)D{rzU{h1syWfGSv*M@0pAKOj(Yh@}A01RsQ Date: Wed, 1 Mar 2017 17:59:23 +0800 Subject: [PATCH 4/9] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E7=9A=84=E7=9B=AE=E5=BD=95=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0226/src/com/coding/basic/ArrayList.java | 126 +++++------ .../0226/src/com/coding/basic/Iterator.java | 12 +- .../0226/src/com/coding/basic/LinkedList.java | 202 +++++++++--------- .../0226/src/com/coding/basic/List.java | 18 +- .../0226/src/com/coding/basic/Queue.java | 56 ++--- .../0226/src/com/coding/basic/Stack.java | 76 +++---- 6 files changed, 245 insertions(+), 245 deletions(-) diff --git a/group16/1287642108/0226/src/com/coding/basic/ArrayList.java b/group16/1287642108/0226/src/com/coding/basic/ArrayList.java index e287419dc0..f82d064e2a 100644 --- a/group16/1287642108/0226/src/com/coding/basic/ArrayList.java +++ b/group16/1287642108/0226/src/com/coding/basic/ArrayList.java @@ -1,63 +1,63 @@ -package com.coding.basic; - -public class ArrayList implements List { - - private int size = 0; - - private Object[] elementData = new Object[100]; - - public void add(Object o) { - IncrementsCapacity(size + 1); - elementData[size++] = o; - - } - - public void add(int index, Object o) { - checkIndex(index); - IncrementsCapacity(size + 1); - System.arraycopy(elementData, index, elementData, index + 1, size - index); - elementData[index] = o; - size++; - } - - public Object get(int index) { - checkIndex(index); - return elementData[index]; - } - - public Object remove(int index) { - checkIndex(index); - Object o = elementData[index]; - System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); - elementData[--size] = null; - return o; - } - - public int size() { - return size; - } - - public int length() { - return elementData.length; - } - - // ���� - private void IncrementsCapacity(int num) { - if (num > elementData.length) { - int oldCapacity = elementData.length; // ��ǰ���鳤�� - int newCapacity = ((num + oldCapacity) * 3) >> 2; // ��ǰ���鳤�ȵ�1.5�� - if (newCapacity - num < 0) { - newCapacity = num; // �����������Dz���,ֱ����Ϊ����ֵ - } - Object[] oldelements = elementData; - elementData = new Object[newCapacity]; - System.arraycopy(oldelements, 0, elementData, 0, size); - } - } - - // �±�Խ���ж� - private void checkIndex(int index) { - if (index >= size || index < 0) - throw new IndexOutOfBoundsException("�����±�Խ��"); - } -} +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + IncrementsCapacity(size + 1); + elementData[size++] = o; + + } + + public void add(int index, Object o) { + checkIndex(index); + IncrementsCapacity(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + public Object get(int index) { + checkIndex(index); + return elementData[index]; + } + + public Object remove(int index) { + checkIndex(index); + Object o = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + elementData[--size] = null; + return o; + } + + public int size() { + return size; + } + + public int length() { + return elementData.length; + } + + // ���� + private void IncrementsCapacity(int num) { + if (num > elementData.length) { + int oldCapacity = elementData.length; // ��ǰ���鳤�� + int newCapacity = ((num + oldCapacity) * 3) >> 2; // ��ǰ���鳤�ȵ�1.5�� + if (newCapacity - num < 0) { + newCapacity = num; // �����������Dz���,ֱ����Ϊ����ֵ + } + Object[] oldelements = elementData; + elementData = new Object[newCapacity]; + System.arraycopy(oldelements, 0, elementData, 0, size); + } + } + + // �±�Խ���ж� + private void checkIndex(int index) { + if (index >= size || index < 0) + throw new IndexOutOfBoundsException("�����±�Խ��"); + } +} diff --git a/group16/1287642108/0226/src/com/coding/basic/Iterator.java b/group16/1287642108/0226/src/com/coding/basic/Iterator.java index e7cbd474ec..ff93e30377 100644 --- a/group16/1287642108/0226/src/com/coding/basic/Iterator.java +++ b/group16/1287642108/0226/src/com/coding/basic/Iterator.java @@ -1,6 +1,6 @@ -package com.coding.basic; - -public interface Iterator { - public boolean hasNext(); - public Object next(); -} +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group16/1287642108/0226/src/com/coding/basic/LinkedList.java b/group16/1287642108/0226/src/com/coding/basic/LinkedList.java index 99c92fb9f1..fc206b4cec 100644 --- a/group16/1287642108/0226/src/com/coding/basic/LinkedList.java +++ b/group16/1287642108/0226/src/com/coding/basic/LinkedList.java @@ -1,101 +1,101 @@ -package com.coding.basic; - -public class LinkedList implements List { - - private Node head; - private Node tail; - private int size = 0 ; - - public void add(Object o){ - addLast(o); - } - - public void add(int index, Object o) { - if (index == 0) { - addFirst(o); - } else if (index >= size) { - addLast(o); - } else { - Node node = new Node(); - node.data = o; - node.next = getNode(index); - getNode(index - 1).next = node; - size++; - } - } - - public Object get(int index) { - Node node = getNode(index); - return node.data; - } - - public Object remove(int index){ - Node currentNode = getNode(index); - Node prevNode = getNode(index - 1); - Node lastNode = getNode(index + 1); - prevNode.next = lastNode; - size--; - return currentNode.data; - } - - public int size(){ - return size; - } - - public void addFirst(Object o){ - Node node=new Node(); - node.data = o; - node.next = head; - head = node; - size++; - } - public void addLast(Object o){ - Node node=new Node(); - node.data = o; - node.next = null; - Node lastNode = getNode(size-1); - lastNode.next = node; - size++; - } - public Object removeFirst(){ - Object obj = getNode(0).data; - Node node = getNode(1); - node.next = head; - size--; - return obj; - } - public Object removeLast(){ - Object obj = getNode(size - 1).data; - Node node = getNode(size - 2); - node.next = null; - size--; - return obj; - } - - //��ȡ�ڵ� - public Node getNode(int index){ - checkIndex(index); - if(index == 0 ){ - return head; - }else if(index == size -1 ){ - return tail; - }else{ - Node node = head; - for(int i=0;i= size || index < 0) - throw new IndexOutOfBoundsException("�����±�Խ��"); - } - - private static class Node { - Object data; - Node next; - } -} +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private Node tail; + private int size = 0 ; + + public void add(Object o){ + addLast(o); + } + + public void add(int index, Object o) { + if (index == 0) { + addFirst(o); + } else if (index >= size) { + addLast(o); + } else { + Node node = new Node(); + node.data = o; + node.next = getNode(index); + getNode(index - 1).next = node; + size++; + } + } + + public Object get(int index) { + Node node = getNode(index); + return node.data; + } + + public Object remove(int index){ + Node currentNode = getNode(index); + Node prevNode = getNode(index - 1); + Node lastNode = getNode(index + 1); + prevNode.next = lastNode; + size--; + return currentNode.data; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node node=new Node(); + node.data = o; + node.next = head; + head = node; + size++; + } + public void addLast(Object o){ + Node node=new Node(); + node.data = o; + node.next = null; + Node lastNode = getNode(size-1); + lastNode.next = node; + size++; + } + public Object removeFirst(){ + Object obj = getNode(0).data; + Node node = getNode(1); + node.next = head; + size--; + return obj; + } + public Object removeLast(){ + Object obj = getNode(size - 1).data; + Node node = getNode(size - 2); + node.next = null; + size--; + return obj; + } + + //��ȡ�ڵ� + public Node getNode(int index){ + checkIndex(index); + if(index == 0 ){ + return head; + }else if(index == size -1 ){ + return tail; + }else{ + Node node = head; + for(int i=0;i= size || index < 0) + throw new IndexOutOfBoundsException("�����±�Խ��"); + } + + private static class Node { + Object data; + Node next; + } +} diff --git a/group16/1287642108/0226/src/com/coding/basic/List.java b/group16/1287642108/0226/src/com/coding/basic/List.java index 10d13b5832..396b1f6416 100644 --- a/group16/1287642108/0226/src/com/coding/basic/List.java +++ b/group16/1287642108/0226/src/com/coding/basic/List.java @@ -1,9 +1,9 @@ -package com.coding.basic; - -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(); -} +package com.coding.basic; + +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/group16/1287642108/0226/src/com/coding/basic/Queue.java b/group16/1287642108/0226/src/com/coding/basic/Queue.java index 95dee3d81b..418e42826e 100644 --- a/group16/1287642108/0226/src/com/coding/basic/Queue.java +++ b/group16/1287642108/0226/src/com/coding/basic/Queue.java @@ -1,28 +1,28 @@ -package com.coding.basic; - -public class Queue { - private LinkedList elementData = new LinkedList(); - - public void enQueue(Object o){ - elementData.addLast(o); - } - - public Object deQueue(){ - if (isEmpty()) { - return null; - }else{ - return elementData.removeFirst(); - } - } - - public boolean isEmpty(){ - if (elementData.size() == 0) { - return true; - } - return false; - } - - public int size(){ - return elementData.size(); - } -} +package com.coding.basic; + +public class Queue { + private LinkedList elementData = new LinkedList(); + + public void enQueue(Object o){ + elementData.addLast(o); + } + + public Object deQueue(){ + if (isEmpty()) { + return null; + }else{ + return elementData.removeFirst(); + } + } + + public boolean isEmpty(){ + if (elementData.size() == 0) { + return true; + } + return false; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group16/1287642108/0226/src/com/coding/basic/Stack.java b/group16/1287642108/0226/src/com/coding/basic/Stack.java index c0b7da89f8..6138bc6973 100644 --- a/group16/1287642108/0226/src/com/coding/basic/Stack.java +++ b/group16/1287642108/0226/src/com/coding/basic/Stack.java @@ -1,38 +1,38 @@ -package com.coding.basic; - -public class Stack { - private ArrayList elementData = new ArrayList(); - - public void push(Object o) { - if (isEmpty()) { - elementData.add(elementData.length() - 1, o); - } - elementData.add(elementData.length() - elementData.size() - 1, o); - } - - public Object pop() { - if (isEmpty()) { - return null; - } - return elementData.remove(elementData.length() - elementData.size() - 1); - } - - public Object peek() { - if (isEmpty()) { - return null; - } - return elementData.get(elementData.length() - elementData.size() - 1); - } - - public boolean isEmpty() { - if (elementData.size() == 0) { - return true; - } - return false; - } - - public int size() { - return elementData.size(); - } - -} +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + if (isEmpty()) { + elementData.add(elementData.length() - 1, o); + } + elementData.add(elementData.length() - elementData.size() - 1, o); + } + + public Object pop() { + if (isEmpty()) { + return null; + } + return elementData.remove(elementData.length() - elementData.size() - 1); + } + + public Object peek() { + if (isEmpty()) { + return null; + } + return elementData.get(elementData.length() - elementData.size() - 1); + } + + public boolean isEmpty() { + if (elementData.size() == 0) { + return true; + } + return false; + } + + public int size() { + return elementData.size(); + } + +} From 314dd5c449f2ef60832fb04a33906f01a66fa165 Mon Sep 17 00:00:00 2001 From: duxl Date: Wed, 1 Mar 2017 18:00:24 +0800 Subject: [PATCH 5/9] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/coderising/array/ArrayUtil.java | 96 +++++++++++++++++++ .../coderising/litestruts/LoginAction.java | 39 ++++++++ .../src/com/coderising/litestruts/Struts.java | 34 +++++++ .../com/coderising/litestruts/StrutsTest.java | 43 +++++++++ .../src/com/coderising/litestruts/View.java | 23 +++++ .../src/com/coderising/litestruts/struts.xml | 11 +++ 6 files changed, 246 insertions(+) create mode 100644 group16/1287642108/0305/src/com/coderising/array/ArrayUtil.java create mode 100644 group16/1287642108/0305/src/com/coderising/litestruts/LoginAction.java create mode 100644 group16/1287642108/0305/src/com/coderising/litestruts/Struts.java create mode 100644 group16/1287642108/0305/src/com/coderising/litestruts/StrutsTest.java create mode 100644 group16/1287642108/0305/src/com/coderising/litestruts/View.java create mode 100644 group16/1287642108/0305/src/com/coderising/litestruts/struts.xml diff --git a/group16/1287642108/0305/src/com/coderising/array/ArrayUtil.java b/group16/1287642108/0305/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..78845d06d0 --- /dev/null +++ b/group16/1287642108/0305/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,96 @@ +package com.coderising.array; + +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){ + + } + + /** + * 现在有如下的一个数组: 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){ + return null; + } + + /** + * 给定两个已经排序好的整形数组, 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){ + return null; + } + /** + * 把一个已经存满数据的数组 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){ + return null; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + return null; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + return null; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + return null; + } + + +} diff --git a/group16/1287642108/0305/src/com/coderising/litestruts/LoginAction.java b/group16/1287642108/0305/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..1005f35a29 --- /dev/null +++ b/group16/1287642108/0305/src/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @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/group16/1287642108/0305/src/com/coderising/litestruts/Struts.java b/group16/1287642108/0305/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..44cc35bf01 --- /dev/null +++ b/group16/1287642108/0305/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,34 @@ +package com.coderising.litestruts; + +import java.util.Map; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 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/group16/1287642108/0305/src/com/coderising/litestruts/StrutsTest.java b/group16/1287642108/0305/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a44c1878ac --- /dev/null +++ b/group16/1287642108/0305/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +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 = "login"; + + 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/group16/1287642108/0305/src/com/coderising/litestruts/View.java b/group16/1287642108/0305/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group16/1287642108/0305/src/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +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/group16/1287642108/0305/src/com/coderising/litestruts/struts.xml b/group16/1287642108/0305/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..99063bcb0c --- /dev/null +++ b/group16/1287642108/0305/src/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file From 43059714ada0de61721218a6ee9b4dccf0284465 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=AD=E5=B7=9E-=E5=A4=A7=E6=98=8E?= <420355244@qq.com> Date: Wed, 1 Mar 2017 22:02:26 +0800 Subject: [PATCH 6/9] delete old --- group16/420355244/.classpath | 7 - group16/420355244/.gitignore | 1 - group16/420355244/.project | 17 -- .../src/com/coding/basic/ArrayList.java | 91 ------- .../src/com/coding/basic/ArrayListTest.java | 64 ----- .../src/com/coding/basic/BinaryTreeNode.java | 32 --- .../src/com/coding/basic/Iterator.java | 7 - .../src/com/coding/basic/LinkedList.java | 224 ------------------ .../src/com/coding/basic/LinkedListTest.java | 112 --------- .../420355244/src/com/coding/basic/List.java | 9 - .../420355244/src/com/coding/basic/Queue.java | 66 ------ .../src/com/coding/basic/QueueTest.java | 56 ----- .../420355244/src/com/coding/basic/Stack.java | 39 --- .../src/com/coding/basic/StackTest.java | 64 ----- 14 files changed, 789 deletions(-) delete mode 100644 group16/420355244/.classpath delete mode 100644 group16/420355244/.gitignore delete mode 100644 group16/420355244/.project delete mode 100644 group16/420355244/src/com/coding/basic/ArrayList.java delete mode 100644 group16/420355244/src/com/coding/basic/ArrayListTest.java delete mode 100644 group16/420355244/src/com/coding/basic/BinaryTreeNode.java delete mode 100644 group16/420355244/src/com/coding/basic/Iterator.java delete mode 100644 group16/420355244/src/com/coding/basic/LinkedList.java delete mode 100644 group16/420355244/src/com/coding/basic/LinkedListTest.java delete mode 100644 group16/420355244/src/com/coding/basic/List.java delete mode 100644 group16/420355244/src/com/coding/basic/Queue.java delete mode 100644 group16/420355244/src/com/coding/basic/QueueTest.java delete mode 100644 group16/420355244/src/com/coding/basic/Stack.java delete mode 100644 group16/420355244/src/com/coding/basic/StackTest.java diff --git a/group16/420355244/.classpath b/group16/420355244/.classpath deleted file mode 100644 index 3e0fb272a8..0000000000 --- a/group16/420355244/.classpath +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - diff --git a/group16/420355244/.gitignore b/group16/420355244/.gitignore deleted file mode 100644 index ae3c172604..0000000000 --- a/group16/420355244/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/bin/ diff --git a/group16/420355244/.project b/group16/420355244/.project deleted file mode 100644 index aec36208a6..0000000000 --- a/group16/420355244/.project +++ /dev/null @@ -1,17 +0,0 @@ - - - Homework - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - diff --git a/group16/420355244/src/com/coding/basic/ArrayList.java b/group16/420355244/src/com/coding/basic/ArrayList.java deleted file mode 100644 index 6534c3c029..0000000000 --- a/group16/420355244/src/com/coding/basic/ArrayList.java +++ /dev/null @@ -1,91 +0,0 @@ -package com.coding.basic; - -import java.util.Arrays; - -public class ArrayList implements List { - - private int size = 0; - - private Object[] elementData = new Object[100]; - - public void add(Object o){ - if(size < elementData.length){ - elementData[size] = o; - }else{ - Object[] newElementData = new Object[elementData.length + elementData.length/2]; - System.arraycopy(elementData, 0, newElementData, 0, size); - newElementData[size] = o; - this.elementData = newElementData; - } - size++; - - } - public void add(int index, Object o){ - if(index >= 0 && index <= size){ - //1.不扩容 - if(index == size - 1){ - //1.1 加在最后 - elementData[index] = o; - }else{ - //1.2 加在前面 - //index的位置的数值变为改对象,index以后位置的都往后挪一位 - Object[] newElementData = new Object[elementData.length]; - System.arraycopy(elementData, 0, newElementData, 0, index); - newElementData[index] = o ; - System.arraycopy(elementData, index, newElementData, index + 1, size - index); - this.elementData = newElementData; - } - size++; - }else{ - throw new IndexOutOfBoundsException(); - } - } - - public Object get(int index){ - if(index < size){ - return elementData[index]; - }else{ - throw new IndexOutOfBoundsException(); - } - } - - public Object remove(int index){ - if(index < size){ - Object obj = elementData[index]; - Object[] newElementData = new Object[elementData.length]; - if(size != 1){ - //1.若集合长度为1 - if(0 == index){ - //1.1.如果remove的是0索引的 - System.arraycopy(elementData, 1, newElementData, 0, size - 1); - }else if(index == size -1){ - //1.2.如果remove的是最后索引的 - System.arraycopy(elementData, 0, newElementData, 0, size - 1); - }else{ - //1.3.在中间 - System.arraycopy(elementData, 0, newElementData, 0, index); - System.arraycopy(elementData, index + 1, newElementData, index, size - index - 1); - } - } - this.elementData = newElementData; - size--; - return obj; - }else{ - throw new IndexOutOfBoundsException(); - } - } - - public int size(){ - return size; - } - - public Iterator iterator(){ - return null; - } - @Override - public String toString() { - return "ArrayList [size=" + size + ", elementData=" + Arrays.toString(elementData) + "]"; - } - - -} diff --git a/group16/420355244/src/com/coding/basic/ArrayListTest.java b/group16/420355244/src/com/coding/basic/ArrayListTest.java deleted file mode 100644 index 2f48bdf4c8..0000000000 --- a/group16/420355244/src/com/coding/basic/ArrayListTest.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.coding.basic; - -import static org.junit.Assert.fail; - -import org.junit.Before; -import org.junit.Test; - -public class ArrayListTest { - - private static ArrayList arrayList = new ArrayList(); - - @Before - public void setUp() throws Exception { - } - - @Test - public void testAddObject() { - for(int i = 0 ;i < 150; i++){ - arrayList.add("aaa"); - } - System.out.println(arrayList); - System.out.println(arrayList.size()); - } - - @Test - public void testAddIntObject() { - arrayList.add("aaa"); - arrayList.add("bbb"); - arrayList.add("ccc"); - arrayList.add("ddd"); - arrayList.add(1,"eee"); - System.out.println(arrayList); - } - - @Test - public void testGet() { - arrayList.add("aaa"); - arrayList.add("bbb"); - arrayList.add("ccc"); - arrayList.add("ddd"); - Object object = arrayList.get(0); - System.out.println(object); - } - - @Test - public void testRemove() { - arrayList.add("aaa"); - arrayList.add("bbb"); - arrayList.add("ccc"); - arrayList.add("ddd"); - arrayList.remove(0); - System.out.println(arrayList); - } - - @Test - public void testSize() { - arrayList.add("aaa"); - arrayList.add("bbb"); - arrayList.add("ccc"); - arrayList.add("ddd"); - System.out.println(arrayList.size()); - } - -} diff --git a/group16/420355244/src/com/coding/basic/BinaryTreeNode.java b/group16/420355244/src/com/coding/basic/BinaryTreeNode.java deleted file mode 100644 index d7ac820192..0000000000 --- a/group16/420355244/src/com/coding/basic/BinaryTreeNode.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.coding.basic; - -public class BinaryTreeNode { - - private Object data; - private BinaryTreeNode left; - private BinaryTreeNode right; - - public Object getData() { - return data; - } - public void setData(Object data) { - this.data = data; - } - public BinaryTreeNode getLeft() { - return left; - } - public void setLeft(BinaryTreeNode left) { - this.left = left; - } - public BinaryTreeNode getRight() { - return right; - } - public void setRight(BinaryTreeNode right) { - this.right = right; - } - - public BinaryTreeNode insert(Object o){ - return null; - } - -} diff --git a/group16/420355244/src/com/coding/basic/Iterator.java b/group16/420355244/src/com/coding/basic/Iterator.java deleted file mode 100644 index 06ef6311b2..0000000000 --- a/group16/420355244/src/com/coding/basic/Iterator.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.coding.basic; - -public interface Iterator { - public boolean hasNext(); - public Object next(); - -} diff --git a/group16/420355244/src/com/coding/basic/LinkedList.java b/group16/420355244/src/com/coding/basic/LinkedList.java deleted file mode 100644 index 9af35a399d..0000000000 --- a/group16/420355244/src/com/coding/basic/LinkedList.java +++ /dev/null @@ -1,224 +0,0 @@ -package com.coding.basic; - -import java.util.NoSuchElementException; - -public class LinkedList implements List { - - - private Node first; - - private Node last; - - private int size = 0; - - public void add(Object o){ - if(null == first){ - //当链表元素为空时,新建一个Node - Node node = new Node(); - node.data = o; - node.next = null; - first = node; - last = node; - size ++; - }else{ - addLast(o); - } - } - public void add(int index , Object o){ - if(index < 0 || index >= size){ - //数组越界异常 - throw new IndexOutOfBoundsException(); - }else{ - if(0 == index){ - //1.如果加在头上 - addFirst(o); - }else{ - //2.加在中间位置 - Node node = first.next; - int nodeIndex = 1; - if(nodeIndex == index){ - //如果是第二个位置的话 - Node nodeAdd = new Node(); - nodeAdd.data = o; - first.next = nodeAdd; - nodeAdd.next = node; - last = node; - size ++; - } - //第三个位置及以后、开始遍历所有的索引 - while(null != node.next){ - //保留遍历中node之前的结点 - Node nodeLast = node; - node = node.next; - nodeIndex++; - if(nodeIndex == index){ - Node nodeAdd = new Node(); - nodeAdd.data = o; - nodeLast.next = nodeAdd; - nodeAdd.next = node; - size ++; - break; - } - } - } - } - - } - public Object get(int index){ - if(index < 0 || index >= size){ - //数组越界异常 - throw new IndexOutOfBoundsException(); - }else{ - if(0 == index){ - //1.如果加在头上 - return first.data; - } - Node node = first.next; - int nodeIndex = 1; - if(nodeIndex == index){ - //如果是第二个位置的话 - return node.data; - } - //第三个位置及以后、开始遍历所有的索引 - while(null != node.next){ - //保留遍历中node之前的结点 - node = node.next; - nodeIndex++; - if(nodeIndex == index){ - return node.data; - } - } - } - throw new IndexOutOfBoundsException(); - } - public Object remove(int index){ - if(index < 0 || index >= size){ - //数组越界异常 - throw new IndexOutOfBoundsException(); - }else{ - if(0 == index){ - //1.如果移除头 - removeFirst(); - }else if(index == (size - 1)){ - //2.移除尾 - removeLast(); - }else{ - //3.移除中间位置 - Node node = first.next; - //从first的零号索引开始 - int nodeIndex = 1; - - //开始遍历所有的索引,记住要移除的索引位数据的前后结点 - Node lastNode = first; - if(index == nodeIndex){ - //第一次不匹配则后续的循环执行 - Object o = node.data; - lastNode.next = node.next; - size--; - return o; - }else{ - while(null != node.next){ - lastNode = node; - node = node.next; - nodeIndex++; - if(index == nodeIndex){ - Object o = node.data; - lastNode.next = node.next; - size--; - return o; - } - } - } - } - } - throw new IndexOutOfBoundsException(); - } - - public int size(){ - return size; - } - - public void addFirst(Object o){ - Node node = new Node(); - node.data = o ; - node.next = first; - first = node; - size++; - } - public void addLast(Object o){ - Node node = new Node(); - node.data = o ; - node.next = null; - last.next = node; - last = node; - size++; - } - public Object removeFirst(){ - Object o = first.data; - Node node = first.next; - first = node; - size--; - return o; - } - public Object removeLast(){ - if(0 == size){ - throw new NoSuchElementException(); - - }else if(1 == size){ - //只有一个元素 - removeFirst(); - }else{ - //第二个元素 - Node node = first.next; - if(null == node.next){ - Object o = node.data; - last = first; - first.next = null; - return o; - }else{ - while(null != node.next){ - //若不止只有2个 ,记录最后一个结点的前一个。 - Node lastNode = node; - node = node.next; - if(null == node.next){ - Object o = node.data; - lastNode.next = null; - last = lastNode; - size--; - return o; - } - } - } - } - throw new NoSuchElementException(); - } - public Iterator iterator(){ - return null; - } - - - private static class Node{ - Object data; - Node next; - - } - - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - if(null != first){ - sb.append(first.data.toString() + ","); - Node node = first.next; - sb.append(node.data.toString() + ","); - while(null != node.next){ - node = node.next; - sb.append(node.data.toString() + ","); - } - } - return sb.toString(); - } - - - -} diff --git a/group16/420355244/src/com/coding/basic/LinkedListTest.java b/group16/420355244/src/com/coding/basic/LinkedListTest.java deleted file mode 100644 index 2caea9679b..0000000000 --- a/group16/420355244/src/com/coding/basic/LinkedListTest.java +++ /dev/null @@ -1,112 +0,0 @@ -package com.coding.basic; - -import static org.junit.Assert.*; - -import org.junit.Before; -import org.junit.Test; - -public class LinkedListTest { - private static LinkedList linkedList = new LinkedList(); - @Before - public void setUp() throws Exception { - } - - @Test - public void testAddObject() { - linkedList.add("aaa"); - linkedList.add("bbb"); - System.out.println(linkedList); - } - - @Test - public void testAddIntObject() { - linkedList.add("aaa"); - linkedList.add("bbb"); - linkedList.add("ccc"); - linkedList.add(2,"ddd"); - System.out.println(linkedList); - System.out.println(linkedList.size()); - } - - @Test - public void testGet() { - linkedList.add("aaa"); - linkedList.add("bbb"); - linkedList.add("ccc"); - linkedList.add("eee"); - linkedList.add("fff"); - linkedList.add("ddd"); -// System.out.println(linkedList.size()); - System.out.println(linkedList.get(3)); - } - - @Test - public void testRemove() { - linkedList.add("aaa"); - linkedList.add("bbb"); - linkedList.add("ccc"); - linkedList.add("eee"); - linkedList.add("fff"); - linkedList.add("ddd"); - linkedList.remove(5); - linkedList.remove(1); - linkedList.remove(2); - System.out.println(linkedList); - System.out.println(linkedList.size()); - } - - @Test - public void testSize() { - linkedList.add("aaa"); - linkedList.add("bbb"); - linkedList.add("ccc"); - linkedList.add("eee"); - linkedList.add("fff"); - linkedList.add("ddd"); - System.out.println(linkedList.size()); - } - - @Test - public void testAddFirst() { - linkedList.add("aaa"); - linkedList.add("bbb"); - linkedList.addFirst("sss"); - System.out.println(linkedList); - } - - @Test - public void testAddLast() { - linkedList.add("aaa"); - linkedList.add("bbb"); - linkedList.add("ccc"); - System.out.println(linkedList); - } - - @Test - public void testRemoveFirst() { - linkedList.add("aaa"); - linkedList.add("bbb"); - linkedList.add("ccc"); - linkedList.removeFirst(); - linkedList.addFirst("eee"); - linkedList.removeFirst(); - System.out.println(linkedList); - } - - @Test - public void testRemoveLast() { - linkedList.add("aaa"); - linkedList.add("bbb"); - linkedList.add("ccc"); - linkedList.removeLast(); - linkedList.add("eee"); - linkedList.addFirst("xxx"); - System.out.println(linkedList); - } - - @Test - public void testIterator() { - fail("Not yet implemented"); - } - -} diff --git a/group16/420355244/src/com/coding/basic/List.java b/group16/420355244/src/com/coding/basic/List.java deleted file mode 100644 index 10d13b5832..0000000000 --- a/group16/420355244/src/com/coding/basic/List.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.coding.basic; - -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/group16/420355244/src/com/coding/basic/Queue.java b/group16/420355244/src/com/coding/basic/Queue.java deleted file mode 100644 index a2f9577b7b..0000000000 --- a/group16/420355244/src/com/coding/basic/Queue.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.coding.basic; - -public class Queue { - private Node first; - private Node last; - private int size = 0; - public void enQueue(Object o){ - if(null == first){ - Node node = new Node(); - node.data = o; - node.next = null; - first = node; - last = node; - }else{ - Node node = new Node(); - node.data = o; - node.next = null; - last.next = node; - last = node; - } - size++; - } - - public Object deQueue(){ - Node second = first.next; - Object o = first.data; - if(null != second){ - first = second; - return o; - }else{ - first = null; - size = 0; - return o; - } - } - - public boolean isEmpty(){ - if(size > 0){ - return false; - }else{ - return true; - } - } - - public int size(){ - return size; - } - static class Node{ - Node next; - Object data; - } - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - if(null != first){ - sb.append(first.data.toString() + ","); - Node node = first.next; - sb.append(node.data.toString() + ","); - while(null != node.next){ - node = node.next; - sb.append(node.data.toString() + ","); - } - } - return sb.toString(); - } -} diff --git a/group16/420355244/src/com/coding/basic/QueueTest.java b/group16/420355244/src/com/coding/basic/QueueTest.java deleted file mode 100644 index 50d7fc0903..0000000000 --- a/group16/420355244/src/com/coding/basic/QueueTest.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.coding.basic; - -import static org.junit.Assert.*; - -import org.junit.Before; -import org.junit.Test; - -public class QueueTest { - - public static Queue queue = new Queue(); - @Before - public void setUp() throws Exception { - } - - @Test - public void testEnQueue() { - queue.enQueue("aaa"); - queue.enQueue("bbb"); - queue.enQueue("ccc"); - queue.enQueue("aaa"); - System.out.println(queue); - } - - @Test - public void testDeQueue() { - queue.enQueue("aaa"); - queue.enQueue("bbb"); - queue.enQueue("ccc"); - queue.enQueue("ddd"); - queue.enQueue("eee"); - queue.deQueue(); - System.out.println(queue); - } - - @Test - public void testIsEmpty() { - System.out.println(queue.isEmpty()); - - } - - @Test - public void testSize() { - queue.enQueue("aaa"); - queue.enQueue("bbb"); - queue.enQueue("ccc"); - queue.enQueue("ddd"); - queue.enQueue("eee"); - System.out.println(queue.size()); - } - - @Test - public void testToString() { - fail("Not yet implemented"); - } - -} diff --git a/group16/420355244/src/com/coding/basic/Stack.java b/group16/420355244/src/com/coding/basic/Stack.java deleted file mode 100644 index 5c59bf34fc..0000000000 --- a/group16/420355244/src/com/coding/basic/Stack.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.coding.basic; - -public class Stack { - private ArrayList elementData = new ArrayList(); - private int size = 0; - public void push(Object o){ - //入栈在栈顶进入最后压入 - elementData.add(o); - size ++; - } - - public Object pop(){ - Object object = elementData.get(size -1); - elementData.remove(size -1); - size --; - return object; - } - - public Object peek(){ - Object object = elementData.get(size -1); - return object; - } - public boolean isEmpty(){ - if(size <= 0){ - return true; - }else{ - return false; - } - } - public int size(){ - return size; - } - - @Override - public String toString() { - return elementData.toString(); - } - -} diff --git a/group16/420355244/src/com/coding/basic/StackTest.java b/group16/420355244/src/com/coding/basic/StackTest.java deleted file mode 100644 index 0628e7c76e..0000000000 --- a/group16/420355244/src/com/coding/basic/StackTest.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.coding.basic; - -import static org.junit.Assert.*; - -import org.junit.Before; -import org.junit.Test; - -public class StackTest { - private static Stack stack = new Stack(); - @Before - public void setUp() throws Exception { - } - - @Test - public void testPush() { - stack.push("aaa"); - stack.push("bbb"); - stack.push("ccc"); - System.out.println(stack); - } - - @Test - public void testPop() { - stack.push("aaa"); - stack.push("bbb"); - stack.push("ccc"); - Object pop = stack.pop(); - System.out.println(pop); - System.out.println(stack); - } - - @Test - public void testPeek() { - stack.push("aaa"); - stack.push("bbb"); - stack.push("ccc"); - Object peek = stack.peek(); - System.out.println(peek); - } - - @Test - public void testIsEmpty() { - System.out.println(stack.isEmpty()); - stack.push("aaa"); - stack.push("bbb"); - stack.push("ccc"); - System.out.println(stack.isEmpty()); - stack.pop(); - stack.pop(); - stack.pop(); - System.out.println(stack.isEmpty()); - } - - @Test - public void testSize() { - stack.push("aaa"); - stack.push("bbb"); - stack.push("ccc"); - stack.pop(); - stack.pop(); - System.out.println(stack.size()); - } - -} From c9f3a3bff10d13fe6921fb60a5e71c6dacb61afd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=AD=E5=B7=9E-=E5=A4=A7=E6=98=8E?= <420355244@qq.com> Date: Wed, 1 Mar 2017 22:08:31 +0800 Subject: [PATCH 7/9] change project --- group16/420355244/Homework1/.classpath | 7 + group16/420355244/Homework1/.gitignore | 1 + group16/420355244/Homework1/.project | 17 ++ .../.settings/org.eclipse.jdt.core.prefs | 11 + .../src/com/coding/basic/ArrayList.java | 91 +++++++ .../src/com/coding/basic/ArrayListTest.java | 62 +++++ .../src/com/coding/basic/BinaryTreeNode.java | 32 +++ .../src/com/coding/basic/Iterator.java | 7 + .../src/com/coding/basic/LinkedList.java | 224 ++++++++++++++++++ .../src/com/coding/basic/LinkedListTest.java | 112 +++++++++ .../Homework1/src/com/coding/basic/List.java | 9 + .../Homework1/src/com/coding/basic/Queue.java | 66 ++++++ .../src/com/coding/basic/QueueTest.java | 56 +++++ .../Homework1/src/com/coding/basic/Stack.java | 39 +++ .../src/com/coding/basic/StackTest.java | 62 +++++ 15 files changed, 796 insertions(+) create mode 100644 group16/420355244/Homework1/.classpath create mode 100644 group16/420355244/Homework1/.gitignore create mode 100644 group16/420355244/Homework1/.project create mode 100644 group16/420355244/Homework1/.settings/org.eclipse.jdt.core.prefs create mode 100644 group16/420355244/Homework1/src/com/coding/basic/ArrayList.java create mode 100644 group16/420355244/Homework1/src/com/coding/basic/ArrayListTest.java create mode 100644 group16/420355244/Homework1/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group16/420355244/Homework1/src/com/coding/basic/Iterator.java create mode 100644 group16/420355244/Homework1/src/com/coding/basic/LinkedList.java create mode 100644 group16/420355244/Homework1/src/com/coding/basic/LinkedListTest.java create mode 100644 group16/420355244/Homework1/src/com/coding/basic/List.java create mode 100644 group16/420355244/Homework1/src/com/coding/basic/Queue.java create mode 100644 group16/420355244/Homework1/src/com/coding/basic/QueueTest.java create mode 100644 group16/420355244/Homework1/src/com/coding/basic/Stack.java create mode 100644 group16/420355244/Homework1/src/com/coding/basic/StackTest.java diff --git a/group16/420355244/Homework1/.classpath b/group16/420355244/Homework1/.classpath new file mode 100644 index 0000000000..373dce4005 --- /dev/null +++ b/group16/420355244/Homework1/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group16/420355244/Homework1/.gitignore b/group16/420355244/Homework1/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group16/420355244/Homework1/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group16/420355244/Homework1/.project b/group16/420355244/Homework1/.project new file mode 100644 index 0000000000..ec1134f33a --- /dev/null +++ b/group16/420355244/Homework1/.project @@ -0,0 +1,17 @@ + + + Homework1 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group16/420355244/Homework1/.settings/org.eclipse.jdt.core.prefs b/group16/420355244/Homework1/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group16/420355244/Homework1/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/group16/420355244/Homework1/src/com/coding/basic/ArrayList.java b/group16/420355244/Homework1/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..6534c3c029 --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/ArrayList.java @@ -0,0 +1,91 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + if(size < elementData.length){ + elementData[size] = o; + }else{ + Object[] newElementData = new Object[elementData.length + elementData.length/2]; + System.arraycopy(elementData, 0, newElementData, 0, size); + newElementData[size] = o; + this.elementData = newElementData; + } + size++; + + } + public void add(int index, Object o){ + if(index >= 0 && index <= size){ + //1.不扩容 + if(index == size - 1){ + //1.1 加在最后 + elementData[index] = o; + }else{ + //1.2 加在前面 + //index的位置的数值变为改对象,index以后位置的都往后挪一位 + Object[] newElementData = new Object[elementData.length]; + System.arraycopy(elementData, 0, newElementData, 0, index); + newElementData[index] = o ; + System.arraycopy(elementData, index, newElementData, index + 1, size - index); + this.elementData = newElementData; + } + size++; + }else{ + throw new IndexOutOfBoundsException(); + } + } + + public Object get(int index){ + if(index < size){ + return elementData[index]; + }else{ + throw new IndexOutOfBoundsException(); + } + } + + public Object remove(int index){ + if(index < size){ + Object obj = elementData[index]; + Object[] newElementData = new Object[elementData.length]; + if(size != 1){ + //1.若集合长度为1 + if(0 == index){ + //1.1.如果remove的是0索引的 + System.arraycopy(elementData, 1, newElementData, 0, size - 1); + }else if(index == size -1){ + //1.2.如果remove的是最后索引的 + System.arraycopy(elementData, 0, newElementData, 0, size - 1); + }else{ + //1.3.在中间 + System.arraycopy(elementData, 0, newElementData, 0, index); + System.arraycopy(elementData, index + 1, newElementData, index, size - index - 1); + } + } + this.elementData = newElementData; + size--; + return obj; + }else{ + throw new IndexOutOfBoundsException(); + } + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + @Override + public String toString() { + return "ArrayList [size=" + size + ", elementData=" + Arrays.toString(elementData) + "]"; + } + + +} diff --git a/group16/420355244/Homework1/src/com/coding/basic/ArrayListTest.java b/group16/420355244/Homework1/src/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..420c412a7b --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/ArrayListTest.java @@ -0,0 +1,62 @@ +package com.coding.basic; + +import org.junit.Before; +import org.junit.Test; + +public class ArrayListTest { + + private static ArrayList arrayList = new ArrayList(); + + @Before + public void setUp() throws Exception { + } + + @Test + public void testAddObject() { + for(int i = 0 ;i < 150; i++){ + arrayList.add("aaa"); + } + System.out.println(arrayList); + System.out.println(arrayList.size()); + } + + @Test + public void testAddIntObject() { + arrayList.add("aaa"); + arrayList.add("bbb"); + arrayList.add("ccc"); + arrayList.add("ddd"); + arrayList.add(1,"eee"); + System.out.println(arrayList); + } + + @Test + public void testGet() { + arrayList.add("aaa"); + arrayList.add("bbb"); + arrayList.add("ccc"); + arrayList.add("ddd"); + Object object = arrayList.get(0); + System.out.println(object); + } + + @Test + public void testRemove() { + arrayList.add("aaa"); + arrayList.add("bbb"); + arrayList.add("ccc"); + arrayList.add("ddd"); + arrayList.remove(0); + System.out.println(arrayList); + } + + @Test + public void testSize() { + arrayList.add("aaa"); + arrayList.add("bbb"); + arrayList.add("ccc"); + arrayList.add("ddd"); + System.out.println(arrayList.size()); + } + +} diff --git a/group16/420355244/Homework1/src/com/coding/basic/BinaryTreeNode.java b/group16/420355244/Homework1/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group16/420355244/Homework1/src/com/coding/basic/Iterator.java b/group16/420355244/Homework1/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group16/420355244/Homework1/src/com/coding/basic/LinkedList.java b/group16/420355244/Homework1/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..9af35a399d --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/LinkedList.java @@ -0,0 +1,224 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + + private Node first; + + private Node last; + + private int size = 0; + + public void add(Object o){ + if(null == first){ + //当链表元素为空时,新建一个Node + Node node = new Node(); + node.data = o; + node.next = null; + first = node; + last = node; + size ++; + }else{ + addLast(o); + } + } + public void add(int index , Object o){ + if(index < 0 || index >= size){ + //数组越界异常 + throw new IndexOutOfBoundsException(); + }else{ + if(0 == index){ + //1.如果加在头上 + addFirst(o); + }else{ + //2.加在中间位置 + Node node = first.next; + int nodeIndex = 1; + if(nodeIndex == index){ + //如果是第二个位置的话 + Node nodeAdd = new Node(); + nodeAdd.data = o; + first.next = nodeAdd; + nodeAdd.next = node; + last = node; + size ++; + } + //第三个位置及以后、开始遍历所有的索引 + while(null != node.next){ + //保留遍历中node之前的结点 + Node nodeLast = node; + node = node.next; + nodeIndex++; + if(nodeIndex == index){ + Node nodeAdd = new Node(); + nodeAdd.data = o; + nodeLast.next = nodeAdd; + nodeAdd.next = node; + size ++; + break; + } + } + } + } + + } + public Object get(int index){ + if(index < 0 || index >= size){ + //数组越界异常 + throw new IndexOutOfBoundsException(); + }else{ + if(0 == index){ + //1.如果加在头上 + return first.data; + } + Node node = first.next; + int nodeIndex = 1; + if(nodeIndex == index){ + //如果是第二个位置的话 + return node.data; + } + //第三个位置及以后、开始遍历所有的索引 + while(null != node.next){ + //保留遍历中node之前的结点 + node = node.next; + nodeIndex++; + if(nodeIndex == index){ + return node.data; + } + } + } + throw new IndexOutOfBoundsException(); + } + public Object remove(int index){ + if(index < 0 || index >= size){ + //数组越界异常 + throw new IndexOutOfBoundsException(); + }else{ + if(0 == index){ + //1.如果移除头 + removeFirst(); + }else if(index == (size - 1)){ + //2.移除尾 + removeLast(); + }else{ + //3.移除中间位置 + Node node = first.next; + //从first的零号索引开始 + int nodeIndex = 1; + + //开始遍历所有的索引,记住要移除的索引位数据的前后结点 + Node lastNode = first; + if(index == nodeIndex){ + //第一次不匹配则后续的循环执行 + Object o = node.data; + lastNode.next = node.next; + size--; + return o; + }else{ + while(null != node.next){ + lastNode = node; + node = node.next; + nodeIndex++; + if(index == nodeIndex){ + Object o = node.data; + lastNode.next = node.next; + size--; + return o; + } + } + } + } + } + throw new IndexOutOfBoundsException(); + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node node = new Node(); + node.data = o ; + node.next = first; + first = node; + size++; + } + public void addLast(Object o){ + Node node = new Node(); + node.data = o ; + node.next = null; + last.next = node; + last = node; + size++; + } + public Object removeFirst(){ + Object o = first.data; + Node node = first.next; + first = node; + size--; + return o; + } + public Object removeLast(){ + if(0 == size){ + throw new NoSuchElementException(); + + }else if(1 == size){ + //只有一个元素 + removeFirst(); + }else{ + //第二个元素 + Node node = first.next; + if(null == node.next){ + Object o = node.data; + last = first; + first.next = null; + return o; + }else{ + while(null != node.next){ + //若不止只有2个 ,记录最后一个结点的前一个。 + Node lastNode = node; + node = node.next; + if(null == node.next){ + Object o = node.data; + lastNode.next = null; + last = lastNode; + size--; + return o; + } + } + } + } + throw new NoSuchElementException(); + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } + + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + if(null != first){ + sb.append(first.data.toString() + ","); + Node node = first.next; + sb.append(node.data.toString() + ","); + while(null != node.next){ + node = node.next; + sb.append(node.data.toString() + ","); + } + } + return sb.toString(); + } + + + +} diff --git a/group16/420355244/Homework1/src/com/coding/basic/LinkedListTest.java b/group16/420355244/Homework1/src/com/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..2caea9679b --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/LinkedListTest.java @@ -0,0 +1,112 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class LinkedListTest { + private static LinkedList linkedList = new LinkedList(); + @Before + public void setUp() throws Exception { + } + + @Test + public void testAddObject() { + linkedList.add("aaa"); + linkedList.add("bbb"); + System.out.println(linkedList); + } + + @Test + public void testAddIntObject() { + linkedList.add("aaa"); + linkedList.add("bbb"); + linkedList.add("ccc"); + linkedList.add(2,"ddd"); + System.out.println(linkedList); + System.out.println(linkedList.size()); + } + + @Test + public void testGet() { + linkedList.add("aaa"); + linkedList.add("bbb"); + linkedList.add("ccc"); + linkedList.add("eee"); + linkedList.add("fff"); + linkedList.add("ddd"); +// System.out.println(linkedList.size()); + System.out.println(linkedList.get(3)); + } + + @Test + public void testRemove() { + linkedList.add("aaa"); + linkedList.add("bbb"); + linkedList.add("ccc"); + linkedList.add("eee"); + linkedList.add("fff"); + linkedList.add("ddd"); + linkedList.remove(5); + linkedList.remove(1); + linkedList.remove(2); + System.out.println(linkedList); + System.out.println(linkedList.size()); + } + + @Test + public void testSize() { + linkedList.add("aaa"); + linkedList.add("bbb"); + linkedList.add("ccc"); + linkedList.add("eee"); + linkedList.add("fff"); + linkedList.add("ddd"); + System.out.println(linkedList.size()); + } + + @Test + public void testAddFirst() { + linkedList.add("aaa"); + linkedList.add("bbb"); + linkedList.addFirst("sss"); + System.out.println(linkedList); + } + + @Test + public void testAddLast() { + linkedList.add("aaa"); + linkedList.add("bbb"); + linkedList.add("ccc"); + System.out.println(linkedList); + } + + @Test + public void testRemoveFirst() { + linkedList.add("aaa"); + linkedList.add("bbb"); + linkedList.add("ccc"); + linkedList.removeFirst(); + linkedList.addFirst("eee"); + linkedList.removeFirst(); + System.out.println(linkedList); + } + + @Test + public void testRemoveLast() { + linkedList.add("aaa"); + linkedList.add("bbb"); + linkedList.add("ccc"); + linkedList.removeLast(); + linkedList.add("eee"); + linkedList.addFirst("xxx"); + System.out.println(linkedList); + } + + @Test + public void testIterator() { + fail("Not yet implemented"); + } + +} diff --git a/group16/420355244/Homework1/src/com/coding/basic/List.java b/group16/420355244/Homework1/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +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/group16/420355244/Homework1/src/com/coding/basic/Queue.java b/group16/420355244/Homework1/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..a2f9577b7b --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/Queue.java @@ -0,0 +1,66 @@ +package com.coding.basic; + +public class Queue { + private Node first; + private Node last; + private int size = 0; + public void enQueue(Object o){ + if(null == first){ + Node node = new Node(); + node.data = o; + node.next = null; + first = node; + last = node; + }else{ + Node node = new Node(); + node.data = o; + node.next = null; + last.next = node; + last = node; + } + size++; + } + + public Object deQueue(){ + Node second = first.next; + Object o = first.data; + if(null != second){ + first = second; + return o; + }else{ + first = null; + size = 0; + return o; + } + } + + public boolean isEmpty(){ + if(size > 0){ + return false; + }else{ + return true; + } + } + + public int size(){ + return size; + } + static class Node{ + Node next; + Object data; + } + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + if(null != first){ + sb.append(first.data.toString() + ","); + Node node = first.next; + sb.append(node.data.toString() + ","); + while(null != node.next){ + node = node.next; + sb.append(node.data.toString() + ","); + } + } + return sb.toString(); + } +} diff --git a/group16/420355244/Homework1/src/com/coding/basic/QueueTest.java b/group16/420355244/Homework1/src/com/coding/basic/QueueTest.java new file mode 100644 index 0000000000..50d7fc0903 --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/QueueTest.java @@ -0,0 +1,56 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class QueueTest { + + public static Queue queue = new Queue(); + @Before + public void setUp() throws Exception { + } + + @Test + public void testEnQueue() { + queue.enQueue("aaa"); + queue.enQueue("bbb"); + queue.enQueue("ccc"); + queue.enQueue("aaa"); + System.out.println(queue); + } + + @Test + public void testDeQueue() { + queue.enQueue("aaa"); + queue.enQueue("bbb"); + queue.enQueue("ccc"); + queue.enQueue("ddd"); + queue.enQueue("eee"); + queue.deQueue(); + System.out.println(queue); + } + + @Test + public void testIsEmpty() { + System.out.println(queue.isEmpty()); + + } + + @Test + public void testSize() { + queue.enQueue("aaa"); + queue.enQueue("bbb"); + queue.enQueue("ccc"); + queue.enQueue("ddd"); + queue.enQueue("eee"); + System.out.println(queue.size()); + } + + @Test + public void testToString() { + fail("Not yet implemented"); + } + +} diff --git a/group16/420355244/Homework1/src/com/coding/basic/Stack.java b/group16/420355244/Homework1/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..5c59bf34fc --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/Stack.java @@ -0,0 +1,39 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size = 0; + public void push(Object o){ + //入栈在栈顶进入最后压入 + elementData.add(o); + size ++; + } + + public Object pop(){ + Object object = elementData.get(size -1); + elementData.remove(size -1); + size --; + return object; + } + + public Object peek(){ + Object object = elementData.get(size -1); + return object; + } + public boolean isEmpty(){ + if(size <= 0){ + return true; + }else{ + return false; + } + } + public int size(){ + return size; + } + + @Override + public String toString() { + return elementData.toString(); + } + +} diff --git a/group16/420355244/Homework1/src/com/coding/basic/StackTest.java b/group16/420355244/Homework1/src/com/coding/basic/StackTest.java new file mode 100644 index 0000000000..b436785574 --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/StackTest.java @@ -0,0 +1,62 @@ +package com.coding.basic; + +import org.junit.Before; +import org.junit.Test; + +public class StackTest { + private static Stack stack = new Stack(); + @Before + public void setUp() throws Exception { + } + + @Test + public void testPush() { + stack.push("aaa"); + stack.push("bbb"); + stack.push("ccc"); + System.out.println(stack); + } + + @Test + public void testPop() { + stack.push("aaa"); + stack.push("bbb"); + stack.push("ccc"); + Object pop = stack.pop(); + System.out.println(pop); + System.out.println(stack); + } + + @Test + public void testPeek() { + stack.push("aaa"); + stack.push("bbb"); + stack.push("ccc"); + Object peek = stack.peek(); + System.out.println(peek); + } + + @Test + public void testIsEmpty() { + System.out.println(stack.isEmpty()); + stack.push("aaa"); + stack.push("bbb"); + stack.push("ccc"); + System.out.println(stack.isEmpty()); + stack.pop(); + stack.pop(); + stack.pop(); + System.out.println(stack.isEmpty()); + } + + @Test + public void testSize() { + stack.push("aaa"); + stack.push("bbb"); + stack.push("ccc"); + stack.pop(); + stack.pop(); + System.out.println(stack.size()); + } + +} From e6c1f6d2b359745ac8062a5a2d83f1857d0ffeae Mon Sep 17 00:00:00 2001 From: starlight0405 Date: Wed, 1 Mar 2017 22:14:06 +0800 Subject: [PATCH 8/9] second code --- .../src/com/coderising/array/ArrayUtil.java | 258 ++++++++++++++++++ .../com/coderising/litestruts/ActionXml.java | 37 +++ .../coderising/litestruts/LoginAction.java | 39 +++ .../src/com/coderising/litestruts/Struts.java | 150 ++++++++++ .../com/coderising/litestruts/StrutsTest.java | 43 +++ .../src/com/coderising/litestruts/View.java | 23 ++ .../src/com/coderising/litestruts/struts.xml | 11 + 7 files changed, 561 insertions(+) create mode 100644 group16/2816977791/secondExercise/src/com/coderising/array/ArrayUtil.java create mode 100644 group16/2816977791/secondExercise/src/com/coderising/litestruts/ActionXml.java create mode 100644 group16/2816977791/secondExercise/src/com/coderising/litestruts/LoginAction.java create mode 100644 group16/2816977791/secondExercise/src/com/coderising/litestruts/Struts.java create mode 100644 group16/2816977791/secondExercise/src/com/coderising/litestruts/StrutsTest.java create mode 100644 group16/2816977791/secondExercise/src/com/coderising/litestruts/View.java create mode 100644 group16/2816977791/secondExercise/src/com/coderising/litestruts/struts.xml diff --git a/group16/2816977791/secondExercise/src/com/coderising/array/ArrayUtil.java b/group16/2816977791/secondExercise/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..3572229739 --- /dev/null +++ b/group16/2816977791/secondExercise/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,258 @@ +package com.coderising.array; + +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) { + int i = 0; + while (i < origin.length - 1 - i) { + int temp = origin[origin.length - 1 - i]; + origin[origin.length - 1 - i] = origin[i]; + origin[i] = temp; + i++; + } + } + + /** + * 现在有如下的一个数组: 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) { + int point = 0; + for (int i : oldArray) { + if (i != 0) { + oldArray[point++] = i; + } + } + int[] newArray = new int[point]; + System.arraycopy(oldArray, 0, newArray, 0, point); + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, 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) { + //两个指针 + int pos1 = 0; + int pos2 = 0; + int point = 0; + int[] newArray = new int[array1.length + array2.length]; + while (pos1 < array1.length && pos2 < array2.length) { + if (array1[pos1] > array2[pos2]) { + newArray[point++] = array2[pos2++]; + } else if (array1[pos1] < array2[pos2]) { + newArray[point++] = array1[pos1++]; + } else { + newArray[point++] = array1[pos1++]; + pos2++; + } + } + while (pos1 < array1.length) { + newArray[point++] = array1[pos1++]; + } + while (pos1 < array2.length) { + newArray[point++] = array1[pos2++]; + } + int[] array = new int[point]; + System.arraycopy(newArray, 0, array, 0, point); + return array; + } + + /** + * 把一个已经存满数据的数组 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) { + int[] newArray = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + int a = 0; + int b = 1; + int[] array = new int[max]; + int i = 0; + if (max >= 2) { + array[i++] = 1; + } + while (a + b < max) { + int temp = b; + b = a + b; + a = temp; + array[i++] = b; + } + int[] newArray = new int[i]; + System.arraycopy(array, 0, newArray, 0, i); + return newArray; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + int[] array = new int[max / 2 + 1]; + int pos = 0; + for (int i = 1; i < max; i++) { + if (prime(i)) { + array[pos++] = i; + } + } + int[] newArray = new int[pos]; + System.arraycopy(array, 0, newArray, 0, pos); + return newArray; + } + + private boolean prime(int value) { + if (value < 2) { + return false; + } else if (value == 2) { + return true; + } else { + for (int i = 2; i < value / 2 + 1; i++) { + if (value % 2 == 0) { + return false; + } + } + return true; + } + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + int pos = 0; + int[] array = new int[max]; + for (int i = 1; i < max; i++) { + if (perfectNumber(i)) { + array[pos++] = i; + } + } + int[] newArray = new int[pos]; + System.arraycopy(array, 0, newArray, 0, pos); + return newArray; + } + + private boolean perfectNumber(int value) { + if (value == 1 || prime(value)) { + return false; + } else { + int sum = 0; + for (int i = 1; i <= value / 2; i++) { + if (value % i == 0) { + sum += i; + } + } + if (sum == value) { + return true; + } + return false; + } + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public String join(int[] array, String seperator) { + String out = ""; + for (int i = 0; i < array.length; i++) { + if (i == 0) { + out += String.valueOf(i); + } else { + out += seperator + String.valueOf(i); + } + } + return out; + } + + + public static void main(String[] args) { + ArrayUtil arrayUtil = new ArrayUtil(); + //reverse + int[] a = {7, 9, 30, 3}; + arrayUtil.reverseArray(a); + System.out.println(a); + + //remove zero + int[] zero = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5}; + int[] afterZero = arrayUtil.removeZero(zero); + System.out.println(afterZero); + + //merge + int[] a1 = {3, 5, 7, 8}; + int[] a2 = {4, 5, 6, 7}; + int[] merge = arrayUtil.merge(a1, a2); + System.out.println(merge); + + //grow + int[] oldArray = {2, 3, 6}; + int[] grow = arrayUtil.grow(oldArray, 3); + System.out.println(grow); + + //fibonacci + int[] fibonacci = arrayUtil.fibonacci(2); + System.out.println(fibonacci); + + //primes + int[] primes = arrayUtil.getPrimes(15); + System.out.println(primes); + + //perfect + int[] perfect = arrayUtil.getPerfectNumbers(500); + System.out.println(perfect); + + //join + int[] joinArray = {2}; + String join = arrayUtil.join(joinArray, "-"); + System.out.println(join); + + } +} diff --git a/group16/2816977791/secondExercise/src/com/coderising/litestruts/ActionXml.java b/group16/2816977791/secondExercise/src/com/coderising/litestruts/ActionXml.java new file mode 100644 index 0000000000..55988befbe --- /dev/null +++ b/group16/2816977791/secondExercise/src/com/coderising/litestruts/ActionXml.java @@ -0,0 +1,37 @@ +package com.coderising.litestruts; + +import java.util.Map; + +/** + * @author nvarchar + * date 2017/3/1 + */ +public class ActionXml { + private String name; + private String className; + private Map map; + + public String getClassName() { + return className; + } + + public void setClassName(String className) { + this.className = className; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Map getMap() { + return map; + } + + public void setMap(Map map) { + this.map = map; + } +} diff --git a/group16/2816977791/secondExercise/src/com/coderising/litestruts/LoginAction.java b/group16/2816977791/secondExercise/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..1005f35a29 --- /dev/null +++ b/group16/2816977791/secondExercise/src/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @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/group16/2816977791/secondExercise/src/com/coderising/litestruts/Struts.java b/group16/2816977791/secondExercise/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..f169cb1cbb --- /dev/null +++ b/group16/2816977791/secondExercise/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,150 @@ +package com.coderising.litestruts; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + + +public class Struts { + + private static Map actionXmlMap; + + static { + //0. 读取配置文件struts.xml + actionXmlMap = parserXml("src/com/coderising/litestruts/struts.xml"); + System.out.println("parse end"); + } + + public static View runAction(String actionName, Map parameters) { + + try { + ActionXml actionXml = actionXmlMap.get(actionName); + if (actionName == null) { + return null; + } else { + String className = actionXml.getClassName(); + Class cls = Class.forName(className); + Object obj = cls.newInstance(); + /** + 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + */ + Method[] methods = cls.getMethods(); + for (String field : parameters.keySet()) { + for (Method method : methods) { + if (method.getName().startsWith("set") && method.getName().toLowerCase().endsWith(field.toLowerCase())) { + method.invoke(obj, parameters.get(field)); + break; + } + } + } + Method action = cls.getMethod("execute"); + //通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + String result = (String) action.invoke(obj); + /** + 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + */ + Field[] fields = cls.getDeclaredFields(); + Map params = new HashMap<>(); + for (Field field : fields) { + for (Method method : methods) { + if (method.getName().startsWith("get") && method.getName().toLowerCase().endsWith(field.getName().toLowerCase())) { + String viewResult = (String) method.invoke(obj); + params.put(field.getName(), viewResult); + break; + } + } + } + /** + 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + */ + View view = new View(); + view.setParameters(params); + view.setJsp(actionXmlMap.get(actionName).getMap().get(result)); + return view; + } + } catch (ClassNotFoundException e) { + e.printStackTrace(); + return null; + } catch (InstantiationException e) { + e.printStackTrace(); + return null; + } catch (IllegalAccessException e) { + e.printStackTrace(); + return null; + } catch (NoSuchMethodException e) { + e.printStackTrace(); + return null; + } catch (InvocationTargetException e) { + e.printStackTrace(); + return null; + } + } + + //解析xml文件 + private static Map parserXml(String fileName) { + try { + Map map = new HashMap<>(); + //create documentBuilder + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + DocumentBuilder db = dbf.newDocumentBuilder(); + //create document + Document document = db.parse(fileName); + //extract root element + Element root = document.getDocumentElement(); + System.out.println("Root element :" + document.getDocumentElement().getNodeName()); + NodeList nodeList = document.getElementsByTagName("action"); + System.out.println("----------------------------"); + for (int temp = 0; temp < nodeList.getLength(); temp++) { + Node nNode = nodeList.item(temp); + System.out.println("\nCurrent Element :" + + nNode.getNodeName()); + if (nNode.getNodeType() == Node.ELEMENT_NODE) { + ActionXml actionXml = new ActionXml(); + Element eElement = (Element) nNode; + System.out.println("action name : " + + eElement.getAttribute("name")); + System.out.println("class name : " + + eElement.getAttribute("class")); + actionXml.setName(eElement.getAttribute("name")); + actionXml.setClassName(eElement.getAttribute("class")); + NodeList result = eElement.getElementsByTagName("result"); + Map results = new HashMap<>(); + for (int i = 0; i < result.getLength(); i++) { + Node resultNode = result.item(i); + if (resultNode.getNodeType() == Node.ELEMENT_NODE) { + Element resultElement = (Element) resultNode; + System.out.println("result name:" + resultElement.getAttribute("name")); + System.out.println("result context:" + resultElement.getTextContent()); + results.put(resultElement.getAttribute("name"), resultElement.getTextContent()); + } + actionXml.setMap(results); + } + map.put(actionXml.getName(), actionXml); + } + } + return map; + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + public static void main(String[] args) { +// parserXml("src/com/coderising/litestruts/struts.xml"); + } +} diff --git a/group16/2816977791/secondExercise/src/com/coderising/litestruts/StrutsTest.java b/group16/2816977791/secondExercise/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a44c1878ac --- /dev/null +++ b/group16/2816977791/secondExercise/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +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 = "login"; + + 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/group16/2816977791/secondExercise/src/com/coderising/litestruts/View.java b/group16/2816977791/secondExercise/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group16/2816977791/secondExercise/src/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +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/group16/2816977791/secondExercise/src/com/coderising/litestruts/struts.xml b/group16/2816977791/secondExercise/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..246b3595ad --- /dev/null +++ b/group16/2816977791/secondExercise/src/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file From 3fd1841d683cc4b2e8971f1b25cfc0300c374d9a Mon Sep 17 00:00:00 2001 From: gaodekui <13526428940@163.com> Date: Thu, 2 Mar 2017 09:10:16 +0800 Subject: [PATCH 9/9] =?UTF-8?q?=E6=8F=90=E4=BA=A4ArrayUtil?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/list20170226/ArrayUtil.java | 276 ++++++++++++++++++ 1 file changed, 276 insertions(+) create mode 100644 group16/1154151360/src/com/list20170226/ArrayUtil.java diff --git a/group16/1154151360/src/com/list20170226/ArrayUtil.java b/group16/1154151360/src/com/list20170226/ArrayUtil.java new file mode 100644 index 0000000000..a09c544787 --- /dev/null +++ b/group16/1154151360/src/com/list20170226/ArrayUtil.java @@ -0,0 +1,276 @@ +package com.list20170226; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import com.sun.org.apache.xalan.internal.xsltc.compiler.sym; + +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){ + + int [] temp = new int [origin.length]; + + for (int i = 0; i < temp.length; i++){ + temp [i] = origin [temp.length - 1 - i]; + } + + origin = temp; + } + + /** + * 现在有如下的一个数组: 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){ + + int newLength = 0; + + for (int i = 0; i < oldArray.length ; i++){ + if (oldArray[i] == 0) + newLength ++; + } + + int [] newArray = new int [oldArray.length - newLength]; + int index = 0; + for (int j = 0; j < oldArray.length; j++){ + if (oldArray[j] != 0) + newArray[index++] = oldArray[j]; + } + + newArray = sort(newArray, 0, newArray.length - 1); + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, 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){ + + int newLength = array1.length + array2.length; + int [] newArray = new int [newLength]; + System.arraycopy(array1, 0, newArray, 0, array1.length); + System.arraycopy(array2, 0, newArray, array1.length, array2.length); + Set arraySet = new HashSet(); + for (int i = 0; i < newArray.length; i++){ + arraySet.add(newArray[i]); + } + int [] tempArray = new int[arraySet.size()]; + Iterator iterator = arraySet.iterator(); + int index = 0; + while (iterator.hasNext()){ + tempArray[index++] = iterator.next(); + } + newArray = sort(tempArray, 0, tempArray.length - 1); + return newArray; + } + /** + * 把一个已经存满数据的数组 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){ + int [] newArray = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, newArray, 0, size); + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + if (max > 1){ + List array = new ArrayList(); + array.add(1); + array.add(1); + for(int i = 2;;i++){ + array.add(array.get(i - 1) + array.get(i - 2)); + if (array.get(i) > max){ + array.remove(i); + break; + } + } + int [] result = returnArray(array); + return result; + }else{ + int [] b = new int[]{}; + return b; + } + + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + ArrayList list1 = new ArrayList(); + ArrayList list2 = new ArrayList(); + for (int i = 2; i < max; i++){ + list1.add(i); + } + + for (int m = 0; m < list1.size(); m++){ + boolean flag = true; + for (int n = 2; n < list1.get(m); n++){ + if (list1.get(m) % n == 0){ + flag = false; + break; + } + + } + if (flag) + list2.add(list1.get(m)); + } + + int [] result = returnArray(list2); + + return result; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + ArrayList array = new ArrayList(); + Map> arrayMap = new HashMap>(); + ArrayList array2 = new ArrayList(); + for (int i = 2; i < max; i++){ + array.add(i); + } + for(int m = 0; m < array.size(); m++){ + ArrayList tempArray = new ArrayList(); + for (int n = 2; n < array.get(m);n++){ + if (array.get(m) % n == 0) + tempArray.add(n); + } + arrayMap.put(array.get(m), tempArray); + } + for(Map.Entry> entry: arrayMap.entrySet()){ + Integer key = entry.getKey(); + ArrayList tempArray = entry.getValue(); + Integer tempInt = 0; + for (Integer i:tempArray){ + tempInt += i; + } + if (key == tempInt){ + array2.add(key); + } + } + int [] result = returnArray(array2); + + return result; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + + StringBuilder builder = new StringBuilder(); + + for (int i = 0; i < array.length; i++){ + builder.append(array[i]); + builder.append(seperator); + } + builder.deleteCharAt(builder.length() - 1); + return builder.toString(); + } +//***************************工具类***************************************** + public int [] returnArray( List list){ + int [] result = new int[list.size()]; + int index = 0; + Iterator iterator = list.iterator(); + while(iterator.hasNext()){ + result[index++] = iterator.next(); + } + return result; + } + + //快速排序算法 + public int [] sort (int [] array, int low, int height ){ + + int start = low; + int end = height; + int key = array[low]; + + while (end > start){ + //从后往前遍历 + while (end > start && array[end] >= key) + end--; + + if (array[end] <= key){ + int temp = array[end]; + array[end] = array[start]; + array[start] = temp; + } + + //从前往后遍历 + while(end > start && array[start] <= key) + start++; + + if (array[start] >= key){ + int temp = array[start]; + array[start] = array[end]; + array[end] = temp; + } + } + + if(start > low)sort(array, low, start - 1); + if (end < height)sort(array, end + 1, height); + + return array; + } +//******************************************************************8 +public static void main(String[] args) { + + ArrayUtil util = new ArrayUtil(); + int [] a1 = {3, 5, 7,8}; + int [] a2 = {4, 5, 6,7}; + int [] a3 = util.getPrimes(23); + + for(int c:a3){ + System.out.print(c+" "); + } + + } +}