Skip to content

Commit 287ab7d

Browse files
committed
小傅哥,文章代码更新。晚上好!
面经手册 · 第7篇《ArrayList也这么多知识?一个指定位置插入就把谢飞机面晕了!》
1 parent 3d7b3d4 commit 287ab7d

File tree

7 files changed

+235
-1
lines changed

7 files changed

+235
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
/interview.iml
22
/interview-04/interview-04.iml
3+
/interview-07/interview-07.iml
4+
/interview-05/interview-05.iml

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414
4. [面经手册 · 第3篇《HashMap核心知识,扰动函数、负载因子、扩容链表拆分,深度学习》](https://bugstack.cn/interview/2020/08/07/%E9%9D%A2%E7%BB%8F%E6%89%8B%E5%86%8C-%E7%AC%AC3%E7%AF%87-HashMap%E6%A0%B8%E5%BF%83%E7%9F%A5%E8%AF%86-%E6%89%B0%E5%8A%A8%E5%87%BD%E6%95%B0-%E8%B4%9F%E8%BD%BD%E5%9B%A0%E5%AD%90-%E6%89%A9%E5%AE%B9%E9%93%BE%E8%A1%A8%E6%8B%86%E5%88%86-%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0.html)
1515
5. [面经手册 · 第4篇《HashMap数据插入、查找、删除、遍历,源码分析》](https://bugstack.cn/interview/2020/08/13/%E9%9D%A2%E7%BB%8F%E6%89%8B%E5%86%8C-%E7%AC%AC4%E7%AF%87-HashMap%E6%95%B0%E6%8D%AE%E6%8F%92%E5%85%A5-%E6%9F%A5%E6%89%BE-%E5%88%A0%E9%99%A4-%E9%81%8D%E5%8E%86-%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90.html)
1616
6. [面经手册 · 第5篇《看图说话,讲解2-3平衡树「红黑树的前身」》](https://bugstack.cn/interview/2020/08/16/%E9%9D%A2%E7%BB%8F%E6%89%8B%E5%86%8C-%E7%AC%AC5%E7%AF%87-%E7%9C%8B%E5%9B%BE%E8%AF%B4%E8%AF%9D-%E8%AE%B2%E8%A7%A32-3%E5%B9%B3%E8%A1%A1%E6%A0%91-%E7%BA%A2%E9%BB%91%E6%A0%91%E7%9A%84%E5%89%8D%E8%BA%AB.html)
17-
7 [面经手册 · 第6篇《带着面试题学习红黑树操作原理,解析什么时候染色、怎么进行旋转、与2-3树有什么关联》](https://bugstack.cn/interview/2020/08/20/%E9%9D%A2%E7%BB%8F%E6%89%8B%E5%86%8C-%E7%AC%AC6%E7%AF%87-%E5%B8%A6%E7%9D%80%E9%9D%A2%E8%AF%95%E9%A2%98%E5%AD%A6%E4%B9%A0%E7%BA%A2%E9%BB%91%E6%A0%91%E6%93%8D%E4%BD%9C%E5%8E%9F%E7%90%86-%E8%A7%A3%E6%9E%90%E4%BB%80%E4%B9%88%E6%97%B6%E5%80%99%E6%9F%93%E8%89%B2-%E6%80%8E%E4%B9%88%E8%BF%9B%E8%A1%8C%E6%97%8B%E8%BD%AC-%E4%B8%8E2-3%E6%A0%91%E6%9C%89%E4%BB%80%E4%B9%88%E5%85%B3%E8%81%94.html)
17+
7. [面经手册 · 第6篇《带着面试题学习红黑树操作原理,解析什么时候染色、怎么进行旋转、与2-3树有什么关联》](https://bugstack.cn/interview/2020/08/20/%E9%9D%A2%E7%BB%8F%E6%89%8B%E5%86%8C-%E7%AC%AC6%E7%AF%87-%E5%B8%A6%E7%9D%80%E9%9D%A2%E8%AF%95%E9%A2%98%E5%AD%A6%E4%B9%A0%E7%BA%A2%E9%BB%91%E6%A0%91%E6%93%8D%E4%BD%9C%E5%8E%9F%E7%90%86-%E8%A7%A3%E6%9E%90%E4%BB%80%E4%B9%88%E6%97%B6%E5%80%99%E6%9F%93%E8%89%B2-%E6%80%8E%E4%B9%88%E8%BF%9B%E8%A1%8C%E6%97%8B%E8%BD%AC-%E4%B8%8E2-3%E6%A0%91%E6%9C%89%E4%BB%80%E4%B9%88%E5%85%B3%E8%81%94.html)
18+
8. [面经手册 · 第7篇《ArrayList也这么多知识?一个指定位置插入就把谢飞机面晕了!》](https://bugstack.cn/interview/2020/08/27/%E9%9D%A2%E7%BB%8F%E6%89%8B%E5%86%8C-%E7%AC%AC7%E7%AF%87-ArrayList%E4%B9%9F%E8%BF%99%E4%B9%88%E5%A4%9A%E7%9F%A5%E8%AF%86-%E4%B8%80%E4%B8%AA%E6%8C%87%E5%AE%9A%E4%BD%8D%E7%BD%AE%E6%8F%92%E5%85%A5%E5%B0%B1%E6%8A%8A%E8%B0%A2%E9%A3%9E%E6%9C%BA%E9%9D%A2%E6%99%95%E4%BA%86.html)

interview-03/interview-03.iml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@
1212
<orderEntry type="inheritedJdk" />
1313
<orderEntry type="sourceFolder" forTests="false" />
1414
<orderEntry type="library" name="Maven: com.alibaba:fastjson:1.2.73" level="project" />
15+
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.13" level="project" />
16+
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
1517
</component>
1618
</module>

interview-05/src/main/java/T.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获!
3+
* 公众号:bugstack虫洞栈
4+
* Create by 小傅哥(fustack) @2020
5+
*/
6+
public class T {
7+
}

interview-07/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>interview</artifactId>
7+
<groupId>org.itstack</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>interview-07</artifactId>
13+
14+
15+
</project>
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
package org.itstack.interview.test;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import org.junit.Test;
5+
import sun.net.idn.Punycode;
6+
7+
import javax.sound.midi.Soundbank;
8+
import java.lang.reflect.Array;
9+
import java.util.*;
10+
11+
public class ApiTest {
12+
13+
public static void main(String[] args) {
14+
List<String> list = new ArrayList<String>(Collections.nCopies(9, "a"));
15+
System.out.println("初始化:" + list);
16+
17+
list.add(2, "b");
18+
System.out.println("插入后:" + list);
19+
}
20+
21+
@Test
22+
public void t2() {
23+
String[] strs = new String[10];
24+
strs[5] = "1";
25+
26+
Arrays.asList();
27+
}
28+
29+
@Test
30+
public void test_remove() {
31+
ArrayList<String> list = new ArrayList<String>();
32+
list.add("a");
33+
list.add("b");
34+
list.add("c");
35+
list.add("d");
36+
list.add("e");
37+
list.add("f");
38+
list.add("g");
39+
list.add("h");
40+
list.add("i");
41+
list.add("j");
42+
System.out.println(list);
43+
44+
list.remove(2);
45+
46+
System.out.println(list);
47+
System.out.println(list.size());
48+
}
49+
50+
@Test
51+
public void test_arraycopy() {
52+
int[] oldArr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
53+
int[] newArr = new int[oldArr.length + (oldArr.length >> 1)];
54+
55+
System.arraycopy(oldArr, 0, newArr, 0, oldArr.length);
56+
57+
newArr[11] = 11;
58+
newArr[12] = 12;
59+
newArr[13] = 13;
60+
newArr[14] = 14;
61+
62+
System.out.println("数组元素:" + JSON.toJSONString(newArr));
63+
System.out.println("数组长度:" + newArr.length);
64+
65+
/**
66+
* 测试结果
67+
*
68+
* 数组元素:[1,2,3,4,5,6,7,8,9,10,0,11,12,13,14]
69+
* 数组长度:15
70+
*/
71+
}
72+
73+
@Test
74+
public void test_copy_remove() {
75+
int[] oldArr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
76+
77+
int index = 2;
78+
int numMoved = 10 - index - 1;
79+
80+
System.arraycopy(oldArr, index + 1, oldArr, index, numMoved);
81+
82+
System.out.println("数组元素:" + JSON.toJSONString(oldArr));
83+
}
84+
85+
@Test
86+
public void init_01() {
87+
ArrayList<String> list = new ArrayList<String>();
88+
list.add("aaa");
89+
list.add("bbb");
90+
list.add("ccc");
91+
}
92+
93+
@Test
94+
public void init_02() {
95+
96+
List<String> list = Arrays.asList("aaa", "bbb", "ccc");
97+
list.add("ddd");
98+
99+
ArrayList<String> obj = new ArrayList<String>(Arrays.asList("aaa", "bbb", "ccc"));
100+
101+
}
102+
103+
@Test
104+
public void init_03() {
105+
ArrayList<String> list = new ArrayList<String>() {{
106+
add("aaa");
107+
add("bbb");
108+
add("ccc");
109+
}};
110+
}
111+
112+
@Test
113+
public void init_04() {
114+
ArrayList<Integer> list = new ArrayList<Integer>(Collections.nCopies(10, 0));
115+
}
116+
117+
@Test
118+
public void t() {
119+
List<Integer> list1 = Arrays.asList(1, 2, 3);
120+
System.out.println("通过数组转换:" + (list1.toArray().getClass() == Object[].class));
121+
122+
ArrayList<Integer> list2 = new ArrayList<Integer>(Arrays.asList(1, 2, 3));
123+
System.out.println("通过集合转换:" + (list2.toArray().getClass() == Object[].class));
124+
}
125+
126+
127+
@Test
128+
public void t_hashIdx() {
129+
130+
List<String> list = new ArrayList<String>(Collections.<String>nCopies(8, "0"));
131+
132+
list.set("a".hashCode() & 8 - 1, "a");
133+
list.set("b".hashCode() & 8 - 1, "b");
134+
list.set("c".hashCode() & 8 - 1, "c");
135+
list.set("d".hashCode() & 8 - 1, "d");
136+
list.set("e".hashCode() & 8 - 1, "e");
137+
list.set("f".hashCode() & 8 - 1, "f");
138+
list.set("g".hashCode() & 8 - 1, "g");
139+
140+
System.out.println("元素集合:" + list);
141+
142+
System.out.println("获取元素f [\"f\".hashCode() & 8 - 1)] Idx:" + ("f".hashCode() & (8 - 1)) + " 元素:" + list.get("f".hashCode() & 8 - 1));
143+
System.out.println("获取元素e [\"e\".hashCode() & 8 - 1)] Idx:" + ("e".hashCode() & (8 - 1)) + " 元素:" + list.get("e".hashCode() & 8 - 1));
144+
System.out.println("获取元素d [\"d\".hashCode() & 8 - 1)] Idx:" + ("d".hashCode() & (8 - 1)) + " 元素:" + list.get("d".hashCode() & 8 - 1));
145+
}
146+
147+
@Test
148+
public void t_ensureCapacity01(){
149+
final int N = 10000000;
150+
ArrayList<Integer> list = new ArrayList<Integer>();
151+
long startTime = System.currentTimeMillis();
152+
for (int i = 0; i < N; i++) {
153+
list.add(i);
154+
}
155+
System.out.println("默认情况耗时:"+(System.currentTimeMillis() - startTime));
156+
}
157+
158+
@Test
159+
public void t_ensureCapacity02(){
160+
final int N = 10000000;
161+
ArrayList<Integer> list = new ArrayList<Integer>(N);
162+
long startTime = System.currentTimeMillis();
163+
for (int i = 0; i < N; i++) {
164+
list.add(i);
165+
}
166+
System.out.println("初始定长容量耗时:"+(System.currentTimeMillis() - startTime));
167+
}
168+
169+
@Test
170+
public void t_ensureCapacity03(){
171+
final int N = 10000000;
172+
ArrayList<Object> list = new ArrayList<Object>();
173+
long startTime = System.currentTimeMillis();
174+
list.ensureCapacity(N);
175+
for (int i = 0; i < N; i++) {
176+
list.add(i);
177+
}
178+
System.out.println("使用ensureCapacity方法耗时:"+(System.currentTimeMillis() - startTime));
179+
}
180+
181+
@Test
182+
public void tt(){
183+
ArrayList<Object> list = new ArrayList<Object>();
184+
final int N = 10000000;
185+
for (int i = 0; i < N; i++) {
186+
list.add(i);
187+
}
188+
189+
ArrayList<Object> list2 = new ArrayList<Object>();
190+
long startTime1 = System.currentTimeMillis();
191+
// list2.ensureCapacity(N);
192+
for (int i = 0; i < N; i++) {
193+
list2.add(i);
194+
}
195+
long endTime1 = System.currentTimeMillis();
196+
System.out.println("使用ensureCapacity方法后:"+(endTime1 - startTime1));
197+
}
198+
199+
}

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<module>interview-03</module>
1515
<module>interview-04</module>
1616
<module>interview-05</module>
17+
<module>interview-07</module>
1718
</modules>
1819

1920
<dependencies>
@@ -22,6 +23,13 @@
2223
<artifactId>fastjson</artifactId>
2324
<version>1.2.73</version>
2425
</dependency>
26+
<!-- https://mvnrepository.com/artifact/junit/junit -->
27+
<dependency>
28+
<groupId>junit</groupId>
29+
<artifactId>junit</artifactId>
30+
<version>4.13</version>
31+
<scope>test</scope>
32+
</dependency>
2533
</dependencies>
2634

2735
</project>

0 commit comments

Comments
 (0)