|
| 1 | +package com.bruce.homework0226; |
| 2 | + |
| 3 | +import com.bruce.utils.MyException; |
| 4 | +import junit.framework.TestCase; |
| 5 | +import org.junit.Test; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.Random; |
| 8 | + |
| 9 | +/** |
| 10 | + * Created by Bruce.Jiao on 17-2-23. |
| 11 | + */ |
| 12 | +public class JuintTest extends TestCase{ |
| 13 | + |
| 14 | + @Test |
| 15 | + public void testArrayList(){ |
| 16 | + try { |
| 17 | + ArrayListV00 arrayList = new ArrayListV00(0); |
| 18 | + arrayList.add("aaa"); |
| 19 | + arrayList.add("bbb"); |
| 20 | + arrayList.add("ccc"); |
| 21 | + arrayList.add("fff"); |
| 22 | + arrayList.add("ggg"); |
| 23 | + System.out.println("集合下标2处的元素:"+arrayList.get(2)); |
| 24 | + System.out.println("是否包含ccc这个元素:"+arrayList.contains("ccc")); |
| 25 | + System.out.println("是否包含ddd这个元素:"+arrayList.contains("ddd")); |
| 26 | + System.out.println("删除前集合大小为:"+arrayList.size()); |
| 27 | + System.out.println("删除下标2处元素前底层数组:"+arrayList); |
| 28 | + arrayList.remove(2); |
| 29 | + System.out.println("删除下标2处元素后底层数组:"+arrayList); |
| 30 | + System.out.println("删除一个元素后集合大小为:"+arrayList.size()); |
| 31 | + arrayList.remove(2); |
| 32 | + System.out.println("再删除下标2处元素后底层数组:"+arrayList); |
| 33 | + System.out.println("集合为:"+ Arrays.toString(arrayList.toArray())); |
| 34 | + System.out.println("集合底层数组长度:"+ arrayList.arrayLength()); |
| 35 | +// System.out.println("集合下标-1处的元素:"+arrayList.get(-1)); |
| 36 | + } catch (MyException e) { |
| 37 | + System.out.println("发生异常>>>"+e); |
| 38 | + } catch (Exception e) { |
| 39 | + e.printStackTrace(); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testLinkedList(){ |
| 45 | + try { |
| 46 | + LinkedListV00<String> linkedList = new LinkedListV00<>(); |
| 47 | + linkedList.add("aaa"); |
| 48 | + linkedList.add("bbb"); |
| 49 | + linkedList.add("ccc"); |
| 50 | + linkedList.add("ddd"); |
| 51 | + System.out.println("删除index=2的元素前:"+linkedList); |
| 52 | + System.out.println("链表尺寸"+linkedList.size()); |
| 53 | + System.out.println("拿到index=2的元素"+linkedList.get(2)); |
| 54 | + linkedList.remove(2); |
| 55 | + System.out.println("删除index=2的元素后:"+linkedList); |
| 56 | + } catch (MyException e) { |
| 57 | + System.out.println(e.getMessage()); |
| 58 | + } catch (Exception e) { |
| 59 | + e.printStackTrace(); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testStack(){ |
| 65 | + try { |
| 66 | + StackV00 stack = new StackV00(); |
| 67 | + stack.push("ccc"); |
| 68 | + stack.push(null); |
| 69 | + stack.push("bbb"); |
| 70 | + stack.push("aaa"); |
| 71 | + System.out.println("栈的大小:"+stack.size()); |
| 72 | + System.out.println("栈是否为空:"+stack.isEmpty()); |
| 73 | + System.out.println("栈是否为空:"+stack); |
| 74 | + System.out.println(stack.pop()); |
| 75 | + System.out.println(stack.pop()); |
| 76 | + System.out.println(stack.pop()); |
| 77 | + System.out.println(stack.pop()); |
| 78 | + stack.clear(); |
| 79 | + System.out.println("清空后,栈大小:"+stack.size()); |
| 80 | + System.out.println("栈是否为空:"+stack.isEmpty()); |
| 81 | + } catch (MyException e) { |
| 82 | + System.out.println(e); |
| 83 | + } catch (Exception e) { |
| 84 | + e.printStackTrace(); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testQueue(){ |
| 90 | + try { |
| 91 | + QueueV00 queue = new QueueV00(); |
| 92 | + System.out.println("队列是否为空:"+queue.isEmpty()); |
| 93 | + queue.add("aaa"); |
| 94 | + queue.add("bbb"); |
| 95 | + queue.add("ccc"); |
| 96 | + queue.add("ddd"); |
| 97 | + System.out.println(queue); |
| 98 | + System.out.println("queue.peek结果:"+queue.peek()); |
| 99 | + System.out.println("peek后队列长度:"+queue.length()); |
| 100 | + System.out.println("queue.poll结果:"+queue.poll()); |
| 101 | + System.out.println("poll后队列长度:"+queue.length()); |
| 102 | + System.out.println("队列是否为空:"+queue.isEmpty()); |
| 103 | + } catch (MyException e) { |
| 104 | + System.out.println(e.getMessage()); |
| 105 | + } catch (Exception e) { |
| 106 | + e.printStackTrace(); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void testArrayLinked(){ |
| 112 | + try { |
| 113 | + ArrayListV00 arrayList = new ArrayListV00(); |
| 114 | + LinkedListV00 linkedList = new LinkedListV00(); |
| 115 | + long start1 = System.currentTimeMillis(); |
| 116 | + for(int i = 0;i<10000;i++){ |
| 117 | + arrayList.add("abc"+i); |
| 118 | + } |
| 119 | + long end1 = System.currentTimeMillis(); |
| 120 | + for(int i = 0;i<10000;i++){ |
| 121 | + linkedList.add("abc"+i); |
| 122 | + } |
| 123 | + long end2 = System.currentTimeMillis(); |
| 124 | + System.out.println("ArrayList的时间:"+(end1-start1)); |
| 125 | + System.out.println("LinkedList的时间:"+(end2-end1)); |
| 126 | + } catch (MyException e) { |
| 127 | + e.printStackTrace(); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + public String getRandomString(int length){ |
| 132 | + String base = "abcdefghijklmnopqrstuvwxyz0123456789"; |
| 133 | + Random random = new Random(); |
| 134 | + StringBuffer sb = new StringBuffer(); |
| 135 | + for(int i = 0;i<length;i++){ |
| 136 | + int number = random.nextInt(base.length()); |
| 137 | + sb.append(base.charAt(number)); |
| 138 | + } |
| 139 | + return sb.toString(); |
| 140 | + } |
| 141 | +} |
0 commit comments