|
| 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 | +} |
0 commit comments