Home >Java >javaTutorial >Data Structures: Arrays

Data Structures: Arrays

王林
王林Original
2024-08-11 20:35:321261browse

Data Structures: Arrays

Static Array

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.

Initialization

public class Array<t> {
    private T[] self;
    private int size;
    @SuppressWarnings("unchecked")
    public Array(int size) {
        if (size 



<p>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.</p>

<h3>
  
  
  Set Method
</h3>



<pre class="brush:php;toolbar:false">public void set(T item, int index) {
        if (index >= this.size || index 



<p>This method is asking for an item to be stored in array and index on which item should be stored.</p>

<h3>
  
  
  Get Method
</h3>



<pre class="brush:php;toolbar:false">public T get(int index) {
        if (index >= this.size || index 



<p>Get Method asks for an index and retrives item from that index.</p>

<h3>
  
  
  Print Method
</h3>



<pre class="brush:php;toolbar:false">public void print() {
        for (int i = 0; i 



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

<h2>
  
  
  Sorted Array
</h2>

<p>Arrays but having a functionality to sort elements itself.</p>

<h3>
  
  
  Initialization
</h3>



<pre class="brush:php;toolbar:false">public class SortedArray<t extends comparable>> {
    private T[] array;
    private int size;
    private final int maxSize;
    @SuppressWarnings("unchecked")
    public SortedArray(int maxSize) {
        if (maxSize 



<p>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.</p>

<h3>
  
  
  Getters
</h3>



<pre class="brush:php;toolbar:false">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; i 



<h3>
  
  
  Search Methods
</h3>



<pre class="brush:php;toolbar:false">private int binarySearch(T target) {
        int left = 0;
        int right = size - 1;
        while (left 



<h3>
  
  
  Traverse Method
</h3>



<pre class="brush:php;toolbar:false">public void traverse(Callback<t> callback) {
        for (int i = 0; i 



<h3>
  
  
  Callback Interface
</h3>



<pre class="brush:php;toolbar:false">public interface Callback<t> {
        void call(T item);
    }
</t>

Use of Callback Interface in Traversing

public class UppercaseCallback implements UnsortedArray.Callback<string> {
    @Override
    public void call(String item) {
        System.out.println(item.toUpperCase());
    }
}
</string>

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; i 



<h2>
  
  
  Dynamic Array
</h2>

<p>Dynamic Array are like array lists or lists.</p>

<h3>
  
  
  Initialization
</h3>



<pre class="brush:php;toolbar:false">public class DynamicArray<t> {
    private T[] array;
    private int size;
    private int capacity;

    @SuppressWarnings("unchecked")
    public DynamicArray(int initialCapacity) {
        if (initialCapacity 



<h3>
  
  
  Insert Method
</h3>



<pre class="brush:php;toolbar:false">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 && size 



<p>Everything else is same.<br>
Hope this helps in working with arrays. Good Luck!</p>


          

            
        

The above is the detailed content of Data Structures: Arrays. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn