GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — data-structure/ArrayStack/src/Array.java at master · developbiao/data-structure Expand file tree/ Copy pathArray.javaMore file actions 206 lines (172 loc) · 3.92 KB / Copy pathArray.javaFile metadata and controls206 lines (172 loc) · 3.92 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 import java.util.Arrays; public class Array<E> { private E[] data; private int size; /** * * construct * @param capacity */ public Array(int capacity) { this.data = (E[]) new Object[capacity]; this.size = 0; } public Array() { this(10); } /** * get size * @return */ public int getSize() { return this.size; } /** * get capacity * @return */ public int getCapacity() { return this.data.length; } public boolean isEmpty() { return this.size == 0; } public void addLast(E e) { this.add(this.size, e); } public void addFirst(E e) { this.add(0, e); } public void add(int index, E e) { if(this.size == this.data.length) { throw new IllegalArgumentException("AddLast failed. Array is full.") ; } if(index < 0 || index > size) { throw new IllegalArgumentException("Add failed. Required index >=0 and <= size."); } for(int i = this.size - 1; i >= index; i--) { this.data[i + 1] = this.data[i]; } this.data[index] = e; size ++; } /** * * Get element by index * @param index * @return */ public E get(int index) { if( index < 0 || index > this.size) { throw new IllegalArgumentException("Get failed. Index is illegal."); } return this.data[index]; } public E getLast() { return data[size - 1]; } /** * Set value with index * @param index * @param e */ public void set(int index, E e) { if(index < 0 || index > this.size) { throw new IllegalArgumentException("Get failed. Index is illegal."); } this.data[index] = e; } /** * Query element contains in the array * @param e * @return */ public boolean contains(E e) { for(int i = 0; i < data.length; i++) { if(data[i] == e) { return true; } } return false; } /** * find element index when not found return -1 * @param e * @return */ public int find(E e) { for(int i = 0; i < data.length; i++) { if(data[i] == e) { return i; } } return -1; } /** * Remove element by index * @param index * @return */ public E remove(int index) { if(index < 0 || index > size) { throw new IllegalArgumentException("Remove failed, Index is illegal."); } E ret = data[index]; for(int i = index + 1; i < size; i++) { data[i - 1] = data[i]; } size --; return ret; } /** * remove first element * @return */ public E removeFirst() { return remove(0); } /** * * remove last element * @return */ public E removeLast() { return remove(size - 1); } /** * remove element with array * @param e */ public void removeElement(E e) { int index = find(e); if(index != -1) { remove(index); } } @Override public String toString() { StringBuilder res = new StringBuilder(); res.append(String.format("Array: size = %d, capacity = %d\n",this.size , this.getCapacity() ) ); res.append('['); for (int i=0; i < this.size; i++) { res.append(data[i]); if(i != this.size - 1) { res.append(", "); } } res.append(']'); return res.toString(); } private void resize(int newCapacity) { E[] newData = (E[]) new Object[newCapacity]; for(int i = 0; i< size; i++) { newData[i] = data[i]; } data = newData; } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |