Array is a linear data structure where all elements are arranged sequentially. It is a collection of elements of same data type stored at contiguous memory locations.
public class Array{ private T[] self; private int size; @SuppressWarnings("unchecked") public Array(int size) { if (size In Core Array Class, we are going to store size of array and a general skeleton for array initialization. In constructor, we are asking for size of array and making an object and type casting it in our desired array.
Set Method
public void set(T item, int index) { if (index >= this.size || indexThis method is asking for an item to be stored in array and index on which item should be stored.
Get Method
public T get(int index) { if (index >= this.size || indexGet Method asks for an index and retrives item from that index.
Print Method
public void print() { for (int i = 0; iPrint Method is just printing all members of an array in a single line with a space seperating each item between them.
Sorted Array
Arrays but having a functionality to sort elements itself.
Initialization
public class SortedArray> { private T[] array; private int size; private final int maxSize; @SuppressWarnings("unchecked") public SortedArray(int maxSize) { if (maxSize In Sorted Array Class, we are going to store size of array and ask for Max size of array as well and a general skeleton for array initialization. In constructor, we are asking for Max Size of array and making an object and type casting it in our desired array.
Getters
public int length() { return this.size; } public int maxLength() { return this.maxSize; } public T get(int index) { if (index = this.size) { throw new IndexOutOfBoundsException("Index out of bounds: " index); } return this.array[index]; }Insertion Method
private int findInsertionPosition(T item) { int left = 0; int right = size - 1; while (left = this.maxSize) { throw new IllegalStateException("The array is already full"); } int position = findInsertionPosition(item); for (int i = size; i > position; i--) { this.array[i] = this.array[i - 1]; } this.array[position] = item; size ; }Insert Method inserts the item on its position in sorted form.
Deletion Method
public void delete(T item) { int index = binarySearch(item); if (index == -1) { throw new IllegalArgumentException("Unable to delete element " item ": the entry is not in the array"); } for (int i = index; iSearch Methods
private int binarySearch(T target) { int left = 0; int right = size - 1; while (leftTraverse Method
public void traverse(Callbackcallback) { for (int i = 0; i Callback Interface
public interface Callback{ void call(T item); } Use of Callback Interface in Traversing
public class UppercaseCallback implements UnsortedArray.Callback{ @Override public void call(String item) { System.out.println(item.toUpperCase()); } } Unsorted Array
It is almost same from above
Initialization and getters are same.Insertion Method
public void insert(T item) { if (this.size >= this.maxSize) { throw new IllegalStateException("The array is already full"); } else { this.self[this.size] = item; this.size ; } }Delete Method is also same
Search Method
public Integer find(T target) { for (int i = 0; iDynamic Array
Dynamic Array are like array lists or lists.
Initialization
public class DynamicArray{ private T[] array; private int size; private int capacity; @SuppressWarnings("unchecked") public DynamicArray(int initialCapacity) { if (initialCapacity Insert Method
private void resize(int newCapacity) { @SuppressWarnings("unchecked") T[] newArray = (T[]) new Object[newCapacity]; for (int i = 0; i = capacity) { resize(2 * capacity); } array[size ] = item; }Delete Method
public void delete(T item) { int index = find(item); if (index == -1) { throw new IllegalArgumentException("Item not found: " item); } for (int i = index; i 1 && sizeEverything else is same.
Hope this helps in working with arrays. Good Luck!
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3