ホームページ  >  記事  >  Java  >  データ構造: 配列

データ構造: 配列

王林
王林オリジナル
2024-08-11 20:35:321127ブラウズ

Data Structures: Arrays

静的配列

配列は、すべての要素が順番に配置される線形データ構造です。これは、連続メモリの場所に保存されている同じデータ型の要素のコレクションです。

初期化

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 サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。