package com.demo; public class TArray<T> { private T[] data; private int size; @SuppressWarnings("unchecked") public TArray(int capacity) { data = (T[]) new Object [capacity]; size = 0; } public TArray() { this(10); } // 获取数组容量 public int getCapacity() { return data.length; } //Get the number of elements in the array public int getSize () { return size; } // determine whether the array is empty public Boolean isEmpty () { return size == 0 ; } // array elements append public void addLast (T E) { the Add (size, E); } // add elements at the beginning of the array public void the addFirst (T E) { the Add ( 0 , E); } // inserted in the middle of the array element public void the Add ( int index, T E) { IF (size == data.length) throw new IllegalArgumentException("add fail,array id full"); if (index < 0 || index > size) throw new IllegalArgumentException("add fail, need index > 0 and index <=size"); for (int i = size - 1; i >= index; i--) data[i + 1] = data[i]; data[index] = e; size++; } // 根据引脚获取值 public T get(int index) { if (index < 0 || index >= size) throw new IllegalArgumentException("get fail , index < 0 or index >= size"); return data[index]; } // 根据引脚设置值 public void set(int index, T e) { if (index < 0 || index >= size) throw new IllegalArgumentException("get fail , index < 0 or index >= size"); data[index] = e; } // 是否包含某个元素 public boolean contains(T e) { for (int0 = I; I <size; I ++ ) { IF (e.equals (Data [I])) { return to true ; } } return to false ; } // find an element corresponding to the position of public int Find (T E) { for ( int I = 0; I <size; I ++ ) { IF (e.equals (Data [I])) { return I; } } return -1 ; } // remove index corresponding element returns the element removed public T Remove ( int index) { IF (index <0 || index> = size) { the throw new new an IllegalArgumentException ( "reomve Fail, Illegal args" ); } T RET = Data [index]; for ( int I = index +. 1; I <size; I ++ ) { Data [I -1] = Data [I]; } size - ; return RET; } // removes the first element public T removeFirst () { return remove (0 ); } // remove the last element of publicRemoveLast T () { return Remove (-size. 1 ); } // The delete elements in the array elements and returns the value corresponding to the deleted public Boolean removeElement (T E) { Boolean In Flag = to true ; int index = Find (E); the try { Remove (index); } the catch (Exception E2) { In Flag = to false ; } return In Flag; } @Override public String toString () { the StringBuilder SB = new new StringBuilder(); sb.append(String.format("Array: size = %d , capacity = %d \n", size, data.length)); sb.append('['); for (int i = 0; i < size; i++) { sb.append(data[i]); if (i != size - 1) { sb.append(','); } } sb.append(']'); return sb.toString(); } }
Array for expansion and volume reduction
Package com.demo; public class TArray <T> { Private T [] Data; Private int size; // Data array element number of the actual @SuppressWarnings ( "an unchecked" ) public TArray ( int Capacity) { Data = (T [ ]) new new Object [capacity]; size = 0 ; } public TArray () { the this (10 ); } // get the array capacity public int getCapacity () { returndata.length; } // Get the number of elements in the array public int getSize () { return size; } // determine whether the array is empty public Boolean isEmpty () { return size == 0 ; } // appended to the end of the array elements public void addLast (T E) { the Add (size, E); } // add elements at the beginning of the array public void the addFirst (T E) { the Add ( 0 , E); } // inserted in the middle of the array element public void the Add ( int index, T e) { if (index < 0 || index > size) throw new IllegalArgumentException("add fail, need index > 0 and index <=size"); if (size == data.length) //扩容数组 resize(2*data.length); for (int i = size - 1; i >= index; i--) data[i + 1] = data[i]; data[index] = e; size++; } @SuppressWarnings("unchecked") private void resize(int length) { T [] newData = (T[]) new Object [length]; for(int i = 0 ; i < size ; i++){ newData[i] = data[i]; } data = newData; } // 根据引脚获取值 public T get(int index) { if (index < 0 || index >= size) throw new IllegalArgumentException("get fail , index < 0 or index >= size"); return data[index]; } //The pin setting value public void SET ( int index, T E) { IF (index <0 || index> = size) the throw new new an IllegalArgumentException ( "GET Fail, index <0 or index> size =" ); Data [index ] = E; } // contains an element public Boolean the contains (T E) { for ( int I = 0; I <size; I ++ ) { iF (e.equals (Data [I])) { return to true ; } } return to false ; } // Find position corresponding to an element public int Find (T E) { for ( int I = 0; I <size; I ++ ) { IF (e.equals (Data [I])) { return I; } } return -1 ; } // remove index corresponding element returns the element removed public T remove ( int index) { IF (index <0 || index> = size) { the throw new new an IllegalArgumentException ( "Fail reomve, args Illegal" ); } RET T =Data [index]; for ( int I = index +. 1; I <size; I ++ ) { Data [I -1] = Data [I]; } size - ; // deletion capacity IF (size == Data. length / 2 ) { a resize (size); } return RET; } // removes the first element public T removeFirst () { return remove (0 ); } // remove the last element of a public T removeLast () { return remove ( . 1-size ); } // The array element corresponding to the element deletion and delete the return value public Boolean removeElement (T E) { Boolean In Flag = to true ; int index = Find (E); the try { Remove (index); } the catch (Exception E2) { In Flag = to false ; } return In Flag; } @Override public String toString () { the StringBuilder SB = new new the StringBuilder (); sb.append (String.format ( "the Array: size =% D, D Capacity =% \ n-", size, data.length)); sb.append('['); for (int i = 0; i < size; i++) { sb.append(data[i]); if (i != size - 1) { sb.append(','); } } sb.append(']'); return sb.toString(); } }
Test code
package com.demo; public class Demo { public static void main (String [] args) { relation <Integer> relation = new relation <> (10 ); for ( int i = 0; i <tArray.getCapacity (); i ++ ) { tArray.add (i, i); } System.out.println (relation); tArray.add ( 2, 345 ); System.out.println (relation); tArray.remove ( 3 ); System.out.println (relation); } }
Output
Array: size = 10 , capacity = 10 [0,1,2,3,4,5,6,7,8,9] Array: size = 11 , capacity = 20 [0,1,345,2,3,4,5,6,7,8,9] Array: size = 10 , capacity = 10 [0,1,345,3,4,5,6,7,8,9]