diff --git a/group10/595128841/src/main/java/org/le/b/ArrayUtil.java b/group10/595128841/src/main/java/org/le/b/ArrayUtil.java new file mode 100644 index 0000000000..29f1b3567a --- /dev/null +++ b/group10/595128841/src/main/java/org/le/b/ArrayUtil.java @@ -0,0 +1,197 @@ +/** + * + */ +package org.le.b; + +import java.util.Arrays; + +/** + * @author yue + * @time 2017年2月28日 + */ +public class ArrayUtil { + + /** + * @param args + */ + public static void main(String[] args) { + //int[] origin = {7, 9 , 30, 3}; + //int[] n = reverseArray(origin); +// int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; +// int[] n = removeZero(oldArr); +// int[] a1 = {3, 5, 7,8,9,12}; +// int[] a2 = {4, 5, 6,7,8,12,14,5}; +// int[] n =merge(a1, a2); +// int[] n = grow(a1,5); + int[] n = fibonacci(100); + System.out.println(Arrays.toString(n)); + } + + /** + * 给定一个整形数组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 int[] reverseArray(int[] origin){ + int[] newArry = new int[origin.length]; + int i= 0,j = origin.length; + while(j > 0){ + newArry[i++] = origin[--j]; + } + return newArry; + } + + /** + * 现在有如下的一个数组: 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[] tempArray = new int[oldArray.length]; + int j = 0; + for(int i = 0; i < oldArray.length;i++){ + if(oldArray[i] > 0){ + tempArray[j++] = oldArray[i]; + } + } + int[] newArray = new int[j]; + System.arraycopy(tempArray, 0, newArray, 0, j); + 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 static int[] merge(int[] array1, int[] array2){ + int len = array1.length; + int[] newArray = new int[array1.length+array2.length]; + System.arraycopy(array1, 0, newArray, 0, array1.length); + for(int i = 0; i < array2.length; i++){ + boolean flag = true; + for(int j = 0; j < array1.length; j++){ + if(array2[i] == array1[j]){ + flag = false; + } + } + if(flag){ + newArray[len++] = array2[i]; + } + } + int[] aa = new int[len]; + System.arraycopy(newArray, 0, aa, 0, len); + for(int i = 0; i < aa.length; i++){ + for(int j = i+1; j < aa.length; j++){ + if(aa[i] > aa[j]){ + int temp = aa[i]; + aa[i] = aa[j]; + aa[j] = temp; + } + } + } + return aa; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int [] oldArray, int size){ + int[] aa = new int[oldArray.length+size]; + System.arraycopy(oldArray, 0, aa, 0, oldArray.length); + return aa; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * Fn = F(n-1)+F(n-2)(n >= 2) + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public static int[] fibonacci(int max){ + if(max <2){ + return new int[0]; + } + int[] temp = new int[8]; + int count = 0; + for(int i = 1;i < max;i ++){ + int len = createFibo(i); + if(len > max) + break; + temp = growInt(temp,count); + temp[count++] = len; + } + int[] res = new int[count]; + System.arraycopy(temp, 0, res, 0, count); + return res; + } + + private static int[] growInt(int[] temp,int count){ + int[] n = temp; + if(count >= temp.length){ + n = new int[temp.length + (temp.length >> 1)]; + System.arraycopy(temp, 0, n, 0, temp.length); + } + return n; + } + + private static int createFibo(int n){ + if(n <= 2){ + return 1; + } + return createFibo(n-1)+createFibo(n-2); + } + + /** + * 返回小于给定最大值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){ + StringBuilder str = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + str.append(seperator).append(array[i]); + } + return str.substring(1).toString(); + } + +} diff --git a/group10/595128841/src/main/java/org/le/b/LoginAction.java b/group10/595128841/src/main/java/org/le/b/LoginAction.java new file mode 100644 index 0000000000..d895f6b5e6 --- /dev/null +++ b/group10/595128841/src/main/java/org/le/b/LoginAction.java @@ -0,0 +1,35 @@ +package org.le.b; + +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/group10/595128841/src/main/java/org/le/b/Struts.java b/group10/595128841/src/main/java/org/le/b/Struts.java new file mode 100644 index 0000000000..303286690f --- /dev/null +++ b/group10/595128841/src/main/java/org/le/b/Struts.java @@ -0,0 +1,155 @@ +package org.le.b; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.DocumentFactory; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +public class Struts { + private static Map actionMap; + + 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字段中。 + + */ + if(actionMap == null){ + actionMap = strutsXmlSax("struts2.xml"); + } + ActionBean actionBean = actionMap.get(actionName); + return processAction(actionBean,parameters); + } + + + private static View processAction(ActionBean actionBean, Map parameters) { + String clazzStr = actionBean.getClazz(); + Map result = actionBean.getResults(); + try { + Class clazz= Class.forName(clazzStr); + Object obj = clazz.newInstance(); + for(String key : parameters.keySet()){ + String name = "set"+(key.charAt(0)+"").toUpperCase()+key.substring(1); + Method method = clazz.getMethod(name, String.class); + method.invoke(obj, parameters.get(key)); + } + Method execute = clazz.getMethod("execute"); + String resultStr = (String)execute.invoke(obj); + String resultJsp = result.get(resultStr); + Map pramMap = new HashMap<>(); + Method meg = clazz.getMethod("getMessage"); + String resultMsg = (String)meg.invoke(obj); + pramMap.put("message", resultMsg); + return new View(resultJsp,pramMap); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } + return null; + } + + + public static Map strutsXmlSax(String path){ + DocumentFactory documentFactory = DocumentFactory.getInstance(); + SAXReader saxReader = new SAXReader(documentFactory); + Document doc = null; + try { + doc = saxReader.read(path); + } catch (DocumentException e) { + e.printStackTrace(); + } + Element rootElement = doc.getRootElement(); + Element pack = rootElement.element("package"); + List actions = pack.elements("action"); + Map actionMap = new HashMap<>(); + for(Element action : actions){ + Attribute name = action.attribute("name"); + Attribute clazz = action.attribute("class"); + List results = action.elements("result"); + Map resMap = new HashMap<>(); + for(Element result : results){ + String key = "success"; + String value = result.getTextTrim(); + Attribute rname = result.attribute("name"); + if(rname != null){ + key = rname.getValue(); + } + resMap.put(key, value); + } + actionMap.put(name.getValue(), new ActionBean(name.getValue(),clazz.getValue(),resMap)); + } + return actionMap; + } + + public static class ActionBean{ + private String name; + private String clazz; + private Map results; + + public ActionBean(String name, String clazz, Map results) { + this.name = name; + this.clazz = clazz; + this.results = results; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getClazz() { + return clazz; + } + + public void setClazz(String clazz) { + this.clazz = clazz; + } + + public Map getResults() { + return results; + } + + public void setResults(Map results) { + this.results = results; + } + + } + +} diff --git a/group10/595128841/src/main/java/org/le/b/StrutsTest.java b/group10/595128841/src/main/java/org/le/b/StrutsTest.java new file mode 100644 index 0000000000..3733a6a777 --- /dev/null +++ b/group10/595128841/src/main/java/org/le/b/StrutsTest.java @@ -0,0 +1,38 @@ +package org.le.b; + +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/group10/595128841/src/main/java/org/le/b/View.java b/group10/595128841/src/main/java/org/le/b/View.java new file mode 100644 index 0000000000..39a9f8a644 --- /dev/null +++ b/group10/595128841/src/main/java/org/le/b/View.java @@ -0,0 +1,28 @@ +package org.le.b; + +import java.util.Map; + +public class View { + + private String jsp; + private Map parameters; + + public View(String resultJsp, Map pramMap) { + this.jsp = resultJsp; + this.parameters = pramMap; + } + 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/group10/595128841/src/main/java/org/le/c/Downloader.java b/group10/595128841/src/main/java/org/le/c/Downloader.java new file mode 100644 index 0000000000..811bb711a4 --- /dev/null +++ b/group10/595128841/src/main/java/org/le/c/Downloader.java @@ -0,0 +1,120 @@ +/** + * + */ +package org.le.c; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.io.RandomAccessFile; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; + +/** + * 多线程图片下载 + * @author yue + * @time 2017年3月11日 + */ +public class Downloader { + + private RandomAccessFile raf; + private String url; + + public Downloader(String url,String path){ + //获取文件名 + String filePath = path+File.separator+url.substring(url.lastIndexOf("/")+1); + System.out.println("保存路径:"+filePath); + this.url = url; + try { + this.raf = new RandomAccessFile(filePath,"rw"); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + + public void downFile() throws IOException{ + URL uurl = new URL(url); + URLConnection conn = uurl.openConnection(); + int cLen = conn.getContentLength();//最大可表示4G + if(cLen < 0){ + System.out.println("无法获取文件大小"); + return; + } + this.raf.setLength(cLen); + //根据文件大小选择合适线程 + int size = getByteArrayLength(cLen); + byte[] buff = new byte[size]; + System.out.println("下载文件:"+url+",文件大小:"+cLen+"字节,单个线程大小:"+size); + int len = 0; + int offset = 0; + int index = 0; + try (InputStream in = conn.getInputStream()){ + while((len = in.read(buff)) != -1){ + byte[] desc = getNewByte(buff,len); + Thread thread = new DownLoadThread(desc,offset,this); + offset += len; + thread.setName("线程"+(++index)); + thread.start(); + } + } catch (MalformedURLException e) { + e.printStackTrace(); + } + } + + private byte[] getNewByte(byte[] src,int len){ + byte[] desc = new byte[len]; + System.arraycopy(src, 0, desc, 0, len); + return desc; + } + + private int getByteArrayLength(int cLen) { + int m = 1024 * 1024; + int s = cLen/m; + if(s == 0){ + return 1024 *100; + } + return m; + } + + public synchronized void writeToFile(byte[] buff,int offset) { + try { + raf.seek(offset); + raf.write(buff); + } catch (IOException e) { + e.printStackTrace(); + } + } + + + public static void main(String[] args) throws Exception { + String url = "http://7xq43s.com1.z0.glb.clouddn.com/yunanding-6.jpg"; + String path = "C:/work/workspace/coding2017/coding2017/group10/595128841"; + Downloader d = new Downloader(url,path); + long st = System.currentTimeMillis(); + d.downFile(); + System.out.println("耗时:"+(System.currentTimeMillis() - st)+" 毫秒"); + } + + static class DownLoadThread extends Thread{ + private byte[] buff; + private int offset; + private Downloader downloader; + + public DownLoadThread(byte[] buff, int offset,Downloader downloader) { + this.buff = buff; + this.downloader = downloader; + this.offset = offset; + } + + @Override + public void run() { + System.out.println(Thread.currentThread().getName()+",length:"+buff.length+",offset:"+offset); + downloader.writeToFile(buff,offset); + } + + } + + +} diff --git a/group10/595128841/src/main/java/org/le/c/ImgFilter.java b/group10/595128841/src/main/java/org/le/c/ImgFilter.java new file mode 100644 index 0000000000..2ce1767d32 --- /dev/null +++ b/group10/595128841/src/main/java/org/le/c/ImgFilter.java @@ -0,0 +1,25 @@ +/** + * + */ +package org.le.c; + +import java.io.File; +import java.io.FilenameFilter; + +/** + * 图片过滤器 + * @author yue + * @time 2017年3月12日 + */ +public class ImgFilter implements FilenameFilter { + + /** + * + */ + @Override + public boolean accept(File dir, String name) { + String s1 = name.toLowerCase(); + return s1.endsWith(".jpg") || s1.endsWith(".png"); + } + +} diff --git a/group10/595128841/src/main/java/org/le/list/ArrayList.java b/group10/595128841/src/main/java/org/le/list/ArrayList.java new file mode 100644 index 0000000000..03a74e139b --- /dev/null +++ b/group10/595128841/src/main/java/org/le/list/ArrayList.java @@ -0,0 +1,144 @@ +/** + * + */ +package org.le.list; + +/** + * @author yue + * @time 2017年2月19日 + */ +public class ArrayList implements List { + + private Object[] elementData; + + private int size; + + public ArrayList(int initCapcity){ + if(initCapcity < 0){ + throw new IllegalArgumentException("initCapcity 必须大于0"); + } + elementData = new Object[initCapcity]; + } + + public ArrayList(){ + elementData = new Object[10]; + } + + @Override + public void add(Object obj) { + grow(size + 1); + elementData[size++] = obj; + } + + @Override + public void add(int index, Object obj) { + rangeCheckForAdd(index); + grow(size + 1); + System.arraycopy(elementData, index, elementData, index+1, size - index); + elementData[index] = obj; + size ++; + } + + @Override + public void remove(Object obj) { + if(obj == null){ + for (int i = 0; i < size; i++) { + if(elementData[i] == null){ + fastRemove(i); + } + } + }else{ + for (int i = 0; i < size; i++) { + if(obj.equals(elementData[i])){ + fastRemove(i); + } + } + } + } + + @Override + public E remove(int index) { + rangeCheck(index); + int movedNum = size - index - 1; + E oldElement = elementData(index); + System.arraycopy(elementData, index+1, elementData, index, movedNum); + elementData[--size] = null; + return oldElement; + } + + @Override + public E get(int index) { + rangeCheck(index); + return elementData(index); + } + + @Override + public E set(int index, E obj) { + rangeCheck(index); + E oldElement = elementData(index); + elementData[index] = obj; + return oldElement; + } + + @Override + public int indexOf(E obj) { + if(obj == null){ + for (int i = 0; i < size; i++) { + if(elementData[i] == null){ + return i; + } + } + }else{ + for (int i = 0; i < size; i++) { + if(obj.equals(elementData[i])){ + return i; + } + } + } + return -1; + } + + /** + * 数组扩容 + * @param minCapacity + */ + private void grow(int minCapacity) { + if(minCapacity <= elementData.length){ + return; + } + int oldCapacity = elementData.length; + int newCapacity = minCapacity + (oldCapacity >> 1); + if(newCapacity < minCapacity){ + newCapacity = minCapacity; + } + if(minCapacity > Integer.MAX_VALUE){ + newCapacity = Integer.MAX_VALUE; + } + Object[] newArray = new Object[newCapacity]; + System.arraycopy(elementData, 0, newArray, 0, newCapacity); + elementData = newArray; + } + + @SuppressWarnings("unchecked") + private E elementData(int index){ + return (E) elementData[index]; + } + + private void fastRemove(int i) { + int numMoved = size - i -1; + if(numMoved > 0){ + System.arraycopy(elementData, i+1, elementData, i, numMoved); + } + elementData[-- size] = null; + } + + private void rangeCheck(int index){ + if(index >= size || index <0) + throw new IndexOutOfBoundsException("index:"+index+",size:"+size); + } + + private void rangeCheckForAdd(int index){ + if(index > size || index <0) + throw new IndexOutOfBoundsException("index:"+index+",size:"+size); + } +} diff --git a/group10/595128841/src/main/java/org/le/list/LinkedList.java b/group10/595128841/src/main/java/org/le/list/LinkedList.java new file mode 100644 index 0000000000..b4ee384ad6 --- /dev/null +++ b/group10/595128841/src/main/java/org/le/list/LinkedList.java @@ -0,0 +1,299 @@ +/** + * + */ +package org.le.list; + +import java.util.NoSuchElementException; + +/** + * @author yue + * @time 2017年2月19日 + */ +public class LinkedList implements List { + + private int size = 0; + + private Node first; + + private Node last; + + private static class Node{ + E item; + Node prev; + Node next; + Node(Node prev,E item, Node next) { + super(); + this.item = item; + this.prev = prev; + this.next = next; + } + } + + public LinkedList(){ + + } + + /** + * 头部插入 + */ + private void linkFirst(E e){ + final Node f = first; + final Node newNode = new Node(null,e,f); + first = newNode; + if(f == null) + last = newNode; + else + f.prev = newNode; + size ++; + } + + /** + * 尾部插入 + */ + private void linkLast(E e){ + final Node l = last; + final Node newNode = new Node<>(l,e,null); + last = newNode; + if(last == null) + first = newNode; + else + l.next = newNode; + size ++; + } + + /** + * 某个不为null元素之前插入 + */ + private void linkBefore(E e,Node succ){ + final Node pred = succ.prev; + final Node newNode = new Node<>(pred,e,succ); + succ.prev = newNode; + if(pred == null) + first = newNode; + else + pred.next = newNode; + size ++; + } + + /** + * 删除头部元素 + */ + private E unlinkFirst(Node f){ + final E element = f.item; + final Node next = f.next; + f.item = null; + f.next = null; + first = next; + if(next == null) + last = null; + else + next.prev = null; + size -- ; + return element; + } + /** + * 删除尾部元素 + * @param l + * @return + */ + private E unlinkLast(Node l){ + final E element = l.item; + final Node prev = l.prev; + l.item = null; + l.prev = null; + last = prev; + if(prev == null) + first = null; + else + prev.next = null; + size -- ; + return element; + } + + /** + * 删除指定节点 + * @param e + * @return + */ + private E unlink(Node e){ + final Node prev = e.prev; + final E element = e.item; + final Node next = e.next; + + if(prev == null){ + first = next; + }else{ + prev.next = next; + e.prev = null; + } + + if(next == null){ + last = prev; + }else{ + next.prev = prev; + e.next = null; + } + e.item = null; + size -- ; + return element; + } + + /** + * 该方法默认在尾部添加 + */ + @Override + public void add(E e) { + linkLast(e); + } + + /** + * + */ + @Override + public void add(int index, E e) { + checkPositionIndex(index); + if(index == size){ + linkLast(e); + }else{ + linkBefore(e, node(index)); + } + } + + private Node node(int index) { + //小于容量一半 + if(index < (size >> 1)){ + Node x = first; + for(int i = 0; i < index; i++){ + x = x.next; + } + return x; + }else{ + Node x = last; + for(int i = size - 1; i > index; i --){ + x = x.prev; + } + return x; + } + } + + private void checkPositionIndex(int index){ + if(index <0 || index > size){ + throw new IndexOutOfBoundsException("索引越界:index:"+index+",size:"+size); + } + } + + private void checkElementIndex(int index){ + if(index <0 || index >= size){ + throw new IndexOutOfBoundsException("索引越界:index:"+index+",size:"+size); + } + } + + /** + * + */ + @Override + public void remove(E obj) { + if(obj == null){ + for(Node x = first;x != null; x = x.next){ + if(x.item == null){ + unlink(x); + } + } + }else{ + for(Node x = first;x != null;x = x.next){ + if(obj.equals(x.item)){ + unlink(x); + } + } + } + } + + /** + * + */ + @Override + public E remove(int index) { + checkElementIndex(index); + return unlink(node(index)); + } + + /** + * + */ + @Override + public E get(int index) { + checkElementIndex(index); + return node(index).item; + } + + /** + * + */ + @Override + public E set(int index, E obj) { + checkElementIndex(index); + Node x = node(index); + E oldVal = x.item; + x.item = obj; + return oldVal; + } + + /** + * + */ + @Override + public int indexOf(E obj) { + int index = 0; + if(obj == null){ + for(Node x = first;x != null;x = x.next){ + if(x.item == null) + return index; + index ++; + } + }else{ + for(Node x = first; x != null; x = x.next){ + if(obj.equals(x.item)) + return index; + index ++; + } + } + return -1; + } + /** + * 弹出栈顶的元素,不删除元素 + * @param e + * @return + */ + public E peek(){ + final Node e = first; + return e == null ? null : e.item; + } + + /** + * 弹出栈顶元素,删除元素 + * @return + */ + public E poll(){ + final Node e = first; + return (e == null) ? null : unlinkFirst(e); + } + /** + * 入栈,栈顶 + * @param e + */ + public void push(E e){ + linkFirst(e); + } + + /** + * 出栈,删除并返回栈顶元素 + * @return + */ + public E pop(){ + final Node f = first; + if(f == null) + throw new NoSuchElementException(); + return unlinkFirst(f); + } + +} diff --git a/group10/595128841/src/main/java/org/le/list/List.java b/group10/595128841/src/main/java/org/le/list/List.java new file mode 100644 index 0000000000..3e50bd5553 --- /dev/null +++ b/group10/595128841/src/main/java/org/le/list/List.java @@ -0,0 +1,19 @@ +package org.le.list; + +public interface List { + + void add(E obj); + + void add(int index,E obj); + + void remove(E obj); + + E remove(int index); + + E get(int index); + + E set(int index,E obj); + + int indexOf(E obj); + +} diff --git a/group10/904627477/.settings/org.eclipse.core.resources.prefs b/group10/904627477/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..4824b80263 --- /dev/null +++ b/group10/904627477/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/group10/904627477/src/com/coding/LinkedList.java b/group10/904627477/src/com/coding/LinkedList.java deleted file mode 100644 index f878f75820..0000000000 --- a/group10/904627477/src/com/coding/LinkedList.java +++ /dev/null @@ -1,175 +0,0 @@ -package com.coding; - -import java.util.NoSuchElementException; - -public class LinkedList implements List { - - private Node head; - - public void add(Object o){ - addLast(o); - } - - public void add(int index , Object o){ - Node node = new Node(); - node.data = o; - if(index==0){ - addFirst(o); - return ; - } - Node before = getNode(index-1); - Node next = before.next; - before.next = node; - node.next = next; - } - - private Node getLastNode(){ - Node temp = head; - if(head!=null){ - while(true){ - if(temp.next!=null){ - temp = temp.next; - }else{ - break; - } - } - }else{ - throw new NoSuchElementException(); - } - return temp; - } - - private Node getNode(int index){ - if(index<0){ - throw new IndexOutOfBoundsException(); - } - int i = 0; - Node temp = head; - while(true){ - if(temp==null){ - throw new IndexOutOfBoundsException(); - } - if(i==index){ - break; - }else{ - i++; - temp = temp.next; - } - } - return temp; - } - - public Object get(int index){ - Node node = getNode(index); - return node.data; - } - - public Object remove(int index){ - if(index==0){ - removeFirst(); - } - Node before = getNode(index-1); - Node temp = getNode(index); - before.next = temp.next; - return temp.data; - } - - public int size(){ - int size = 0; - Node temp = head; - while(true){ - if(temp==null){ - break; - }else{ - size++; - temp = temp.next; - } - } - return size; - } - - public void addFirst(Object o){ - Node node = new Node(); - node.data = o; - node.next = head; - head = node; - } - - public void addLast(Object o){ - Node node = new Node(); - node.data = o; - if(head==null){ - head = node; - return; - } - Node last = getLastNode(); - last.next = node; - } - public Object removeFirst(){ - if(head == null){ - throw new NoSuchElementException(); - } - Object obj = head.data; - head = head.next; - return obj; - } - public Object removeLast(){ - if(head == null){ - throw new NoSuchElementException(); - } - if(head.next == null){ - return removeFirst(); - } - Node before = head; - Node temp = head.next; - while(true){ - if(temp.next==null){ - break; - }else{ - before = temp; - temp = temp.next; - } - } - before.next = null; - return temp.data; - } - - public Iterator iterator(){ - return new LinkedIterator(); - } - - - private static class Node{ - Object data; - Node next; - } - - private class LinkedIterator implements Iterator{ - - private Node node; - - public LinkedIterator(){ - node = head; - } - - @Override - public boolean hasNext() { - if(node!=null){ - return true; - } - return false; - } - - @Override - public Object next() { - if(node==null){ - throw new NoSuchElementException(); - }else{ - Object obj = node.data; - node = node.next; - return obj; - } - } - - } -} diff --git a/group10/904627477/src/com/coding/array/ArrayUtil.java b/group10/904627477/src/com/coding/array/ArrayUtil.java index 37f230812a..aaa1dbb96a 100644 --- a/group10/904627477/src/com/coding/array/ArrayUtil.java +++ b/group10/904627477/src/com/coding/array/ArrayUtil.java @@ -10,6 +10,9 @@ public class ArrayUtil { * @return */ public void reverseArray(int[] origin){ + if(origin==null){ + return ; + } int len = origin.length; for (int i = 0; i < len/2 ; i++) { int temp = origin[len-1-i]; @@ -27,6 +30,9 @@ public void reverseArray(int[] origin){ */ public int[] removeZero(int[] oldArray){ + if(oldArray==null){ + return new int[0]; + } int[] tempArr = new int[oldArray.length]; int size = 0; for (int i = 0; i < oldArray.length; i++) { @@ -49,6 +55,11 @@ public int[] removeZero(int[] oldArray){ */ public int[] merge(int[] array1, int[] array2){ + if(array1==null&&array2==null){ + return new int[0]; + }else if(array1==null||array2==null){ + return array1==null?array2:array1; + } int[] arr3 = new int[array1.length+array2.length]; int len1 = array1.length; int len2 = array2.length; @@ -85,6 +96,9 @@ public int[] merge(int[] array1, int[] array2){ * @return */ public int[] grow(int [] oldArray, int size){ + if(oldArray==null){ + return new int[0]; + } if(size<0){ throw new IllegalArgumentException(); } @@ -95,6 +109,20 @@ public int[] grow(int [] oldArray, int size){ return newArr; } + public static byte[] grow(byte[] oldArray, int size){ + if(oldArray==null){ + return new byte[0]; + } + if(size<0){ + throw new IllegalArgumentException(); + } + byte[] newArr = new byte[oldArray.length + size]; + for (int i = 0; i < oldArray.length; i++) { + newArr[i] = oldArray[i]; + } + return newArr; + } + /** * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] @@ -202,13 +230,28 @@ public boolean isPerfectNumber(int n){ * @param s * @return */ - public String join(int[] array, String seperator){ + /*My + public String join(int[] array, String seperator){ String result = ""; for (int i = 0; i < array.length; i++) { result = result + array[i] + seperator; } int index = result.lastIndexOf(seperator); return result.substring(0, index); + }*/ + + public String join(int[] array, String seperator){ + if(array==null){ + return ""; + } + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < array.length; i++) { + sb.append(array[i]); + if(i7->10 , 逆置后变为 10->7->3 + */ + public void reverse() { + if (head == null || head.next == null) { + return; + } + Node temp = head.next; + Node newHead = this.head; + newHead.next = null; + while (temp != null) { + Node next = temp.next; + temp.next = newHead; + newHead = temp; + temp = next; + } + this.head = newHead; + } + + /** + * 删除一个单链表的前半部分 例如:list = 2->5->7->8 , 删除以后的值为 7->8 如果list = 2->5->7->8->10 + * ,删除以后的值为7,8,10 + */ + public void removeFirstHalf() { + if (head == null || head.next == null) { + return; + } + int len = size(); + for (int i = 0; i < len / 2; i++) { + head = head.next; + } + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * + * @param i + * @param length + */ + public void remove(int i, int length) { + if (i < 0 || i >= size()) { + throw new IndexOutOfBoundsException(); + } + if(head==null){ + return; + } + Node ni = head; + Node temp = head; + for (int j = 0; j < i + length; j++) { + if (temp == null) { + break; + } + if (j + 1 == i) { + ni = temp; + } + temp = temp.next; + } + ni.next = temp; + if (i == 0) { + head = temp; + } + } + + /** + * 假定当前链表和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) { + if(list==null||head==null||list.head==null){ + return new int[0]; + } + int[] arr = new int[0]; + Node tempA = head; + Node tempB = list.head; + int index=0; + while(tempA!=null&&tempB!=null){ + int len = (int)tempB.data - index; + int i = 0; + for(i=0;i=max){ + return ; + } + Node temp = new Node(); + temp.next = head; + while(temp.next!=null){ + if(temp.next.data==null){ + temp = temp.next; + continue; + } + int d = (int) temp.next.data; + if(d>min&&d=max){ + break; + } + } + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * + * @param list + */ + public LinkedList intersection(LinkedList list) { + LinkedList link = new LinkedList(); + if(list==null||head==null||list.head==null){ + return link; + } + Node tempA = new Node(); + tempA.next = head; + Node tempB = new Node(); + tempB.next = list.head; + while(tempA.next!=null&&tempB.next!=null){ + if(tempA.next.data==null){ + tempA = tempA.next; + continue; + } + if(tempB.next.data==null){ + tempB = tempB.next; + continue; + } + int a = (int)tempA.next.data; + int b = (int)tempB.next.data; + if(ab){ + tempB = tempB.next; + }else{ + link.add(tempA.next.data); + tempA = tempA.next; + tempB = tempB.next; + } + } + return link; + } +} diff --git a/group10/904627477/src/com/coding/List.java b/group10/904627477/src/com/coding/basic/List.java similarity index 83% rename from group10/904627477/src/com/coding/List.java rename to group10/904627477/src/com/coding/basic/List.java index 5e78eced11..396b1f6416 100644 --- a/group10/904627477/src/com/coding/List.java +++ b/group10/904627477/src/com/coding/basic/List.java @@ -1,4 +1,4 @@ -package com.coding; +package com.coding.basic; public interface List { public void add(Object o); diff --git a/group10/904627477/src/com/coding/Queue.java b/group10/904627477/src/com/coding/basic/Queue.java similarity index 88% rename from group10/904627477/src/com/coding/Queue.java rename to group10/904627477/src/com/coding/basic/Queue.java index 8fd01bef96..2b2f92f74b 100644 --- a/group10/904627477/src/com/coding/Queue.java +++ b/group10/904627477/src/com/coding/basic/Queue.java @@ -1,4 +1,4 @@ -package com.coding; +package com.coding.basic; public class Queue { diff --git a/group10/904627477/src/com/coding/Stack.java b/group10/904627477/src/com/coding/basic/Stack.java similarity index 90% rename from group10/904627477/src/com/coding/Stack.java rename to group10/904627477/src/com/coding/basic/Stack.java index 3f8b319607..083215e615 100644 --- a/group10/904627477/src/com/coding/Stack.java +++ b/group10/904627477/src/com/coding/basic/Stack.java @@ -1,4 +1,4 @@ -package com.coding; +package com.coding.basic; import java.util.EmptyStackException; diff --git a/group10/904627477/src/com/coding/download/CreateThread.java b/group10/904627477/src/com/coding/download/CreateThread.java new file mode 100644 index 0000000000..d9a22edbbd --- /dev/null +++ b/group10/904627477/src/com/coding/download/CreateThread.java @@ -0,0 +1,32 @@ +package com.coding.download; + + +import com.coding.download.api.Resource; + +public class CreateThread extends Thread { + + private Resource resource; + private int length; + + public CreateThread(Resource resource,int length){ + this.resource = resource; + this.length = length; + } + + @Override + public void run() { + int startPos = 0; + while(true){ + //System.out.println(startPos); + if(startPos>=length){ + resource.setFlag(true); + break; + }else{ + startPos = resource.increace(); + } + } + } + + + +} diff --git a/group10/904627477/src/com/coding/download/DownloadThread.java b/group10/904627477/src/com/coding/download/DownloadThread.java new file mode 100644 index 0000000000..7e98539050 --- /dev/null +++ b/group10/904627477/src/com/coding/download/DownloadThread.java @@ -0,0 +1,36 @@ +package com.coding.download; + +import java.io.File; +import java.io.IOException; +import java.io.RandomAccessFile; + +import com.coding.download.api.Connection; +import com.coding.util.IOUtils; + +public class DownloadThread extends Thread { + + Connection conn; + int startPos; + int endPos; + private File file; + + public DownloadThread(Connection conn, int startPos, int endPos,File file) { + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.file = file; + } + + public void run() { + byte[] buff; + try { + buff = conn.read(startPos, endPos); + if(buff!=null&&buff.length!=0){ + IOUtils.writeFile(file, startPos, buff); + } + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/group10/904627477/src/com/coding/download/FileDownloader.java b/group10/904627477/src/com/coding/download/FileDownloader.java new file mode 100644 index 0000000000..146491f2db --- /dev/null +++ b/group10/904627477/src/com/coding/download/FileDownloader.java @@ -0,0 +1,102 @@ +package com.coding.download; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.ArrayList; +import java.util.List; + +import com.coding.download.api.Connection; +import com.coding.download.api.ConnectionException; +import com.coding.download.api.ConnectionManager; +import com.coding.download.api.DownloadListener; +import com.coding.download.api.Resource; +import com.coding.util.IOUtils; + + + + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + private static String localFile = "c:/test/test.jpg"; + + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + /**/ + try { + Connection conn = cm.open(url); + int length = conn.getContentLength(); + File file = new File(localFile); + if(!file.exists()){ + IOUtils.createFile(length, localFile); + } + Resource res = new Resource(url,file); + Thread c = new CreateThread(res,length); + Thread r = new RemoveThread(res,listener); + c.start(); + r.start(); + } catch (ConnectionException e) { + e.printStackTrace(); + } + /*Connection conn = null; + try { + conn = cm.open(this.url); + int length = conn.getContentLength(); + File file = new File(localFile); + if(!file.exists()){ + IOUtils.createFile(length, localFile); + } + + + + new DownloadThread(cm.open(this.url),0,length-1,file).start(); + + } catch (ConnectionException e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + conn.close(); + } + }*/ + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} diff --git a/group10/904627477/src/com/coding/download/FileDownloaderTest.java b/group10/904627477/src/com/coding/download/FileDownloaderTest.java new file mode 100644 index 0000000000..f3345d75d7 --- /dev/null +++ b/group10/904627477/src/com/coding/download/FileDownloaderTest.java @@ -0,0 +1,59 @@ +package com.coding.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coding.download.api.ConnectionManager; +import com.coding.download.api.DownloadListener; +import com.coding.download.impl.ConnectionManagerImpl; + + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "https://img6.bdstatic.com/img/image/smallpic/22.jpg"; + + FileDownloader downloader = new FileDownloader(url); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} diff --git a/group10/904627477/src/com/coding/download/RemoveThread.java b/group10/904627477/src/com/coding/download/RemoveThread.java new file mode 100644 index 0000000000..0a1b5e415f --- /dev/null +++ b/group10/904627477/src/com/coding/download/RemoveThread.java @@ -0,0 +1,30 @@ +package com.coding.download; + +import com.coding.download.api.DownloadListener; +import com.coding.download.api.Resource; + +public class RemoveThread extends Thread { + + private Resource resource; + private DownloadListener listener; + + public RemoveThread(Resource resource,DownloadListener listener){ + this.resource = resource; + this.listener = listener; + } + + @Override + public void run() { + while(true){ + if(resource.isFlag()&&resource.getThreads().size()==0){ + listener.notifyFinished(); + break; + }else{ + resource.decreace(); + } + } + } + + + +} diff --git a/group10/904627477/src/com/coding/download/api/Connection.java b/group10/904627477/src/com/coding/download/api/Connection.java new file mode 100644 index 0000000000..65f3dae9c5 --- /dev/null +++ b/group10/904627477/src/com/coding/download/api/Connection.java @@ -0,0 +1,23 @@ +package com.coding.download.api; + +import java.io.IOException; + +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/group10/904627477/src/com/coding/download/api/ConnectionException.java b/group10/904627477/src/com/coding/download/api/ConnectionException.java new file mode 100644 index 0000000000..67f59470a3 --- /dev/null +++ b/group10/904627477/src/com/coding/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coding.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group10/904627477/src/com/coding/download/api/ConnectionManager.java b/group10/904627477/src/com/coding/download/api/ConnectionManager.java new file mode 100644 index 0000000000..df00b84b77 --- /dev/null +++ b/group10/904627477/src/com/coding/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coding.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group10/904627477/src/com/coding/download/api/DownloadListener.java b/group10/904627477/src/com/coding/download/api/DownloadListener.java new file mode 100644 index 0000000000..bc01938d90 --- /dev/null +++ b/group10/904627477/src/com/coding/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coding.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group10/904627477/src/com/coding/download/api/Resource.java b/group10/904627477/src/com/coding/download/api/Resource.java new file mode 100644 index 0000000000..ae525fd9b5 --- /dev/null +++ b/group10/904627477/src/com/coding/download/api/Resource.java @@ -0,0 +1,92 @@ +package com.coding.download.api; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +import com.coding.download.DownloadThread; +import com.coding.download.impl.ConnectionManagerImpl; + +public class Resource { + + private int index; + private int size; + private String url; + private List threads; + private File file; + private boolean flag; + + public Resource(String url,File file){ + index = 0; + size = 1024*2; + threads = new ArrayList(); + flag = false; + this.url = url; + this.file = file; + } + + public synchronized int increace(){ + //System.out.println(threads.size()); + while(threads.size()>=5){ + try { + this.wait(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + int startPos = index*size; + try { + Connection conn = new ConnectionManagerImpl().open(url); + DownloadThread down = new DownloadThread(conn, startPos, startPos+size-1,file); + down.start(); + index ++; + threads.add(down); + this.notify(); + } catch (ConnectionException e) { + e.printStackTrace(); + } + return index*size; + } + + public synchronized void decreace(){ + //System.out.println("decreace:"+threads.size()); + while(threads.size()==0){ + try { + this.wait(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + /*try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + }*/ + for (int i = 0; i < threads.size();i++) { + if(!threads.get(i).isAlive()){ + threads.remove(i); + this.notify(); + break; + } + } + } + + public List getThreads() { + return threads; + } + + public void setThreads(List threads) { + this.threads = threads; + } + + public boolean isFlag() { + return flag; + } + + public void setFlag(boolean flag) { + this.flag = flag; + } + + + +} diff --git a/group10/904627477/src/com/coding/download/impl/ConnectionImpl.java b/group10/904627477/src/com/coding/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..6f9ef0d6ae --- /dev/null +++ b/group10/904627477/src/com/coding/download/impl/ConnectionImpl.java @@ -0,0 +1,47 @@ +package com.coding.download.impl; + +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; + +import com.coding.array.ArrayUtil; +import com.coding.download.api.Connection; + + +public class ConnectionImpl implements Connection{ + + private URLConnection ucon; + + public ConnectionImpl(String url) throws IOException{ + URL u = new URL(url); + ucon = u.openConnection(); + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + ucon.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + InputStream in = ucon.getInputStream(); + byte[] result = new byte[0]; + byte[] buff = new byte[1024]; + int len = 0; + while((len=in.read(buff))!=-1){ + int rLen = result.length; + result =ArrayUtil.grow(result, len); + System.arraycopy(buff, 0, result, rLen, len); + } + return result; + } + + @Override + public int getContentLength() { + return ucon.getContentLength(); + } + + @Override + public void close() { + //ucon = null; + } + +} diff --git a/group10/904627477/src/com/coding/download/impl/ConnectionManagerImpl.java b/group10/904627477/src/com/coding/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..f949ddce6f --- /dev/null +++ b/group10/904627477/src/com/coding/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,22 @@ +package com.coding.download.impl; + + +import java.io.IOException; + +import com.coding.download.api.Connection; +import com.coding.download.api.ConnectionException; +import com.coding.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException{ + try { + return new ConnectionImpl(url); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + +} diff --git a/group10/904627477/src/com/coding/test/ArrayListTest.java b/group10/904627477/src/com/coding/test/ArrayListTest.java index d65a692351..fc711a315a 100644 --- a/group10/904627477/src/com/coding/test/ArrayListTest.java +++ b/group10/904627477/src/com/coding/test/ArrayListTest.java @@ -6,8 +6,8 @@ import org.junit.Before; import org.junit.Test; -import com.coding.ArrayList; -import com.coding.Iterator; +import com.coding.basic.ArrayList; +import com.coding.basic.Iterator; public class ArrayListTest { diff --git a/group10/904627477/src/com/coding/test/LinkedListTest.java b/group10/904627477/src/com/coding/test/LinkedListTest.java index af48a6fb0b..81d8632cfe 100644 --- a/group10/904627477/src/com/coding/test/LinkedListTest.java +++ b/group10/904627477/src/com/coding/test/LinkedListTest.java @@ -6,8 +6,8 @@ import org.junit.Before; import org.junit.Test; -import com.coding.Iterator; -import com.coding.LinkedList; +import com.coding.basic.Iterator; +import com.coding.basic.LinkedList; public class LinkedListTest { @@ -114,5 +114,152 @@ public void testIterator() { assertEquals("333", it.next()); assertEquals(false, it.hasNext()); } + + @Test + public void testReverse(){ + link.reverse(); + assertEquals(3, link.size()); + assertEquals("333", link.get(0)); + assertEquals("222", link.get(1)); + assertEquals("111", link.get(2)); + } + + @Test + public void testRemoveFirstHalf(){ + link.add("444"); + link.add("555"); + link.removeFirstHalf(); + assertEquals(3, link.size()); + assertEquals("333", link.get(0)); + link.add("666"); + link.removeFirstHalf(); + assertEquals(2, link.size()); + assertEquals("555", link.get(0)); + } + + @Test + public void testRemoveForIndex(){ + link.add("444"); + link.add("555"); + link.remove(1, 2); + assertEquals(3, link.size()); + assertEquals("444", link.get(1)); + + link.remove(2, 5); + assertEquals(2, link.size()); + + link.remove(0,1); + assertEquals(1, link.size()); + assertEquals("444", link.get(0)); + } + + @Test + //11->101->201->301->401->501->601->701 listB = 1->3->4->6 [101,301,401,601] + public void testGetElements(){ + link = new LinkedList(); + link.add(11); + link.add(101); + link.add(201); + link.add(301); + link.add(401); + link.add(501); + link.add(601); + link.add(701); + LinkedList linkB = new LinkedList(); + linkB.add(1); + linkB.add(3); + linkB.add(4); + linkB.add(6); + int[] eles = link.getElements(linkB); + assertEquals(4, eles.length); + assertEquals(101, eles[0]); + assertEquals(301, eles[1]); + assertEquals(401, eles[2]); + assertEquals(601, eles[3]); + } + + @Test + public void testSubtract(){ + link = new LinkedList(); + link.add(11); + link.add(101); + link.add(201); + link.add(301); + link.add(401); + link.add(501); + link.add(601); + link.add(701); + LinkedList linkB = new LinkedList(); + linkB.add(401); + linkB.add(201); + linkB.add(601); + link.subtract(linkB); + assertEquals(5, link.size()); + assertEquals(11, link.get(0)); + assertEquals(101, link.get(1)); + assertEquals(301, link.get(2)); + assertEquals(501, link.get(3)); + assertEquals(701, link.get(4)); + } + @Test + public void testRemoveDuplicateValues(){ + link = new LinkedList(); + link.add(11); + link.add(101); + link.add(101); + link.add(301); + link.add(401); + link.add(401); + link.removeDuplicateValues(); + assertEquals(4, link.size()); + assertEquals(11, link.get(0)); + assertEquals(101, link.get(1)); + assertEquals(301, link.get(2)); + assertEquals(401, link.get(3)); + } + + @Test + public void testRemoveRange(){ + link = new LinkedList(); + link.add(11); + link.add(101); + link.add(201); + link.add(301); + link.add(401); + link.add(501); + link.add(601); + link.add(701); + link.removeRange(200, 600); + assertEquals(4, link.size()); + assertEquals(11, link.get(0)); + assertEquals(101, link.get(1)); + assertEquals(601, link.get(2)); + assertEquals(701, link.get(3)); + } + + @Test + public void testIntersection(){ + link = new LinkedList(); + link.add(11); + link.add(101); + link.add(201); + link.add(301); + link.add(401); + link.add(501); + link.add(601); + link.add(701); + LinkedList linkB = new LinkedList(); + linkB.add(201); + linkB.add(305); + linkB.add(401); + linkB.add(504); + linkB.add(601); + LinkedList linkC = link.intersection(linkB); + assertEquals(3, linkC.size()); + assertEquals(201, linkC.get(0)); + assertEquals(401, linkC.get(1)); + assertEquals(601, linkC.get(2)); + } + } diff --git a/group10/904627477/src/com/coding/test/QueueTest.java b/group10/904627477/src/com/coding/test/QueueTest.java index 779120c0fb..5434522f14 100644 --- a/group10/904627477/src/com/coding/test/QueueTest.java +++ b/group10/904627477/src/com/coding/test/QueueTest.java @@ -6,7 +6,8 @@ import org.junit.Before; import org.junit.Test; -import com.coding.Queue; +import com.coding.basic.Queue; + public class QueueTest { diff --git a/group10/904627477/src/com/coding/test/StackTest.java b/group10/904627477/src/com/coding/test/StackTest.java index 38de36c9d9..ec96c6adcb 100644 --- a/group10/904627477/src/com/coding/test/StackTest.java +++ b/group10/904627477/src/com/coding/test/StackTest.java @@ -6,7 +6,8 @@ import org.junit.Before; import org.junit.Test; -import com.coding.Stack; +import com.coding.basic.Stack; + public class StackTest { diff --git a/group10/904627477/src/com/coding/test/Test.java b/group10/904627477/src/com/coding/test/Test.java index c8024a8c97..806eb3a1c5 100644 --- a/group10/904627477/src/com/coding/test/Test.java +++ b/group10/904627477/src/com/coding/test/Test.java @@ -1,29 +1,57 @@ package com.coding.test; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.RandomAccessFile; +import java.net.URL; +import java.net.URLConnection; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import com.coding.basic.LinkedList; +import com.coding.download.api.Connection; +import com.coding.download.api.ConnectionException; +import com.coding.download.impl.ConnectionManagerImpl; + public class Test { - public static void main(String[] args) { - /*int[] origin = {7, 9 , 30, 3}; - ArrayUtil au = new ArrayUtil(); - au.reverseArray(origin); - for (int i : origin) { - System.out.println(i); + public static void main(String[] args) throws IOException, ConnectionException { + /*URLConnection con = new URL("https://img6.bdstatic.com/img/image/smallpic/22.jpg").openConnection(); + InputStream in = con.getInputStream(); + System.out.println("---------------"); + + con.setRequestProperty("Range", "bytes=" + 1 + "-" + 2);*/ + /*URLConnection con = new URL("https://img6.bdstatic.com/img/image/smallpic/22.jpg").openConnection(); + con.getContentLength(); + con.connect(); + //con.setRequestProperty("Range", "bytes=" + 0 + "-" + 27573); + con.addRequestProperty("Range", "bytes=" + 0 + "-" + 27573); + System.out.println(con.getContentLength()); + InputStream in = con.getInputStream(); + int length = con.getContentLength(); + File file = new File("c:/test/test.jpg"); + //RandomAccessFile out = new RandomAccessFile(file, "rw"); + FileOutputStream out = new FileOutputStream(file); + byte[] buff = new byte[length]; + int len = 0; + while((len=in.read(buff))!=-1){ + out.write(buff, 0, len); } - */ -/* int a = 5; - System.out.println(5/2); - int[] a1 = new int[0]; - System.out.println(a1.length); - System.out.println(Math.sqrt(2));*/ -// int[] a2 = new ArrayUtil().getPerfectNumbers(100); -// for (int i = 0; i < a2.length; i++) { -// System.out.println(a2[i]); -// } - System.out.println(Test.class.getResource("").getPath()); + out.close();*/ + /**/Connection con = new ConnectionManagerImpl().open("https://img6.bdstatic.com/img/image/smallpic/22.jpg"); + byte[] buff = con.read(0, 27573); + File file = new File("c:/test/test.jpg"); + FileOutputStream out = new FileOutputStream(file); + out.write(buff); + out.close(); } } diff --git a/group10/904627477/src/com/coding/util/IOUtils.java b/group10/904627477/src/com/coding/util/IOUtils.java new file mode 100644 index 0000000000..d016d55603 --- /dev/null +++ b/group10/904627477/src/com/coding/util/IOUtils.java @@ -0,0 +1,40 @@ +package com.coding.util; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.RandomAccessFile; + +public class IOUtils { + + public static void writeFile(File file,int startPos,byte[] buff) throws IOException{ + if(buff==null){ + return; + } + RandomAccessFile out = new RandomAccessFile(file, "rw"); + out.seek(startPos); + out.write(buff); + out.close(); + } + + public static void createFile(long length,String filePath){ + RandomAccessFile file = null; + try { + file = new RandomAccessFile(filePath, "rw"); + file.setLength(length); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally{ + if(file!=null){ + try { + file.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + +}