Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 22 additions & 32 deletions group24/330657387/src/main/week01/data_structure/LinkedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
}
Expand Down Expand Up @@ -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;
Expand All @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

}

Expand All @@ -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());
}
Expand Down Expand Up @@ -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(), "传入链表为空?");
}
Expand All @@ -171,7 +166,7 @@ public void testRemoveDuplicateValues() {

list.removeDuplicateValues();

System.out.println(list.ToString());
System.out.println(list.toString());
}

@Test
Expand All @@ -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(), "输入有问题!");
}
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package main.week03.download;

import java.io.File;
import java.io.RandomAccessFile;
import java.util.concurrent.CyclicBarrier;

Expand Down Expand Up @@ -33,7 +34,7 @@ public void run() {
+ "]");

byte[] data = conn.read(startPos, endPos);
//设置文件的读取权限
//设置文件的读取权限,每个线程都独立有这个实例,这样,多线程读写同一文件就没问题。
RandomAccessFile file = new RandomAccessFile(localFile, "rw");

file.seek(startPos);
Expand All @@ -47,9 +48,9 @@ public void run() {
barrier.await(); // 等待别的线程完成

} catch (Exception e) {
//如果线程出错了,无法await,怎么处理?
e.printStackTrace();

}
} finally{}//这块里应该写close的

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void run() {

int length = conn.getContentLength();

//确保文件里有足够的空间
//确保文件里有足够的空间,就先创建空文件。
createPlaceHolderFile(this.filePath, length);

//每个线程的读取区间
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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];

//输出流
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import main.week03.download.api.ConnectionException;
import main.week03.download.api.ConnectionManager;

//返回接口,是对实现的一种隐蔽
public class ConnectionManagerImpl implements ConnectionManager {

@Override
Expand Down
Binary file modified group24/330657387/src/main/week03/download/test.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.