diff --git a/group22/2622819383/Task1/LinkedList.java b/group22/2622819383/Task1/LinkedList.java index 81c1db409c..57dfdef603 100644 --- a/group22/2622819383/Task1/LinkedList.java +++ b/group22/2622819383/Task1/LinkedList.java @@ -129,8 +129,11 @@ public Node insertAsSucc(Object data) { * 把该链表逆置 * 例如链表为 3->7->10 , 逆置后变为 10->7->3 */ - public void reverse(){ - + public void reverse(){ + int times = theSize; + int index = 0; + while (0 < --times) + add(index++, removeLast()); } /** @@ -139,8 +142,10 @@ public void reverse(){ * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 */ - public void removeFirstHalf(){ - + public void removeFirstHalf(){ + int times = theSize / 2; + while (0 < times--) + removeFirst(); } /** @@ -148,19 +153,46 @@ public void removeFirstHalf(){ * @param i * @param length */ - public void remove(int i, int length){ + public void remove(int i, int length){ + Node head = get(i).pred(); //删除(head, tail)之间元素 删除[i, i + length - 1]之间元素 + Node tail = get(i + length - 1).succ(); + head.succ = tail; + tail.pred = head; + theSize -= length; } /** * 假定当前链表和list均包含已升序排列的整数 * 从当前链表中取出那些list所指定的元素 * 例如当前链表 = 11->101->201->301->401->501->601->701 - * listB = 1->3->4->6 + * list = 1->3->4->6 * 返回的结果应该是[101,301,401,601] * @param list */ - public static int[] getElements(LinkedList list){ - return null; + public int[] getElements(LinkedList list){ + Iterator itSelf = iterator(); + Iterator itList = list.iterator(); + int[] ret = new int[list.size()]; + + int i = 0; //list中元素的值,代表当前列表中要取出元素的秩 + lastI = 0;//上一次取出元素的秩 + moveTimes = 0; + value = itSelf.next(); + index = 0;//要返回的数组中元素的秩 + + while (itList.hasNext()) { + i = itList.next(); + if (theSize <= i) throw new IndexOutOfBoundsException(); + + moveTimes = i - lastI; + while (0 < moveTimes--) + value = itSelf.next(); + + ret[index++] = value; + lastI = i; + } + + return ret; } /** @@ -169,9 +201,37 @@ public static int[] getElements(LinkedList list){ * @param list */ + //返回与e相等的元素的秩;如果查找失败则返回-1 + private int find(Object e) { + Iterator it = iterator(); + int i = -1; //要返回的元素的秩 + Object value = null; + + while (it.hasNext()) { + value = it.next(); + i++; + if (value == e) return i; + if (e < value) return -1; + } - public void subtract(LinkedList list){ - + return -1; + } + + public void subtract(LinkedList list){ + Iterator it = list.iterator(); + Object value = null; + int i = -1; + + while (it.hasNext()) { + value = it.next(); + i = find(value); + + //删去重复元素 + while (0 <= i) { + remove(i); + i = find(value); + } + } } /** @@ -179,7 +239,20 @@ public void subtract(LinkedList list){ * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) */ public void removeDuplicateValues(){ - + Node current = header.succ(); + Node next = current; + int removedNum = 0; + + while ((next = next.succ()) != trailer) { + if (current.data() == next.data()) { + removedNum++; + } else { + current.succ = next; + next.pred = current; + current = next; + } + } + theSize -= removedNum; } /** @@ -188,7 +261,26 @@ public void removeDuplicateValues(){ * @param min * @param max */ + //[low, min]U[max, end] + + public void removeRange(int min, int max){ + //删去(i, j] + int i = 0, j = 0; + Iterator it = iterator(); + while (it.hasNext()) { + Object value = it.next(); + if (value <= min) i++; + if (value < max) j++; + else break; //if(max <= value) break; + } + + Node head = get(i); + Node tail = get(j).succ(); + + head.succ = tail; + tail.pred = head; + theSize -= (j - i); } @@ -197,7 +289,27 @@ public void removeRange(int min, int max){ * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 * @param list */ - public LinkedList intersection( LinkedList list){ - return null; + //交集:属于A且属于B的元素的合集 + public LinkedList intersection(LinkedList list){ + LinkedList ret = new LinkedList(); + Iterator it = iterator(); + Iterator itList = list.iterator(); + Object value1 = null, value2 = null; + + if (it.hasNext() && itList.hasNext()) { + value1 = it.next(); + value2 = itList.next(); + } + + while (value1 != null && value2 != null) { + if (value1 < value2) value1 = it.hasNext() ? it.next() : null; + else if (value2 < value1) value2 = itList.hasNext() ? itList.next() : null; + else { + ret.add(value1); + value1 = it.hasNext() ? it.next() : null; + value2 = itList.hasNext() ? itList.next() : null; + } + } + return ret; } } diff --git "a/group22/2622819383/Task2/litestruts/Struts2\345\256\236\346\210\230.txt" "b/group22/2622819383/Task2/litestruts/Struts2\345\256\236\346\210\230.txt" deleted file mode 100644 index e6997ab49a..0000000000 --- "a/group22/2622819383/Task2/litestruts/Struts2\345\256\236\346\210\230.txt" +++ /dev/null @@ -1,125 +0,0 @@ -Struts2瀹炴垬 -----------------------------chapter1 Struts2锛氱幇浠eb妗嗘灦 -涓銆乄eb搴旂敤绋嬪簭锛氬揩閫熷涔 - HTTP锛氬熀浜庢枃鏈 + 鏃犵姸鎬 - Java Servlet锛氳В鍐冲簳灞傚鎴/鏈嶅姟鍣ㄩ棶棰樸 - 灏咹TTP鍏紑缁橨ava璇█锛氭帴鍙桯TTP璇锋眰瀵硅薄锛屾鏌ユ暟鎹紝鎵ц鍚庡彴閫昏緫锛屽悜瀹㈡埛杩斿洖鍝嶅簲銆 - 楂樼骇鍔熻兘锛氫細璇濇満鍒躲 - 妗嗘灦锛氬叧娉ㄥ簲鐢ㄧ▼搴忕骇鍒殑闂銆 - HTTP瀛楃涓插埌Java鏁版嵁绫诲瀷鐨勮浆鎹紱 - 楠岃瘉鏁版嵁锛氬瓧绗︿覆鍚堟硶锛涜浆鎹㈠悗鐨刯ava绫诲瀷鍚堟硶锛 - 璁块棶涓氬姟閫昏緫锛氱壒瀹氬伐浣滄祦锛 - 璁块棶鏁版嵁灞傦細鐗瑰畾宸ヤ綔娴侊紱 - 鍛堢幇琛ㄧず灞(HTML绛)锛 - 鎻愪緵鍥介檯鍖栧拰鏈湴鍖栨敮鎸併 - -浜屻乄eb搴旂敤绋嬪簭妗嗘灦 - - 妗嗘灦鑷姩鍖栦簡甯歌浠诲姟锛 - 妗嗘灦鎻愪緵浜嗘灦鏋勮В鍐虫柟妗堬紱 - -涓夈丼truts2妗嗘灦 - - 1.Struts2姒傝锛歁VC妯″紡 - Browser鈥斺旇姹傗斺>鎺у埗鍣(FilterDisPatcher)鈥斺旇皟鐢ㄥ姩浣溾斺>妯″瀷鈥斺旈夋嫨缁撴灉鈥斺>瑙嗗浘鈥斺斿憟鐜伴〉闈⑩斺>Browser - //璇锋眰URL鍒板姩浣滅殑鏄犲皠锛氱敱XML閰嶇疆鏂囦欢鎴杍ava娉ㄨВ瀹屾垚 - //鍔ㄤ綔锛氬皢涓氬姟閫昏緫璋冪敤灏佽鍒颁竴涓崟鐙殑宸ヤ綔鍗曞厓 + 鍔ㄤ綔鏄竴涓暟鎹紶杈撶殑鍦烘墍銆 - 2.Struts宸ヤ綔鍘熺悊 - 璋冪敤鍔ㄤ綔鈥斺旀嫤鎴櫒鈥斺>鍔ㄤ綔鈥斺旇皟鐢ㄧ粨鏋溾斺>缁撴灉鈥斺旀嫤鎴櫒鈥斺>鍙戝洖缁撴灉 - | | - OGNL OGNL - | | - __________________________________ - | ActionContext(ThreadLocal) | - |ValueStack銆佽姹傘佷細璇濄... | - 鎷︽埅鍣ㄥ畬鎴愪换鍔★細鏃ュ織銆佹暟鎹獙璇併佺被鍨嬭浆鎹€佹枃浠朵笂浼犵瓑銆 - ValueStack锛氫繚绠¤姹傚垱绔嬬浉鍏崇殑鎵鏈夋暟鎹殑涓涓瓨鍌ㄥ尯鍩熴 - OGNL锛氳闂瓨鍌ㄥ湪涓ぎ瀛樺偍搴揤alueStack涓暟鎹殑宸ュ叿锛堣〃杈惧紡璇█锛夈 - ValueStack涓殑鏁版嵁璺熺潃澶勭悊璇锋眰缁忚繃鎵鏈夐樁娈碉紝璐┛妗嗘灦鐨勬暣涓繃绋嬨傝繖鏄洜涓篤alueStack瀛樺湪鍦ㄤ竴涓嚎绋嬫湰鍦板璞★紙ThreadLocal锛変腑銆 - ActionContext: 鍖呭惈鎵鏈夌殑鏁版嵁锛岃繖浜涙暟鎹瀯鎴愪簡鍔ㄤ綔鎵ц鐨勭幆澧冦 - --------------------------chapter2 鍒濊瘑Struts -涓銆佸0鏄庢ф灦鏋勨斺斿簲鐢ㄧ▼搴忛厤缃細鍦ㄨ緝楂樼骇鍒弿杩板簲鐢ㄧ▼搴忕殑鏋舵瀯缁勪欢 - - 澹版槑閭d簺瀵硅薄浣滀负搴旂敤绋嬪簭鐨勫姩浣(action)銆佺粨鏋(result)銆佷互鍙婃嫤鎴櫒(interceptor) - 澹版槑杩囩▼涓昏鍖呮嫭鍒跺畾鍝釜Java绫诲疄鐜板摢涓帴鍙c - 涓ょ閰嶇疆 - 澹版槑鏋舵瀯鐨勪袱绉嶆柟寮 - 鏅鸿兘榛樿鍊 - -浜屻佺畝鍗曠殑HelloWorld绀轰緥 - - 閮ㄧ讲绀轰緥搴旂敤绋嬪簭 - 鎺㈢储HelloWorld搴旂敤绋嬪簭 - -涓夈佷娇鐢ㄦ敞瑙g殑HelloWorld - --------------------------chapter3 浣跨敤Struts2鍔ㄤ綔 -涓銆丼truts鍔ㄤ綔绠浠 -浜屻佹墦鍖呭姩浣 -涓夈佸疄鐜板姩浣 -鍥涖佸悜瀵硅薄浼犻佹暟鎹 -浜斻佹渚嬬爺绌讹細鏂囦欢涓婁紶 - --------------------------chapter4 浣跨敤鎷︽埅鍣ㄨ拷鍔犲伐浣滄祦 --------------------------chapter5 鏁版嵁杞Щ锛歄GNL鍜岀被鍨嬭浆鎹 --------------------------chapter6 鏋勫缓瑙嗗浘鈥斺旀爣绛 --------------------------chapter7 UI缁勪欢鏍囩 --------------------------chapter8 缁撴灉 --------------------------chapter9 缁ф壙Spring鍜孒ibernate/JPA --------------------------chapter10 鎺㈢储楠岃瘉妗嗘灦 --------------------------chapter11 鐞嗚В鍥介檯鍖 --------------------------chapter12 浣跨敤鎻掍欢鎵╁睍Struts2 --------------------------chapter13 鏈浣冲疄璺 --------------------------chapter14 浠庣粡鍏窼truts杩佺Щ --------------------------chapter15 楂樼骇涓婚 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/group22/2622819383/Task3/LinkedList.java b/group22/2622819383/Task3/LinkedList.java new file mode 100644 index 0000000000..02ef7abc52 --- /dev/null +++ b/group22/2622819383/Task3/LinkedList.java @@ -0,0 +1,181 @@ +public class LinkedList { + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse() { + int times = theSize - 1; //第一个元素自然移动到最后,所以只需进行theSize - 1次操作 + int index = 0; + while (0 < times) { + add(index++, removeLast()); + times--; + } + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf() { + int times = theSize / 2; + while (0 < times--) + removeFirst(); + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length) { + Node head = get(i).pred(); //删除(head, tail)之间元素 删除[i, i + length - 1]之间元素 + Node tail = get(i + length - 1).succ(); + + head.succ = tail; + tail.pred = head; + theSize -= length; + } + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * list = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list) { + Iterator it = iterator(); + int[] ret = new int[list.size()]; + int start = -1; + int value = 0; + int i = 0; //数组ret的索引 + + for (Integer num : list) { + while (start < num && it.hasNext()) { + value = it.next(); + start++; + } + ret[i++] = value; + } + return ret; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + + * @param list + */ + public void subtract(LinkedList list) { + Object current = null; + for (Object e : list) { + Iterator it = iterator(); + while (it.hasNext()) { + current = it.next(); + if (current.compareTo(e) == 0) + it.remove(); + if (current.compareTo(e) > 0) + break; + } + } + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues() { + Node current = header.succ(); + Node next = current; + int removedNum = 0; + + while ((next = next.succ()) != trailer) { + if (current.data() == next.data()) { + removedNum++; + } else { + current.succ = next; + next.pred = current; + current = next; + } + } + theSize -= removedNum; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + + + public void removeRange(int min, int max) { + //双链表删去(p, q)间的节点 + Node p = header; + Node q = null; + int removedNum = 0; //要删去节点的数目 + while ((p = p.succ()) != trailer && (p.data() <= min)) + ; + p = p.prev(); + q = p; + while ((q = q.succ()) != trailer && (q.data() < max)) + removedNum++; + p.succ = q; + q.prev = p; + theSize -= removedNum; + + + + /* + //删去(i, j] + int i = 0, j = 0; + Iterator it = iterator(); + while (it.hasNext()) { + int value = it.next(); + if (value <= min) i++; + if (value < max) j++; + else break; //if(max <= value) break; + } + + Node head = get(i); + Node tail = get(j).succ(); + + head.succ = tail; + tail.pred = head; + theSize -= (j - i); + */ + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + //交集:属于A且属于B的元素的合集 + public LinkedList intersection(LinkedList list) { + LinkedList ret = new LinkedList(); + Iterator it = iterator(); + Iterator itList = list.iterator(); + Object value1 = null, value2 = null; + + if (it.hasNext() && itList.hasNext()) { + value1 = it.next(); + value2 = itList.next(); + } + //用null作为遍历完毕的标志 + //循环结束标志:其中一个LinkedList已经遍历完毕 + while (value1 != null && value2 != null) { + if (value1 < value2) value1 = it.hasNext() ? it.next() : null; + else if (value2 < value1) value2 = itList.hasNext() ? itList.next() : null; + else { + ret.add(value1); + value1 = it.hasNext() ? it.next() : null; + value2 = itList.hasNext() ? itList.next() : null; + } + } + return ret; + } +} diff --git a/group22/2622819383/Task3/download/Connection.java b/group22/2622819383/Task3/download/Connection.java new file mode 100644 index 0000000000..14eb68f232 --- /dev/null +++ b/group22/2622819383/Task3/download/Connection.java @@ -0,0 +1,53 @@ +import java.net.URL; +import java.net.HttpURLConnection; +import java.io.IOException; +import java.io.InputStream; +import java.io.ByteArrayOutputStream; + +public class Connection { + private URL url; + private HttpURLConnection conn; + + //创建一个Connection对象,相应建立了一个HttpURLConnection连接 + public Connection(String url) { + try { + this.url = new URL(url); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void initConnection() { + try { + conn = (HttpURLConnection)url.openConnection(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + //从服务器下载startPos-endPos字节范围的资源内容到一个字节数组 + //Range: 用于客户端到服务器端的请求,可通过该字段指定下载文件的某一段大小,及其单位。 + public byte[] read(int startPos, int endPos) throws IOException { + initConnection(); + conn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + InputStream in = conn.getInputStream(); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + + byte[] buf = new byte[1024]; + int hasRead = 0; + while ((hasRead = in.read(buf)) != -1) { + out.write(buf, 0, hasRead); + } + out.close(); + in.close(); + return out.toByteArray(); + } + + public int getContentLength() { + initConnection(); + return conn.getContentLength(); + } + + public void close() { + } +} \ No newline at end of file diff --git a/group22/2622819383/Task3/download/DownloadThread.java b/group22/2622819383/Task3/download/DownloadThread.java new file mode 100644 index 0000000000..a04911cf7c --- /dev/null +++ b/group22/2622819383/Task3/download/DownloadThread.java @@ -0,0 +1,34 @@ +import java.io.IOException; +import java.net.HttpURLConnection; +import java.io.RandomAccessFile; +import java.util.concurrent.locks.*; +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + String targetURL; + //final ReentrantLock lock = new ReentrantLock(); + + + public DownloadThread(Connection conn, int startPos, int endPos, String targetURL) { + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.targetURL = targetURL; + } + + public void run() { + System.out.println("线程" + getName() + "startPos:" + startPos + "; endPos:" + endPos); + try { + RandomAccessFile raf = new RandomAccessFile(targetURL, "rw"); + byte[] buf = conn.read(startPos, endPos); + raf.seek(startPos); + raf.write(buf); + raf.close(); + } catch (IOException e) { + e.printStackTrace(); + } + System.out.println("线程" + this.getName() + "下载完成."); + } +} diff --git a/group22/2622819383/Task3/download/FileDownloader.java b/group22/2622819383/Task3/download/FileDownloader.java new file mode 100644 index 0000000000..c4d8fa167d --- /dev/null +++ b/group22/2622819383/Task3/download/FileDownloader.java @@ -0,0 +1,66 @@ +import java.util.List; +import java.util.ArrayList; +import java.io.RandomAccessFile; +import java.io.IOException; +public class FileDownloader { + + String url; + + public FileDownloader(String url) { + this.url = url; + } + + public void execute() throws IOException { + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (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方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + conn = new Connection(url); + int length = conn.getContentLength(); + String targetURL = "E:/" + url.substring(url.lastIndexOf("/") + 1); + RandomAccessFile raf = new RandomAccessFile(targetURL, "rw"); + raf.setLength(length); + raf.close(); + + for (int i = 0; i < 3; i++) { + int part = length / 3; + int start = i * part; + int end = (i == 2) ? length - 1 : (i + 1) * part - 1; + DownloadThread t = new DownloadThread(conn, start, end, targetURL); + t.start(); + } + } + + /* + DownloadListener listener; + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + */ + + public static void main(String[] args) { + try { + new FileDownloader("http://images2015.cnblogs.com/blog/610238/201604/610238-20160421154632101-286208268.png").execute(); + } catch (IOException e) { + e.printStackTrace(); + } + } +}