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!

When using MyBatis-Plus or tk.mybatis...

How to query personnel data through natural language processing? In modern data processing, how to efficiently query personnel data is a common and important requirement. ...

In processing next-auth generated JWT...

In IntelliJ...

Discussion on the reasons why JavaScript cannot obtain user computer hardware information In daily programming, many developers will be curious about why JavaScript cannot be directly obtained...

RuoYi framework circular dependency problem troubleshooting and solving the problem of circular dependency when using RuoYi framework for development, we often encounter circular dependency problems, which often leads to the program...

About SpringCloudAlibaba microservices modular development using SpringCloud...

Questions about a curve integral This article will answer a curve integral question. The questioner had a question about the standard answer to a sample question...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)