データ構造: 配列
静的配列
配列は、すべての要素が順番に配置される線形データ構造です。これは、連続メモリの場所に保存されている同じデータ型の要素のコレクションです。
初期化
public class Array<T> { private T[] self; private int size; @SuppressWarnings("unchecked") public Array(int size) { if (size <= 0) { throw new IllegalArgumentException("Invalid array size (must be positive): " + size); } else { this.size = size; this.self = (T[]) new Object[size]; } } }
コア配列クラスでは、配列のサイズと配列の初期化のための一般的なスケルトンを保存します。コンストラクターでは、配列のサイズを要求し、オブジェクトを作成し、それを目的の配列に型キャストします。
セットメソッド
public void set(T item, int index) { if (index >= this.size || index < 0) { throw new IndexOutOfBoundsException("Index Out of bounds: " + index); } else { this.self[index] = item; } }
このメソッドは、配列に格納される項目と、どの項目を格納するかについてのインデックスを要求しています。
取得メソッド
public T get(int index) { if (index >= this.size || index < 0) { throw new IndexOutOfBoundsException("Index Out of bounds"); } else { return self[index]; } }
Get メソッドはインデックスを要求し、そのインデックスから項目を取得します。
印刷方法
public void print() { for (int i = 0; i < size; i++) { System.out.println(this.self[i]+" "); } }
Print メソッドは、配列のすべてのメンバーをスペースで区切って 1 行に出力するだけです。
ソートされた配列
配列ですが、要素自体を並べ替える機能があります。
初期化
public class SortedArray<T extends Comparable<T>> { private T[] array; private int size; private final int maxSize; @SuppressWarnings("unchecked") public SortedArray(int maxSize) { if (maxSize <= 0) { throw new IllegalArgumentException("Invalid array max size (must be positive): " + maxSize); } this.array = (T[]) new Comparable[maxSize]; this.size = 0; this.maxSize = maxSize; } }
ソートされた配列クラスでは、配列のサイズを保存し、配列の最大サイズと配列の初期化のための一般的なスケルトンも要求します。コンストラクターでは、配列の最大サイズを要求し、オブジェクトを作成し、それを目的の配列に型キャストします。
ゲッター
public int length() { return this.size; } public int maxLength() { return this.maxSize; } public T get(int index) { if (index < 0 || index >= this.size) { throw new IndexOutOfBoundsException("Index out of bounds: " + index); } return this.array[index]; }
挿入方法
private int findInsertionPosition(T item) { int left = 0; int right = size - 1; while (left <= right) { int mid = (left + right) / 2; int cmp = item.compareTo(this.array[mid]); if (cmp < 0) { right = mid - 1; } else { left = mid + 1; } } return left; } public void insert(T item) { if (this.size >= 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 メソッドは、並べ替えられた形式でその位置に項目を挿入します。
削除方法
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 < size - 1; i++) { this.array[i] = this.array[i + 1]; } this.array[size - 1] = null; size--; }
検索方法
private int binarySearch(T target) { int left = 0; int right = size - 1; while (left <= right) { int mid = (left + right) / 2; int cmp = target.compareTo(this.array[mid]); if (cmp == 0) { return mid; } else if (cmp < 0) { right = mid - 1; } else { left = mid + 1; } } return -1; } public Integer find(T target) { int index = binarySearch(target); return index == -1 ? null : index; }
トラバース法
public void traverse(Callback<T> callback) { for (int i = 0; i < this.size; i++) { callback.call(this.array[i]); } }
コールバックインターフェイス
public interface Callback<T> { void call(T item); }
トラバースでのコールバック インターフェイスの使用
public class UppercaseCallback implements UnsortedArray.Callback<String> { @Override public void call(String item) { System.out.println(item.toUpperCase()); } }
未ソートの配列
上から見てもほぼ同じ
初期化とゲッターは同じです。
挿入方法
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++; } }
削除方法も同様です
検索方法
public Integer find(T target) { for (int i = 0; i < this.size; i++) { if (this.self[i].equals(target)) { return i; } } return null; }
ダイナミックアレイ
動的配列は配列リストまたはリストに似ています。
初期化
public class DynamicArray<T> { private T[] array; private int size; private int capacity; @SuppressWarnings("unchecked") public DynamicArray(int initialCapacity) { if (initialCapacity <= 0) { throw new IllegalArgumentException("Invalid initial capacity: " + initialCapacity); } this.capacity = initialCapacity; this.array = (T[]) new Object[initialCapacity]; this.size = 0; } }
挿入メソッド
private void resize(int newCapacity) { @SuppressWarnings("unchecked") T[] newArray = (T[]) new Object[newCapacity]; for (int i = 0; i < size; i++) { newArray[i] = array[i]; } array = newArray; capacity = newCapacity; } public void insert(T item) { if (size >= capacity) { resize(2 * capacity); } array[size++] = item; }
削除メソッド
public void delete(T item) { int index = find(item); if (index == -1) { throw new IllegalArgumentException("Item not found: " + item); } for (int i = index; i < size - 1; i++) { array[i] = array[i + 1]; } array[--size] = null; if (capacity > 1 && size <= capacity / 4) { resize(capacity / 2); } }
他はすべて同じです。
これが配列の操作に役立つことを願っています。頑張ってください!
以上がデータ構造: 配列の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











一部のアプリケーションが適切に機能しないようにする会社のセキュリティソフトウェアのトラブルシューティングとソリューション。多くの企業は、内部ネットワークセキュリティを確保するためにセキュリティソフトウェアを展開します。 ...

多くのアプリケーションシナリオでソートを実装するために名前を数値に変換するソリューションでは、ユーザーはグループ、特に1つでソートする必要がある場合があります...

システムドッキングでのフィールドマッピング処理は、システムドッキングを実行する際に難しい問題に遭遇することがよくあります。システムのインターフェイスフィールドを効果的にマッピングする方法A ...

データベース操作にMyBatis-Plusまたはその他のORMフレームワークを使用する場合、エンティティクラスの属性名に基づいてクエリ条件を構築する必要があることがよくあります。あなたが毎回手動で...

intellijideaultimatiateバージョンを使用してスプリングを開始します...

Javaオブジェクトと配列の変換:リスクの詳細な議論と鋳造タイプ変換の正しい方法多くのJava初心者は、オブジェクトのアレイへの変換に遭遇します...

eコマースプラットフォーム上のSKUおよびSPUテーブルの設計の詳細な説明この記事では、eコマースプラットフォームでのSKUとSPUのデータベース設計の問題、特にユーザー定義の販売を扱う方法について説明します。

Redisキャッシュソリューションは、製品ランキングリストの要件をどのように実現しますか?開発プロセス中に、多くの場合、ランキングの要件に対処する必要があります。
