Advertisement
eliasdaler

Generic array in java

Dec 20th, 2013
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.21 KB | None | 0 0
  1. //MyArray.java
  2. /* Copyright (c) 2013 Elias Daler
  3.  
  4.     This software is provided 'as-is', without any express or implied
  5.     warranty. In no event will the authors be held liable for any damages
  6.     arising from the use of this software.
  7.  
  8.     Permission is granted to anyone to use this software for any purpose,
  9.     including commercial applications, and to alter it and redistribute it
  10.     freely, subject to the following restrictions:
  11.  
  12.     1. The origin of this software must not be misrepresented; you must not
  13.     claim that you wrote the original software. If you use this software
  14.     in a product, an acknowledgment in the product documentation would be
  15.     appreciated but is not required.
  16.  
  17.     2. Altered source versions must be plainly marked as such, and must not be
  18.     misrepresented as being the original software.
  19.  
  20.     3. This notice may not be removed or altered from any source
  21.     distribution.
  22. */
  23.  
  24. import java.lang.reflect.Array;
  25.  
  26. public class MyArray<T> {
  27.     private T[] arr;
  28.     private int size;
  29.  
  30.     @SuppressWarnings({"unchecked"})
  31.     public MyArray(int s) {
  32.         if(s <= 0) {
  33.             throw new IndexOutOfBoundsException();
  34.         }
  35.         arr = (T[])new Object[s];
  36.         size = s;
  37.     }
  38.  
  39.     public void insert(T item, int index) {
  40.         if(index >= size || index < 0) {
  41.             throw new ArrayIndexOutOfBoundsException();
  42.         }
  43.         arr[index] = item;
  44.     }
  45.  
  46.     public void insert(T item) {
  47.         T[] arr_new = (T[])new Object[size + 1]; // create bigger array
  48.         arr_new[size] = item; // insert item in the end
  49.         for(int i = 0; i < size; i++) { // copy old array
  50.             arr_new[i] = arr[i];
  51.         }
  52.         arr = arr_new;
  53.         size++;
  54.     }
  55.  
  56.     public T get(int index) {
  57.         if(index >= size || index < 0) {
  58.             throw new ArrayIndexOutOfBoundsException();
  59.         }
  60.         return arr[index];
  61.     }
  62.  
  63.     public void remove(int index) {
  64.         if(index >= size || index < 0) {
  65.             throw new ArrayIndexOutOfBoundsException();
  66.         }
  67.         arr[size] = null;
  68.     }
  69.  
  70.     public int size() {
  71.         return size;
  72.     }
  73.  
  74.     public String toString() {
  75.         String str = new String();
  76.         for(int i = 0; i < size; i++) {
  77.             if(arr[i] != null) {
  78.                 str += arr[i].toString();
  79.                 if(i != size - 1) str += ", "; // don't add comma in the end
  80.             } else {
  81.                 str += "null, ";
  82.             }
  83.         }
  84.         return str;
  85.     }
  86.  
  87.     @Override
  88.     public boolean equals(Object o) {
  89.         if(!(o instanceof MyArray)) return false;
  90.         MyArray<T> second = (MyArray<T>)o;
  91.         if(size != second.size())  {
  92.             return false;
  93.         }
  94.         for(int i = 0; i < size; i++) {
  95.             if(arr[i] != second.get(i)) return false;
  96.         }
  97.         return true;
  98.     }
  99.  
  100.     public int hashCode() {
  101.         return arr.hashCode();
  102.     }
  103. }
  104.  
  105. // main.java
  106.  
  107. public class Main {
  108.  
  109.     public static void main(String[] args) {
  110.         MyArray<Integer> test = new MyArray<Integer>(10);
  111.         test.insert(5, 2);
  112.         test.insert(10, 3);
  113.         test.insert(20);
  114.         System.out.println(test.toString());
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement