<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Muhammad Abdul Ghani Qureshi</title>
    <description>The latest articles on DEV Community by Muhammad Abdul Ghani Qureshi (@abdulghani002).</description>
    <link>https://dev.to/abdulghani002</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1912773%2Fa61f0f65-1f3d-467f-9bc6-589121853add.jpg</url>
      <title>DEV Community: Muhammad Abdul Ghani Qureshi</title>
      <link>https://dev.to/abdulghani002</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abdulghani002"/>
    <language>en</language>
    <item>
      <title>Data Structures: Arrays</title>
      <dc:creator>Muhammad Abdul Ghani Qureshi</dc:creator>
      <pubDate>Sun, 11 Aug 2024 11:31:55 +0000</pubDate>
      <link>https://dev.to/abdulghani002/data-structures-arrays-1ad4</link>
      <guid>https://dev.to/abdulghani002/data-structures-arrays-1ad4</guid>
      <description>&lt;h2&gt;
  
  
  Static Array
&lt;/h2&gt;

&lt;p&gt;Array is a linear data structure where all elements are arranged sequentially. It is a collection of elements of &lt;strong&gt;&lt;em&gt;same&lt;/em&gt;&lt;/strong&gt; data type stored at &lt;strong&gt;&lt;em&gt;contiguous memory&lt;/em&gt;&lt;/strong&gt; locations.&lt;/p&gt;

