diff --git a/group24/815591664/2017Learning/src/com/coderising/array/ArrayUtil.java b/group24/815591664/2017Learning/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..7c30103d89 --- /dev/null +++ b/group24/815591664/2017Learning/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,348 @@ +package com.coderising.array; + +import java.util.ArrayList; +import java.util.Arrays; + +import java.util.List; + + +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 static void main(String[] args) { + int[] a = {7, 9, 30, 3, 4}; + reverseArray(a); + System.out.println(Arrays.toString(a)); + + + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5,0} ; + System.out.println(Arrays.toString(removeZero(oldArr))); + + + int[] a1 = {3, 5, 7,8}; + int[] a2 = {4, 5, 6,7}; + + System.out.println(Arrays.toString(merge(a1,a2))); + + + int[] b = { 2,3,6}; + System.out.println(Arrays.toString(grow(b,5))); + + System.out.println(genFibonacci(5)); + + System.out.println(Arrays.toString(fibonacci(30))); + + System.out.println(Arrays.toString(getPrimes(10000))); + + System.out.println(getFactor(10)); + + System.out.println(isPerfectNum(1000)); + +// System.out.println(); + System.out.println(Arrays.toString(getPerfectNumbers(100))); + + System.out.println(join(a,"&")); + + + } + public static void reverseArray(int[] origin){ + + if(origin.length==0){ + return; + + } + int[] copy = new int[origin.length]; + System.arraycopy(origin, 0, copy, 0, origin.length); + + for (int i = 0; i < copy.length; i++) { + + origin[i] = copy[copy.length-1-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 static int[] removeZero(int[] oldArray){ + int newSize = 0; + for (int i = 0; i < oldArray.length; i++) { + if(oldArray[i]!=0){ + newSize++; + } + } + int index = 0; + int[] newArr = new int[newSize]; + for (int i = 0; i < oldArray.length; i++) { + if(oldArray[i]!=0){ + newArr[index] = oldArray[i]; + index++; + } + } + return newArr; + } + + /** + * 给定两个已经排序好的整形数组, 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 static int[] merge(int[] array1, int[] array2){ + Arrays.sort(array1); + Arrays.sort(array2); + + int[] newArr = new int[array1.length+array2.length]; + + System.arraycopy(array1, 0, newArr, 0,array1.length ); + System.arraycopy(array2, 0, newArr, array1.length, array2.length); + Arrays.sort(newArr); + List list = new ArrayList(); + for(int i=0;i list = new ArrayList(); + for(int i =0;i list = new ArrayList(); + for(int i=2;i<=max;i++){ + if(isPrime(i)){ + list.add(i); + } + } + + return listToArray(list); + + + } + + public static int[] listToArray(List list){ + if(list == null){ + return null; + } + + int[] arr = new int[list.size()]; + + for(int i=0;i list = new ArrayList(); + for(int i=1;i<=max;i++){ + if(isPerfectNum(i)){ + list.add(i); + } + } + + return listToArray(list); + } + + + public static boolean isPerfectNum(int num){ + if(num <=1){ + return false; + } + + List factors = getFactor(num); + int sum = 0; + for (Integer integer : factors) { + sum = integer+sum; + } + if(sum == num){ + return true; + } + return false; + } + + public static List getFactor(int num){ + List list = new ArrayList(); + list.add(1); + + + for(int i=2;i getFactor(int num){ + List list = new ArrayList(); + list.add(1); + int temp = num; + + while(!isPrime(temp)){ + if(temp ==1){ + break; + } + for(int i=2;i<=temp;i++){ + if(temp % i ==0){ + list.add(i); + temp = temp/i; + break; + } + } + + } + list.add(temp); + + return list; + }*/ + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator){ + StringBuilder sb = new StringBuilder(); + for (int i : array) { + sb.append(i); + sb.append(seperator); + + } + + return sb.subSequence(0, sb.length()-1).toString(); + } + + +} diff --git a/group24/815591664/2017Learning/src/com/coderising/litestruts/LoginAction.java b/group24/815591664/2017Learning/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group24/815591664/2017Learning/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/group24/815591664/2017Learning/src/com/coderising/litestruts/Struts.java b/group24/815591664/2017Learning/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..550d47ec2b --- /dev/null +++ b/group24/815591664/2017Learning/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,137 @@ +package com.coderising.litestruts; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.net.URLDecoder; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.jdom2.Document; +import org.jdom2.Element; +import org.jdom2.JDOMException; +import org.jdom2.input.SAXBuilder; +import org.jdom2.output.XMLOutputter; + +import com.sun.istack.internal.Builder; + + + +public class Struts { + + @SuppressWarnings("deprecation") + 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字段中。 + + */ + View view = new View(); + String xmlpath = Struts.class.getResource("struts.xml").getFile(); + SAXBuilder builder = null; + try { + xmlpath = URLDecoder.decode(xmlpath, "utf-8"); + builder = new SAXBuilder(false); + Document doc = builder.build(xmlpath); + Element root = doc.getRootElement(); + Element action = root.getChild("action"); + String className = action.getAttributeValue("class"); + Class clazz = Class.forName(className); + Object logAction = clazz.newInstance(); + for(String key :parameters.keySet()){ + String methodName = "set"+ Struts.captureName(key); + Method method = null; + try { + method = clazz.getMethod(methodName, String.class); + } catch (SecurityException e) { + e.printStackTrace(); + break; + } catch (NoSuchMethodException e) { + break; + } + method.invoke(logAction, parameters.get(key)); + + } + Method executeMethod = clazz.getMethod("execute", (Class[])null); + String result = (String)executeMethod.invoke(logAction, (Object[])null); + Method[] declareMtds = clazz.getDeclaredMethods(); + Map paramForView = new HashMap(); + for (Method method : declareMtds) { + String methodName = method.getName(); + if(methodName.startsWith("get")){ + paramForView.put(methodName.substring(3).toLowerCase(), (String)method.invoke(logAction, (Object[])null)); + } + + } + view.setParameters(paramForView); + List results = action.getChildren("result"); + for (Element element : results) { + String resultName = element.getAttributeValue("name"); + if(result.equals(resultName)){ + view.setJsp(element.getText()); + } + } + + } catch (JDOMException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return view; + } + + + public static String captureName(String name) { + // name = name.substring(0, 1).toUpperCase() + name.substring(1); +// return name; + char[] cs=name.toCharArray(); + cs[0]-=32; + return String.valueOf(cs); + + + } + +} diff --git a/group24/815591664/2017Learning/src/com/coderising/litestruts/StrutsTest.java b/group24/815591664/2017Learning/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group24/815591664/2017Learning/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/group24/815591664/2017Learning/src/com/coderising/litestruts/View.java b/group24/815591664/2017Learning/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group24/815591664/2017Learning/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/group24/815591664/2017Learning/src/com/coding/basic/ArrayList.java b/group24/815591664/2017Learning/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..e5fec6fceb --- /dev/null +++ b/group24/815591664/2017Learning/src/com/coding/basic/ArrayList.java @@ -0,0 +1,134 @@ +package com.coding.basic; + +import java.util.Arrays; +import java.util.LinkedList; + +/** + * @author hugaoqing + * created on 2017-3-8 + */ +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[3]; + + /* + * 添加元素 + * + */ + public void add(Object o){ + /*if(elementData.length == size){ + Object[] buffer = new Object[size+15]; + System.arraycopy(elementData, 0, buffer, 0, size); + elementData = buffer; + elementData[size] = o; + size++; + }else { + + elementData[size] = o; + size++; + }*/ + add(size, o); + + } + + + /* + * + * 指定位置添加元素 + */ + public void add(int index, Object o){ + if(index <0 || index > size){ + throw new IndexOutOfBoundsException(); + } + if(size+1=size){ + throw new IndexOutOfBoundsException(); + } + return elementData[index]; + } + + public Object remove(int index){ + if(index<0||index>=size){ + throw new IndexOutOfBoundsException(); + } + Object out = elementData[index]; + Object[] temp = new Object[size-index-1]; + System.arraycopy(elementData, index+1, temp, 0, size-index-1); + System.arraycopy(temp, 0, elementData,index, size-index-1); + //将移除的元素的指针去掉,避免内存泄漏 + elementData[size-1] = null; + size--; + return out; + } + + public int size(){ + return this.size; + } + + @Override + public String toString() { + Object[] objs = new Object[size]; + System.arraycopy(elementData, 0,objs , 0, size); + return Arrays.toString(objs); + + } + public Iterator iterator(){ + return new Iterator() { + int cursor = 0; + public Object next() throws Exception { + cursor++; + return get(cursor-1); + } + + public boolean hasNext() { + + return this.cursor0){ + while(curNode.getRight()!=null){ + curNode = curNode.getRight(); + } + curNode = node; + + }else{ + while(curNode.getLeft()!=null){ + curNode = curNode.getLeft(); + } + curNode = node; + } + + } + return null; + } + +} diff --git a/group24/815591664/2017Learning/src/com/coding/basic/BinaryTreeNode.java b/group24/815591664/2017Learning/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..4bd92572c9 --- /dev/null +++ b/group24/815591664/2017Learning/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,109 @@ +package com.coding.basic; + + + +/** + * @author hugaoqing + * created on 2017-3-11 + */ +public class BinaryTreeNode { + + private Comparable data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(Comparable data2) { + // TODO Auto-generated constructor stub + } + public BinaryTreeNode() { + // TODO Auto-generated constructor stub + } + /*public BinaryTreeNode(Comparable data) { + super(); + this.data = data; + }*/ + public Comparable getData() { + return data; + } + public void setData(Comparable 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(Comparable data){ + /*if(this.data==null){ + return new BinaryTreeNode(o); + } + + BinaryTreeNode curNode = this; + while(curNode != null){ + if(curNode.data.compareTo(o) == 0){ + return curNode; + } + else if(o.compareTo(curNode.data) < 0){ + BinaryTreeNode node = curNode; + curNode = curNode.left; + if(curNode == null){ + curNode = new BinaryTreeNode(o); + node.left = curNode; + return curNode; + } + + + }else if(o.compareTo(curNode.data) > 0){ + BinaryTreeNode node = curNode; + curNode = curNode.right; + if(curNode == null){ + curNode = new BinaryTreeNode(o); + node.right = curNode; + return curNode; + } + } + } + return curNode;*/ + BinaryTreeNode curNode = this; + BinaryTreeNode insertNode = new BinaryTreeNode(); + insertNode.setData(data); + + while(curNode != null){ + if(null == curNode.data){ + curNode.setData(data); + break; + }else{ + Comparable dataOfNode = curNode.getData(); + if(dataOfNode.compareTo(data) == 0){ + break; + }else if(dataOfNode.compareTo(data) < 0){ + BinaryTreeNode leftNode = curNode.left; + if(null == leftNode){ + curNode.setLeft(insertNode); + break; + } + curNode = leftNode; + }else{ + BinaryTreeNode rightNode = curNode.right; + if(null == rightNode){ + curNode.setRight(insertNode); + break; + } + curNode = rightNode; + } + } + } + return insertNode; + + + } + +} diff --git a/group24/815591664/2017Learning/src/com/coding/basic/Iterator.java b/group24/815591664/2017Learning/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..d4656a7daf --- /dev/null +++ b/group24/815591664/2017Learning/src/com/coding/basic/Iterator.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface Iterator { + //ӿijԱĬ϶final static +// int cursor = 0; + public boolean hasNext(); + public Object next() throws Exception; + +} diff --git a/group24/815591664/2017Learning/src/com/coding/basic/LinkedList.java b/group24/815591664/2017Learning/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..3d7e06e19f --- /dev/null +++ b/group24/815591664/2017Learning/src/com/coding/basic/LinkedList.java @@ -0,0 +1,223 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private int size; + + + public void add(Object o){ + this.addLast(o); + + } + public void add(int index , Object o){ + if(index<0 || index>size){ + throw new IndexOutOfBoundsException(); + } + if(index==0){ + this.addFirst(o); + size++; + return; + }else if(index==size){ + this.addLast(o); + size++; + return; + } + Node preNode = this.getNode(index-1); + Node curNode = this.getNode(index); + Node newNode = new Node(o, curNode); + preNode.next = newNode; + + + size++; + + + } + + private Node getNode(int index){ + if(index <0 || index>=size){ + throw new IndexOutOfBoundsException(); + } + if(index ==0){ + return head; + } + Node curNode = head; + for(int i=1;i<=index;i++){ + curNode = head.next; + } + return curNode; + } + + public Object get(int index){ + if(index<0 || index>=size){ + throw new IndexOutOfBoundsException(); + } + + Node temp = head; + for(int i =1;i<=index;i++){ + temp = temp.next; + } + return temp.data; + } + public Object remove(int index){ + if(index<0 || index>=size){ + throw new IndexOutOfBoundsException(); + } + Object o = null; + if(size == 1){ + o = head.data; + size--; + return o; + } + if(index==0){ + o = head.data; + Node afterHead = head.next; + head = afterHead; + + }else if(index==size-1){ + Node preTail = getNode(index-1); + Node tail = preTail.next; + o = tail.data; + preTail.next = null; + }else{ + Node preCur = getNode(index-1); + Node cur = preCur.next; + Node nextCur = cur.next; + o = cur.data; + preCur.next = nextCur; + + } + size--; + return o; + + + + + + + + } + + public int size(){ + return this.size; + } + + public void addFirst(Object o){ + Node node = new Node(o,null); + + if(head == null){ + head = node; + size++; + return; + } + head = new Node(o, head); + size++; + + } + public void addLast(Object o){ + //½ڵnextָָtail + Node add = new Node(o, null); + if(head==null){ + head = add; + size++; + return; + } + Node curNode = head; + while(curNode.next != null){ + curNode = curNode.next; + } + + curNode.next = add; + size++; + } + + + public Object removeFirst() throws Exception{ + return this.remove(0); + } + public Object removeLast() throws Exception{ + return this.remove(size-1); + } + + private class Itr implements Iterator{ + int cursor = 0; + public boolean hasNext() { + return cursor 0){ + randomAccessFile.write(buffer, 0, length); + total += length; + /* + * 将当前现在到的位置保存到文件中 + */ + downThreadStream.seek(0); + downThreadStream.write((startIndex + total + "").getBytes("UTF-8")); + } + + downThreadStream.close(); + inputStream.close(); + randomAccessFile.close(); + cleanTemp(downThreadFile);//删除临时文件 + System.out.println("线程"+ threadId + "下载完毕"); + }else{ + System.out.println("响应码是" +connection.getResponseCode() + ". 服务器不支持多线程下载"); + } + } catch (Exception e) { + e.printStackTrace(); + } + + } + } + + //删除线程产生的临时文件 + private synchronized void cleanTemp(File file){ + file.delete(); + } + + //获取下载文件的名称 + private String getFileName(URL url){ + String filename = url.getFile(); + return filename.substring(filename.lastIndexOf("/")+1); + } + + public static void main(String[] args) { + try { + new Test().download(); + + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/group27/815591664/2017Learning/src/com/coderising/download/api/Connection.java b/group27/815591664/2017Learning/src/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..7a8c9e7600 --- /dev/null +++ b/group27/815591664/2017Learning/src/com/coderising/download/api/Connection.java @@ -0,0 +1,25 @@ +package com.coderising.download.api; + +import java.io.IOException; +import java.net.HttpURLConnection; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group27/815591664/2017Learning/src/com/coderising/download/api/ConnectionException.java b/group27/815591664/2017Learning/src/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..1551a80b3d --- /dev/null +++ b/group27/815591664/2017Learning/src/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group27/815591664/2017Learning/src/com/coderising/download/api/ConnectionManager.java b/group27/815591664/2017Learning/src/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..ce045393b1 --- /dev/null +++ b/group27/815591664/2017Learning/src/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group27/815591664/2017Learning/src/com/coderising/download/api/DownloadListener.java b/group27/815591664/2017Learning/src/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/group27/815591664/2017Learning/src/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group27/815591664/2017Learning/src/com/coderising/download/impl/ConnectionImpl.java b/group27/815591664/2017Learning/src/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..2d1053ebae --- /dev/null +++ b/group27/815591664/2017Learning/src/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,44 @@ +package com.coderising.download.impl; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + + private HttpURLConnection conn = null; + + public ConnectionImpl(HttpURLConnection conn) { + super(); + this.conn = conn; + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + conn.setRequestProperty("Range", "bytes="+ startPos + "-" + endPos); + InputStream is = conn.getInputStream(); + byte[] buffer = new byte[endPos - startPos + 1]; + is.read(buffer, 0, endPos - startPos + 1); + + return buffer; + + } + + @Override + public int getContentLength(){ + return conn.getContentLength(); + } + + @Override + public void close() { + + if(conn != null){ + conn.disconnect(); + } + } + +} diff --git a/group27/815591664/2017Learning/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group27/815591664/2017Learning/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..69817bd783 --- /dev/null +++ b/group27/815591664/2017Learning/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,47 @@ +package com.coderising.download.impl; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + + try { + URL urlObj = new URL(url); + HttpURLConnection conn = (HttpURLConnection)urlObj.openConnection(); + //超时 + conn.setConnectTimeout(3*1000); + //conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); + conn.setRequestMethod("GET"); + return new ConnectionImpl(conn); + } catch (Exception e) { + throw new ConnectionException(); + } + + } + + public static void main(String[] args) throws ConnectionException, IOException { + ConnectionManager cm = new ConnectionManagerImpl(); + Connection conn = cm.open("http://localhost:8080/ForDownload/test.jpg"); + + System.out.println(conn.getContentLength()); + + byte[] content = conn.read(0, conn.getContentLength()-1); + OutputStream os = new FileOutputStream("d:test.jpg"); + os.write(content); + os.flush(); + + conn.close(); + os.close(); + } + +} diff --git a/group27/815591664/2017Learning/src/com/coding/basic/LinkedList.java b/group27/815591664/2017Learning/src/com/coding/basic/LinkedList.java index 6f2fbf1f5b..abb912f644 100644 --- a/group27/815591664/2017Learning/src/com/coding/basic/LinkedList.java +++ b/group27/815591664/2017Learning/src/com/coding/basic/LinkedList.java @@ -3,7 +3,6 @@ public class LinkedList implements List { private Node head; - private Node tail; private int size; @@ -11,100 +10,90 @@ public void add(Object o){ this.addLast(o); } - public void add(int index , Object o) throws Exception{ + public void add(int index , Object o){ + if(index<0 || index>size){ + throw new IndexOutOfBoundsException(); + } if(index==0){ this.addFirst(o); + size++; return; - }else if(index==size-1){ + }else if(index==size){ this.addLast(o); + size++; return; - }else{ - - - Node curNode = this.getNode(index); - Node pre = curNode.previous; -// Node next = curNode.next; - // - Node newNode = new Node(o, pre, curNode); - curNode.previous = newNode; - pre.next = newNode; } + Node preNode = this.getNode(index-1); + Node curNode = this.getNode(index); + Node newNode = new Node(o, curNode); + preNode.next = newNode; + + size++; } - private Node getNode(int index) throws Exception{ - if(index>=size){ - throw new Exception("±곬"); + + private Node getNode(int index){ + if(index <0 || index>=size){ + throw new IndexOutOfBoundsException(); } if(index ==0){ return head; - }else if(index==size-1){ - return tail; - - }else{ - Node temp = head; - for(int i =1;i<=index;i++){ - temp = temp.next; - } - return temp; } + Node curNode = head; + for(int i=1;i<=index;i++){ + curNode = curNode.next; + } + return curNode; } - public Object get(int index) throws Exception{ - if(index>=size){ - throw new Exception("±곬"); + + public Object get(int index){ + if(index<0 || index>=size){ + throw new IndexOutOfBoundsException(); } - if(index ==0){ - return head.data; - }else if(index==size-1){ - return tail.data; - - }else{ - Node temp = head; - for(int i =1;i<=index;i++){ - temp = temp.next; - } - return temp.data; + + Node temp = head; + for(int i =1;i<=index;i++){ + temp = temp.next; } + return temp.data; } - public Object remove(int index) throws Exception{ - if(index>=size){ - throw new Exception("±곬"); + public Object remove(int index){ + if(index<0 || index>=size){ + throw new IndexOutOfBoundsException(); } Object o = null; if(size == 1){ o = head.data; + size--; + return o; + } + if(index==0){ + o = head.data; + Node afterHead = head.next; + head = afterHead; + + }else if(index==size-1){ + Node preTail = getNode(index-1); + Node tail = preTail.next; + o = tail.data; + preTail.next = null; }else{ - if(index==0){ - - Node afterHead = head.next; - afterHead.previous = null; - head = afterHead; - o = head.data; - - }else if(index == size-1){ - Node beforeTail = tail.previous; - beforeTail.next = null; - tail = beforeTail; - o = tail.data; - }else{ - Node curNode = this.getNode(index); - Node pre = curNode.previous; - Node next = curNode.next; - //мڶϿָ - Node temp = new Node(next.data, pre, next.next); - pre.next = temp; - next = temp; - o = curNode.data; - - } + Node preCur = getNode(index-1); + Node cur = preCur.next; + Node nextCur = cur.next; + o = cur.data; + preCur.next = nextCur; + + } + size--; + return o; - } - size--; - return o; + } @@ -114,39 +103,39 @@ public int size(){ } public void addFirst(Object o){ - Node node = new Node(o, null, head); + Node node = new Node(o,null); if(head == null){ head = node; - tail = node; - }else{ - head.previous = node; - head = node; + size++; + return; } + head = new Node(o, head); size++; } public void addLast(Object o){ - //½ڵpreviousָָtail - Node curNode = new Node(o, tail, null); - if(tail==null){ - //ǰΪʱýڵͷβΪýڵ - head = curNode; - tail = curNode; - }else{ - //Ϊʱһڵnextָָ¼Ľڵ㼴 - tail.next = curNode; - //¼ڵΪtail - tail = curNode; - + //½ڵnextָָtail + Node add = new Node(o, null); + if(head==null){ + head = add; + size++; + return; + } + Node curNode = head; + while(curNode.next != null){ + curNode = curNode.next; } - size++; + curNode.next = add; + size++; } - public Object removeFirst() throws Exception{ + + + public Object removeFirst(){ return this.remove(0); } - public Object removeLast() throws Exception{ + public Object removeLast(){ return this.remove(size-1); } @@ -186,7 +175,9 @@ public String toString() { } } - sb.deleteCharAt(sb.lastIndexOf(",")); + if(sb.indexOf(",") != -1){ + sb.deleteCharAt(sb.lastIndexOf(",")); + } sb.append("]"); return sb.toString(); @@ -199,12 +190,10 @@ public String toString() { private static class Node{ private Object data; - private Node previous; private Node next; - public Node(Object data, Node previous, Node next) { + public Node(Object data, Node next) { super(); this.data = data; - this.previous = previous; this.next = next; } @@ -228,9 +217,225 @@ public static void main(String[] args) throws Exception { ll.add(4); System.out.println(ll); - Iterator itr = ll.iterator(); + + ll.reverse(); + System.out.println(ll); + /*System.out.println(ll.get(0)); + System.out.println(ll.get(1)); + System.out.println(ll.get(2));*/ + + LinkedList ll2 = new LinkedList(); + ll2.add(1); +// ll2.add(1); + ll2.add(2); +// ll2.add(3); + ll2.add(3); +// ll2.add(4); + ll2.add(4); + ll2.add(5); +// ll2.removeFirstHalf(); +// ll2.remove(2,3); +// ll2.removeDuplicateValues(); + + System.out.println(ll2); + +// ll2.removeRange(2, 6); +// ll2.remove(3); + System.out.println(ll2); + + LinkedList ll3 = new LinkedList(); + ll3.add(2); + ll3.add(4); + ll2.subtract(ll3); + System.out.println(ll2); + + + + + + } + + + /** + * Ѹ + * Ϊ 3->7->10 , úΪ 10->7->3 + */ + public void reverse(){ + LinkedList temp = new LinkedList(); + for(int i = size - 1;i >= 0; i--){ + temp.add(this.get(i)); + } + System.out.println("---"+temp.toString()+"---"); + //ԭ + this.clear(); + + System.out.println("---"+this.toString()+"---"); + for(int i = 0; i < temp.size();i++){ + Object o = temp.get(i); + this.add(o); + } + + } + + /** + * ɾһǰ벿 + * 磺list = 2->5->7->8 , ɾԺֵΪ 7->8 + * list = 2->5->7->8->10 ,ɾԺֵΪ7,8,10 + + */ + public void removeFirstHalf(){ + if(this.size() == 0){ + return; + } + int temp = this.size(); + for(int i = 1; i <= temp/2; i++){ + this.removeFirst(); + } + + + } + + public void clear(){ + + Iterator itr = this.iterator(); while(itr.hasNext()){ - System.out.println(itr.next()); + this.removeFirst(); + } + this.head = null; + } + + /** + * ӵiԪؿʼ ɾlength Ԫ עi0ʼ + * @param i + * @param length + */ + public void remove(int i, int length){ + + for(int j = 0;j < length; j++){ + this.remove(i); + } + + } + /** + * ٶǰlistBе + * ӵǰȡЩlistBָԪ + * 統ǰ = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * صĽӦ[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + int[] result = new int[list.size()]; + for(int i = 0; i min && countForMin == 0){ + indexMin = i; + countForMin++; + } + if(eleBack < max && countForMax == 0){ + indexMax = this.size()-1-i; + countForMax++; + } + } + + if(indexMin != -1 && indexMax != -1){ + for(int i = indexMin; i <= indexMax; i++){ + this.remove(indexMin); + } + + } + + } + + /** + * 赱ǰͲlistָԪֵУͬһеԪֵͬ + * ҪCԪΪǰlistԪصĽұCеԪֵ + * @param list + */ + public LinkedList intersection( LinkedList list){ + LinkedList result = new LinkedList(); + for(int i = 0; i < this.size(); i++){ + Integer temp1 = (Integer)this.get(i); + for(int j = 0; j < list.size(); j++){ + Integer temp2 = (Integer)list.get(j); + if(temp1 == temp2){ + result.add(temp2); + } + + } } + return result; } }