Java の動的配列とは、ユーザーの要件に応じて配列のサイズを拡大または縮小することを意味します。要素が配列から削除される間、配列のサイズは縮小される必要があり、要素が配列に追加されると、配列のサイズは拡張されます。配列は同種の要素を格納するために使用され、同じ種類の要素を一度に格納できることを意味します。
例: 整数、浮動小数点数、倍精度数値、文字列、文字、オブジェクトなどを保存できますが、一度に保存できるのは特定の型のみです。
無料ソフトウェア開発コースを始めましょう
Web 開発、プログラミング言語、ソフトウェア テスト、その他
配列は 3 つの方法で宣言できます:
1.配列[]
例:
int Array[]={1,2,4};
2. [] 配列
例:
int[] Array ={1,2,4};
3. []A
例:
int []Array ={1,2,4};
配列要素は以下を使用して反復されます:
配列を動的にする他の方法はありますか?
コレクション Arraylist に対する通常の動的配列と比較した利点は何ですか?
構文:
class DynamicArray { addingElements() { //custom logic } addingElementsAtIndex(int index, int element) { //custom logic } removingElements() { //custom logic } removingElementsAtIndex(int index, int element) { //custom logic } increasingSize() { //custom logic } decreasingSize() { //custom logic } printArrayElements() { //custom logic } . . . }
構文:
class ArrayListLogic { List<Generic Type> list=new ArrayList<Generic Type>(); list.add(); list.remove(index,element); . . . }
以下は Java での動的配列の例です:
要素を配列に追加し、サイズと容量を動的に増加します。
コード:
package com.dynamicarray; import java.util.Arrays; public class DynamicArray { // declaring an array int myArray[]; // stores the present size of the array int sizeOfMyArray; // stores total capacity of an array int arrayCapacity; // initializing array, size and capacity public DynamicArray() { myArray = new int[2]; sizeOfMyArray = 0; arrayCapacity = 2; } // method for adding elements public void addElementsToArray(int element) { // makes the capacity double if all the array elements filled if (sizeOfMyArray == arrayCapacity) { increaseCapacity(2); } myArray[sizeOfMyArray] = element; sizeOfMyArray++; } // method for adding elements to specific position public void addElementAtPosition(int position, int value) { // makes the capacity double if all the array elements filled if (sizeOfMyArray == arrayCapacity) { increaseCapacity(2); } // shifting array elements for (int p = sizeOfMyArray - 1; p >= position; p--) { myArray[p + 1] = myArray[p]; } // adding the element at specific position myArray[position] = value; sizeOfMyArray++; } // method for getting the element from specific position public int getElementAtposition(int position) { return myArray[position]; } // method for increasing capacity if all the elements in an array filled public void increaseCapacity(int minimumCapacity) { int temp[] = new int[arrayCapacity * minimumCapacity]; for (int p = 0; p < arrayCapacity; p++) { temp[p] = myArray[p]; } myArray = temp; arrayCapacity = arrayCapacity * minimumCapacity; } // method for array current size public int displaySize() { return sizeOfMyArray; } // method for array total capacity public int displayCapacity() { return arrayCapacity; } // method for display all elements public void displayArrayElements() { System.out.println("elements in array are :" + Arrays.toString(myArray)); } public static void main(String[] args) { DynamicArray array = new DynamicArray(); System.out.println("==================================================================="); System.out.println("Inital array size " + array.displaySize() + " and initial capacity " + array.displayCapacity()); System.out.println("==================================================================="); // adding elements at index 0 and 1 array.addElementsToArray(10);//line 1 array.addElementsToArray(20);//line 2 System.out.println("Size of myArray =>" + array.displaySize() + " and Capacity :" + array.displayCapacity()); array.addElementsToArray(30); //line 3 System.out.println("Size of myArray =>" + array.displaySize() + " and Capacity :" + array.displayCapacity()); array.displayArrayElements(); //line 4 // adding element at index 1 array.addElementAtPosition(1, 50); System.out.println("Size of myArray =>" + array.displaySize() + " and Capacity :" + array.displayCapacity()); array.displayArrayElements(); // adding element at index 2 array.addElementAtPosition(2, 60); System.out.println("Size of myArray =>" + array.displaySize() + " and Capacity :" + array.displayCapacity()); array.displayArrayElements(); } }
出力:
説明:
コード:
package com.dynamicarray; import java.util.Arrays; public class DynamicArray { // declaring an array int myArray[]; // stores the present size of the array int sizeOfMyArray; // stores total capacity of an array int arrayCapacity; // initializing array, size and capacity public DynamicArray() { myArray = new int[2]; sizeOfMyArray = 0; arrayCapacity = 2; } // method for adding elements public void addElementsToArray(int element) { // makes the capacity double if all the array elements filled if (sizeOfMyArray == arrayCapacity) { increaseCapacity(2); } myArray[sizeOfMyArray] = element; sizeOfMyArray++; } // method for adding elements to specific position public void addElementAtPosition(int position, int value) { // makes the capacity double if all the array elements filled if (sizeOfMyArray == arrayCapacity) { increaseCapacity(2); } // shifting array elements for (int p = sizeOfMyArray - 1; p >= position; p--) { myArray[p + 1] = myArray[p]; } // adding the element at specific position myArray[position] = value; sizeOfMyArray++; } // method for getting the element from specific position public int getElementAtposition(int position) { return myArray[position]; } // method for removing elements public void removeAtPosition(int position) { if (position >= sizeOfMyArray || position < 0) { System.out.println("Opps!No elements found " + position + " position"); } else { for (int p = position; p < sizeOfMyArray - 1; p++) { myArray[p] = myArray[p + 1]; } myArray[sizeOfMyArray - 1] = 0; sizeOfMyArray--; } } // method for increasing capacity if all the elements in an array filled public void increaseCapacity(int minimumCapacity) { int temp[] = new int[arrayCapacity * minimumCapacity]; for (int p = 0; p < arrayCapacity; p++) { temp[p] = myArray[p]; } myArray = temp; arrayCapacity = arrayCapacity * minimumCapacity; } // method for make an array size to initial size public void makeInitialSize() { System.out.println("Making an array to initial size"); int temp[] = new int[sizeOfMyArray]; for (int q = 0; q < sizeOfMyArray; q++) { temp[q] = myArray[q]; } myArray = temp; arrayCapacity = myArray.length; } // method for array current size public int displaySize() { return sizeOfMyArray; } // method for array total capacity public int displayCapacity() { return arrayCapacity; } // method for display all elements public void displayArrayElements() { System.out.println("elements in array are :" + Arrays.toString(myArray)); } public static void main(String[] args) { DynamicArray array = new DynamicArray(); System.out.println("==================================================================="); System.out.println("Inital array size " + array.sizeOfMyArray + " and initial capacity " + array.arrayCapacity); System.out.println("==================================================================="); array.addElementsToArray(10); array.addElementsToArray(20); array.addElementsToArray(30); array.addElementsToArray(40); array.displayArrayElements(); array.removeAtPosition(2); System.out.println("Size after Remove Operation=>" + array.displaySize() + " and Capacity :" + array.displayCapacity()); array.displayArrayElements(); array.removeAtPosition(2); System.out.println("Size after Remove Operation=>" + array.displaySize() + " and Capacity :" + array.displayCapacity()); array.displayArrayElements(); array.removeAtPosition(1); System.out.println("Size after Remove Operation=>" + array.displaySize() + " and Capacity :" + array.displayCapacity()); array.displayArrayElements(); array.removeAtPosition(2); System.out.println("Size after Remove Operation =>" + array.displaySize() + " and Capacity :" + array.displayCapacity()); array.displayArrayElements(); array.removeAtPosition(1); System.out.println("Size after Remove Operation =>" + array.displaySize() + " and Capacity :" + array.displayCapacity()); array.displayArrayElements(); // Make the array to initial size array.makeInitialSize(); System.out.println(" After trimming Size of myArray =>" + array.displaySize() + " and Capacity :" + array.displayCapacity()); array.displayArrayElements(); array.addElementsToArray(-5); System.out.println("After trimming Size of myArray =>" + array.displaySize() + " and Capacity :" + array.displayCapacity()); array.displayArrayElements(); array.addElementsToArray(-6); System.out.println("After trimming Size of myArray =>" + array.displaySize() + " and Capacity :" + array.displayCapacity()); array.displayArrayElements(); } }
出力:
ArrayList を使用した動的配列。
コード:
package com.dynamicarray; import java.util.ArrayList; import java.util.List; public class ArrayListDynamic { public static void main(String[] args) { List<Integer> list=new ArrayList<Integer>(); list.add(10); list.add(20); list.add(30); list.add(40); System.out.println("Adding the elements ArrayList =>"+list); System.out.println("Adding the elements ArrayList size =>"+list.size()); /*Array List capacity formula newCapacity = (oldCapacity * 3/2) + 1*/ list.add(4, 50); System.out.println("After adding the element at specific index =>"+list+" and size "+list.size()); list.remove(4); list.remove(3); System.out.println("After removing the elements =>"+list+" and size "+list.size()); } }
出力:
通常の動的配列では、実装開発者はカスタム ロジックを記述する必要がありますが、コレクション ArrayList では、事前定義されたすべてのメソッドが使用できるため、カスタム ロジックを記述する必要はありません。
以上がJavaの動的配列の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。