&lt;h3&gt;
  
  
  Initialization
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Array&amp;lt;T&amp;gt; {
    private T[] self;
    private int size;
    @SuppressWarnings("unchecked")
    public Array(int size) {
        if (size &amp;lt;= 0) {
            throw new IllegalArgumentException("Invalid array size (must be positive): " + size);
        } else {
            this.size = size;
            this.self = (T[]) new Object[size];
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;h3&gt;
  
  
  Set Method
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public void set(T item, int index) {
        if (index &amp;gt;= this.size || index &amp;lt; 0) {
            throw new IndexOutOfBoundsException("Index Out of bounds: " + index);
        } else {
            this.self[index] = item;
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method is asking for an item to be stored in array and index on which item should be stored.&lt;/p&gt;

&lt;h3&gt;
  
  
  Get Method
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public T get(int index) {
        if (index &amp;gt;= this.size || index &amp;lt; 0) {
            throw new IndexOutOfBoundsException("Index Out of bounds");
        } else {
            return self[index];
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Get Method asks for an index and retrives item from that index.&lt;/p&gt;

&lt;h3&gt;
  
  
  Print Method
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public void print() {
        for (int i = 0; i &amp;lt; size; i++) {
            System.out.println(this.self[i]+" ");
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Print Method is just printing all members of an array in a single line with a space seperating each item between them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sorted Array
&lt;/h2&gt;

&lt;p&gt;Arrays but having a functionality to sort elements itself.&lt;/p&gt;

&lt;h3&gt;
  
  
  Initialization
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class SortedArray&amp;lt;T extends Comparable&amp;lt;T&amp;gt;&amp;gt; {
    private T[] array;
    private int size;
    private final int maxSize;
    @SuppressWarnings("unchecked")
    public SortedArray(int maxSize) {
        if (maxSize &amp;lt;= 0) {
            throw new IllegalArgumentException("Invalid array max size (must be positive): " + maxSize);
        }
        this.array = (T[]) new Comparable[maxSize];
        this.size = 0;
        this.maxSize = maxSize;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;h3&gt;
  
  
  Getters
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public int length() {
        return this.size;
    }
 public int maxLength() {
        return this.maxSize;
    }
 public T get(int index) {
        if (index &amp;lt; 0 || index &amp;gt;= this.size) {
            throw new IndexOutOfBoundsException("Index out of 
 bounds: " + index);
        }
        return this.array[index];
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Insertion Method
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private int findInsertionPosition(T item) {
        int left = 0;
        int right = size - 1;
        while (left &amp;lt;= right) {
            int mid = (left + right) / 2;
            int cmp = item.compareTo(this.array[mid]);
            if (cmp &amp;lt; 0) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return left;
    }
public void insert(T item) {
        if (this.size &amp;gt;= this.maxSize) {
            throw new IllegalStateException("The array is already full");
        }

        int position = findInsertionPosition(item);

        for (int i = size; i &amp;gt; position; i--) {
            this.array[i] = this.array[i - 1];
        }
        this.array[position] = item;
        size++;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Insert Method inserts the item on its position in sorted form.&lt;/p&gt;

&lt;h3&gt;
  
  
  Deletion Method
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    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; i &amp;lt; size - 1; i++) {
            this.array[i] = this.array[i + 1];
        }
        this.array[size - 1] = null;
        size--;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Search Methods
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private int binarySearch(T target) {
        int left = 0;
        int right = size - 1;
        while (left &amp;lt;= right) {
            int mid = (left + right) / 2;
            int cmp = target.compareTo(this.array[mid]);
            if (cmp == 0) {
                return mid;
            } else if (cmp &amp;lt; 0) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return -1;
    }
public Integer find(T target) {
        int index = binarySearch(target);
        return index == -1 ? null : index;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Traverse Method
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public void traverse(Callback&amp;lt;T&amp;gt; callback) {
        for (int i = 0; i &amp;lt; this.size; i++) {
            callback.call(this.array[i]);
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Callback Interface
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public interface Callback&amp;lt;T&amp;gt; {
        void call(T item);
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Use of Callback Interface in Traversing
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class UppercaseCallback implements UnsortedArray.Callback&amp;lt;String&amp;gt; {
    @Override
    public void call(String item) {
        System.out.println(item.toUpperCase());
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Unsorted Array
&lt;/h2&gt;

&lt;p&gt;It is almost same from above&lt;br&gt;
Initialization and getters are same.&lt;/p&gt;

&lt;h3&gt;
  
  
  Insertion Method
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public void insert(T item) {
        if (this.size &amp;gt;= this.maxSize) {
            throw new IllegalStateException("The array is already full");
        } else {
            this.self[this.size] = item;
            this.size++;
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Delete Method is also same&lt;/p&gt;

&lt;h3&gt;
  
  
  Search Method
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public Integer find(T target) {
        for (int i = 0; i &amp;lt; this.size; i++) {
            if (this.self[i].equals(target)) {
                return i;
            }
        }
        return null;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Dynamic Array
&lt;/h2&gt;

&lt;p&gt;Dynamic Array are like array lists or lists.&lt;/p&gt;

&lt;h3&gt;
  
  
  Initialization
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class DynamicArray&amp;lt;T&amp;gt; {
    private T[] array;
    private int size;
    private int capacity;

    @SuppressWarnings("unchecked")
    public DynamicArray(int initialCapacity) {
        if (initialCapacity &amp;lt;= 0) {
            throw new IllegalArgumentException("Invalid initial capacity: " + initialCapacity);
        }
        this.capacity = initialCapacity;
        this.array = (T[]) new Object[initialCapacity];
        this.size = 0;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Insert Method
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private void resize(int newCapacity) {
        @SuppressWarnings("unchecked")
        T[] newArray = (T[]) new Object[newCapacity];
        for (int i = 0; i &amp;lt; size; i++) {
            newArray[i] = array[i];
        }
        array = newArray;
        capacity = newCapacity;
    }
    public void insert(T item) {
        if (size &amp;gt;= capacity) {
            resize(2 * capacity);
        }
        array[size++] = item;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Delete Method
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public void delete(T item) {
        int index = find(item);
        if (index == -1) {
            throw new IllegalArgumentException("Item not found: " + item);
        }

        for (int i = index; i &amp;lt; size - 1; i++) {
            array[i] = array[i + 1];
        }
        array[--size] = null;
        if (capacity &amp;gt; 1 &amp;amp;&amp;amp; size &amp;lt;= capacity / 4) {
            resize(capacity / 2);
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Everything else is same.&lt;br&gt;
Hope this helps in working with arrays. Good Luck!&lt;/p&gt;

</description>
      <category>datastructures</category>
      <category>java</category>
      <category>arrays</category>
      <category>dynamicarrays</category>
    </item>
  </channel>
</rss>
