diff --git a/group24/330657387/src/main/week01/data_structure/LinkedList.java b/group24/330657387/src/main/week01/data_structure/LinkedList.java index aafe3654ca..3e4053e1da 100644 --- a/group24/330657387/src/main/week01/data_structure/LinkedList.java +++ b/group24/330657387/src/main/week01/data_structure/LinkedList.java @@ -194,7 +194,7 @@ public void removeFirstHalf() { */ public void remove(int i, int length) { rangeCheck(i); - rangeCheck(i + length - 1); + rangeCheck(i + length - 1);//或者当length超出长度,直接认为删除i后面的所有部分。 if (i == 0) { head = getNode(length); size -= length; @@ -216,8 +216,13 @@ public int[] getElements(LinkedList list) throws Exception { if (list == null) { throw new Exception("传入链表为空?"); } + int[] res = new int[list.size]; for (int i = 0; i < list.size; i++) { + //这个list里的值不一定合法的。可以跳过那些不合法的值。 + if(i > size - 1){ + continue; + } res[i] = Integer.parseInt(get( Integer.parseInt(list.get(i).toString()) - 1).toString()); } @@ -298,6 +303,7 @@ public void removeRange(int min, int max) throws Exception { lastRemove = iter.position - 1; } } + //移动指针的时候,注意不要留下指空的指针。不然相关node会无法被gc if(hasmin && firstRemove == 0){ head = getNode(lastRemove); size -= lastRemove-firstRemove+1; @@ -318,44 +324,28 @@ public void removeRange(int min, int max) throws Exception { * @param list */ public LinkedList intersection(LinkedList list) { - if(0 == list.size){ - return this; - } - if(0 == size){ - return list; + if(0 == list.size || 0 == size){ + return new LinkedList(); } + LinkedList res = new LinkedList(); - Node a = head, b = list.head; - while(null != a && null != b){ - if(a.equals(b)){ - res.add(a.data); - a = a.next; - b = b.next; - continue; + Node node1 = this.head; + Node node2 = list.head; + while(node1 != null && node2 != null){ + if((int)node1.data<(int)node2.data){ + node1 = node1.next; + }else if((int)node1.data>(int)node2.data){ + node2 = node2.next; + }else{ + res.add(node1.data); + node1 = node1.next; + node2 = node2.next; } - if(Integer.parseInt(a.data.toString()) > Integer.parseInt(b.data.toString())){ - res.add(b.data); - b = b.next; - continue; - } - if(Integer.parseInt(a.data.toString()) < Integer.parseInt(b.data.toString())){ - res.add(a.data); - a = a.next; - continue; - } - } - while(null != a){ - res.add(a.data); - a = a.next; - } - while(null != b){ - res.add(b.data); - b = b.next; } return res; } - public String ToString() { + public String toString() { LinkedListIterator iter = this.iterator(); StringBuilder sb = new StringBuilder(); while (iter.hasNext()) { diff --git a/group24/330657387/src/main/week01/data_structure/LinkedListTest.java b/group24/330657387/src/main/week01/data_structure/LinkedListTest.java index 671cc20cd2..c0aa471f79 100644 --- a/group24/330657387/src/main/week01/data_structure/LinkedListTest.java +++ b/group24/330657387/src/main/week01/data_structure/LinkedListTest.java @@ -71,14 +71,9 @@ public void testIterator() { public void testReverse() { LinkedList l = lists[2]; l.reverse(); - LinkedListIterator iter = l.iterator(); - StringBuilder sb = new StringBuilder(); - while (iter.hasNext()) { - sb.append(iter.next()); - } // assertEquals("", sb.toString()); - // assertEquals("A", sb.toString()); - assertEquals("EDCBA", sb.toString()); + // assertEquals("A", l.toString()); + assertEquals("E->D->C->B->A->null", l.toString()); } @@ -105,7 +100,7 @@ public void testRemoveByIndex() { try{ LinkedList l = lists[2]; l.remove(0, 1); - System.out.println(l.ToString()); + System.out.println(l.toString()); }catch(Exception e){ assertEquals(IndexOutOfBoundsException.class, e.getClass()); } @@ -155,7 +150,7 @@ public void testSubtract() { l.add(66); try{ list.subtract(l); - System.out.println(list.ToString()); + System.out.println(list.toString()); }catch(Exception e){ assertEquals(e.getMessage(), "传入链表为空?"); } @@ -171,7 +166,7 @@ public void testRemoveDuplicateValues() { list.removeDuplicateValues(); - System.out.println(list.ToString()); + System.out.println(list.toString()); } @Test @@ -185,11 +180,11 @@ public void testRemoveRange() throws Exception { list.add(77); list.add(88); list.add(99); - System.out.println(list.ToString()); + System.out.println(list.toString()); try{ list.removeRange(50, 80); - System.out.println(list.ToString()); + System.out.println(list.toString()); }catch(Exception e){ assertEquals(e.getMessage(), "输入有问题!"); } @@ -208,11 +203,11 @@ public void testIntersection() { // list.add(99); LinkedList l = new LinkedList(); - l.add(10); -// l.add(30); -// l.add(40); -// l.add(60); + l.add(11); + l.add(33); +// l.add(44); +// l.add(66); - System.out.println(list.intersection(l).ToString()); + System.out.println(list.intersection(l).toString()); } } diff --git a/group24/330657387/src/main/week03/download/DownloadThread.java b/group24/330657387/src/main/week03/download/DownloadThread.java index f18bbf234c..37602d1c25 100644 --- a/group24/330657387/src/main/week03/download/DownloadThread.java +++ b/group24/330657387/src/main/week03/download/DownloadThread.java @@ -1,5 +1,6 @@ package main.week03.download; +import java.io.File; import java.io.RandomAccessFile; import java.util.concurrent.CyclicBarrier; @@ -33,7 +34,7 @@ public void run() { + "]"); byte[] data = conn.read(startPos, endPos); - //设置文件的读取权限 + //设置文件的读取权限,每个线程都独立有这个实例,这样,多线程读写同一文件就没问题。 RandomAccessFile file = new RandomAccessFile(localFile, "rw"); file.seek(startPos); @@ -47,9 +48,9 @@ public void run() { barrier.await(); // 等待别的线程完成 } catch (Exception e) { + //如果线程出错了,无法await,怎么处理? e.printStackTrace(); - - } + } finally{}//这块里应该写close的 } } diff --git a/group24/330657387/src/main/week03/download/FileDownloader.java b/group24/330657387/src/main/week03/download/FileDownloader.java index 54d6c260ad..165dc4dfb2 100644 --- a/group24/330657387/src/main/week03/download/FileDownloader.java +++ b/group24/330657387/src/main/week03/download/FileDownloader.java @@ -58,7 +58,7 @@ public void run() { int length = conn.getContentLength(); - //确保文件里有足够的空间? + //确保文件里有足够的空间,就先创建空文件。 createPlaceHolderFile(this.filePath, length); //每个线程的读取区间 diff --git a/group24/330657387/src/main/week03/download/impl/ConnectionImpl.java b/group24/330657387/src/main/week03/download/impl/ConnectionImpl.java index e725ff15a1..e42087d663 100644 --- a/group24/330657387/src/main/week03/download/impl/ConnectionImpl.java +++ b/group24/330657387/src/main/week03/download/impl/ConnectionImpl.java @@ -12,7 +12,8 @@ import main.week03.download.api.Connection; import main.week03.download.api.ConnectionException; -public class ConnectionImpl implements Connection { +//包级可见,是保护措施 +class ConnectionImpl implements Connection { URL url; static final int BUFFER_SIZE = 1024; @@ -29,13 +30,15 @@ public ConnectionImpl(String _url) throws ConnectionException { @Override public byte[] read(int startPos, int endPos) throws IOException { int totalLen = endPos - startPos + 1; - + //是URLConnection的子类,负责http协议的链接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); - conn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); - InputStream inputStream = conn.getInputStream(); + //客户端可以在请求里放置参数,设置接收数据区间 + //代替了is.skip(),但是is.skip里有read,所以是边读边移动下标的,和本程序意图相违背。 + conn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + byte[] buffer = new byte[BUFFER_SIZE]; //输出流 diff --git a/group24/330657387/src/main/week03/download/impl/ConnectionManagerImpl.java b/group24/330657387/src/main/week03/download/impl/ConnectionManagerImpl.java index 2253f4fa06..47d5dd22e1 100644 --- a/group24/330657387/src/main/week03/download/impl/ConnectionManagerImpl.java +++ b/group24/330657387/src/main/week03/download/impl/ConnectionManagerImpl.java @@ -4,6 +4,7 @@ import main.week03.download.api.ConnectionException; import main.week03.download.api.ConnectionManager; +//返回接口,是对实现的一种隐蔽 public class ConnectionManagerImpl implements ConnectionManager { @Override diff --git a/group24/330657387/src/main/week03/download/test.jpg b/group24/330657387/src/main/week03/download/test.jpg index 3a052212e5..a959a6ad20 100644 Binary files a/group24/330657387/src/main/week03/download/test.jpg and b/group24/330657387/src/main/week03/download/test.jpg differ