Niz je fiksne veličine, homogen struktura podataka . Ograničenje nizova je da su fiksne veličine. To znači da pri deklaraciji niza moramo navesti broj elemenata. Ovdje se postavlja pitanje što ako želimo umetnuti element, a nema više mjesta za novi element? Evo, koncept dinamički niz dolazi u postojanje. Dinamički povećava veličinu niza.
U ovom odjeljku ćemo razumjeti što je dinamički niz, značajke dinamičkog niza, kako promijeniti veličinu dinamičkog niza, i kako implementirati dinamički niz u Javi .
Što je dinamički niz?
Dinamički niz je a promjenjiva veličina struktura podataka popisa. Automatski raste kada pokušamo umetnuti element ako više nema mjesta za novi element. Omogućuje nam dodavanje i uklanjanje elemenata. Dodjeljuje memoriju za vrijeme izvođenja pomoću gomile. Može promijeniti svoju veličinu tijekom rada.
U Java , ArrayList je implementacija promjenjive veličine. Implementira sučelje popisa i pruža sve metode povezane s operacijama popisa. Snaga dinamičkog niza je:
- Brzo traženje
- Varijabilna veličina
- Prijatelj predmemorije
Rad dinamičkog niza
U dinamičkom nizu, elementi se pohranjuju kontinuirano od početka niza, a preostali prostor ostaje neiskorišten. Elemente možemo dodavati dok se rezervirani razmak u potpunosti ne potroši. Kada se rezervirani prostor potroši i potrebno je dodati neke elemente. U tom slučaju, polje fiksne veličine mora se povećati. Imajte na umu da prije dodavanja elementa dodjeljujemo veći niz, kopiramo elemente iz niza i vraćamo novostvoreni niz.
Drugi način dodavanja elementa je da prvo stvorite funkciju koja stvara novi niz dvostruke veličine, kopira sve elemente iz starog niza i vraća novi niz. Slično, također možemo smanjiti veličinu dinamičkog niza.
Veličina u odnosu na kapacitet
Inicijalizacija dinamičkog niza stvara niz fiksne veličine. Na sljedećoj slici implementacija polja ima 10 indeksa. Dodali smo pet elemenata nizu. Sada, temeljni niz ima duljinu pet. Stoga je duljina veličine dinamičkog niza 5, a njegov kapacitet 10. Dinamički niz prati krajnju točku.
Značajke Dynamic Array
U Javi dinamički niz ima tri ključne značajke: Dodajte element, izbrišite element i promijenite veličinu niza.
Dodajte element u dinamički niz
U dinamičkom nizu možemo stvoriti niz fiksne veličine ako želimo dodati još neke elemente u niz. Obično stvara novo polje dvostruke veličine. Nakon toga kopira sve elemente u novonastali niz. Koristimo sljedeći pristup:
Brisanje elementa iz dinamičkog niza
Ako želimo ukloniti element iz niza na navedenom indeksu, koristimo uklonitiNa(i) metoda. Metoda analizira indeksni broj tog elementa koji želimo obrisati. Nakon brisanja elementa, pomiče preostale elemente (elemente koji su desno od izbrisanog elementa) ulijevo od navedenog indeksnog broja. Također koristimo metodu remove() koja briše element s kraja niza. Nakon pomicanja elemenata, pohranjuje 0 u palači posljednjeg elementa. Shvatimo to kroz primjer, kao što smo pokazali na sljedećoj slici.
Promjena veličine dinamičkog polja u Javi
Moramo promijeniti veličinu niza u dva scenarija ako:
- Niz koristi dodatnu memoriju od potrebne.
- Niz zauzima svu memoriju i moramo dodati elemente.
U prvom slučaju koristimo srinkSize() metoda za promjenu veličine niz . Smanjuje veličinu niza. Oslobađa dodatnu ili neiskorištenu memoriju. U drugom slučaju koristimo growSize() metoda za promjenu veličine niza. Povećava veličinu niza.
To je skupa operacija jer zahtijeva veći niz i kopira sve elemente iz prethodnog niza nakon čega vraća novi niz.
Pretpostavimo da je u gornjem nizu potrebno dodati još šest elemenata i da u nizu nema više memorije za pohranu elemenata. U takvim slučajevima rastemo niz pomoću growSize() metoda.
Inicijalizirajte dinamički niz
Inicijalizacija dinamičkog niza ista je kao i statičkog niza. Razmotrite sljedeći Java program koji inicijalizira dinamički niz.
InitializeDynamicArray.java
public class InitializeDynamicArray { public static void main(String[] args) { //declaring array int array[]; //initialize an array array= new int[6]; //adding elements to the array array[0] = 34; array[1] = 90; array[2] = 12; array[3] = 22; array[4] = 9; array[5] = 27; System.out.print('Elements of Array are: '); //iteraton over the array for(int i=0; i <array.length ; i++) { system.out.print(array[i] +' '); } < pre> <p> <strong>Output:</strong> </p> <pre> Elements of Array are: 34 90 12 22 9 27 </pre> <p>Let's implement the operations in a Java program that we have discussed above.</p> <p> <strong>DynamicArrayExample1.java</strong> </p> <pre> public class DynamicArrayExample1 { private int array[]; private int count; private int sizeofarray; //creating a constructor of the class that initializes the values public DynamicArrayExample1() { array = new int[1]; count = 0; sizeofarray = 1; } //creating a function that appends an element at the end of the array public void addElement(int a) { //compares if the number of elements is equal to the size of the array or not if (count == sizeofarray) { //invoking the growSize() method that creates an array of double size growSize(); } //appens an element at the end of the array array[count] = a; count++; } //function that creates an array of double size public void growSize() { //declares a temp[] array int temp[] = null; if (count == sizeofarray) { //initialize a double size array of array temp = new int[sizeofarray * 2]; { for (int i = 0; i <sizeofarray; i++) { copies all the elements of old array temp[i]="array[i];" } sizeofarray="sizeofarray" * 2; creating a function that deletes an element at specified index public void addelementat(int index, int a) compare size with number if not equal grows (count="=" sizeofarray) invoking growsize() method growsize(); for (int i="count" - 1;>= index; i--) { //shifting all the elements to the left from the specified index array[i + 1] = array[i]; } //inserts an element at the specified index array[index] = a; count++; } public static void main(String[] args) { DynamicArrayExample1 da = new DynamicArrayExample1(); //adding elements to the array da.addElement(12); da.addElement(22); da.addElement(35); da.addElement(47); da.addElement(85); da.addElement(26); da.addElement(70); da.addElement(81); da.addElement(96); da.addElement(54); System.out.println('Elements of the array:'); //iterate over the array for accessing the elements for (int i = 0; i <da.sizeofarray; 5 99 i++) { system.out.print(da.array[i] + ' '); } system.out.println(); determines and prints the size number of elements array system.out.println('size array: da.sizeofarray); system.out.println('no. in da.count); invoking method to add an element at specified index da.addelementat(5, 99); where is be system.out.println(' elements after adding 5:'); iterate over for accessing (int i="0;" < da.sizeofarray; pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/02/dynamic-array-java-6.webp" alt="Dynamic Array in Java"> <p>Let's shrink the array, delete the last element, and a specified element from the array.</p> <p> <strong>DynamicArrayExample2.java</strong> </p> <pre> public class DynamicArrayExample2 { private int array[]; private int count; private int sizeofarray; //creating a constructor of the class that initializes the values public DynamicArrayExample2() { array = new int[1]; count = 0; sizeofarray = 1; } //creating a function that appends an element at the end of the array public void addElement(int a) { //compares if the number of elements is equal to the size of the array or not if (count == sizeofarray) { //invoking the growSize() method that creates an array of double size growSize(); } //appens an element at the end of the array array[count] = a; count++; } //function that creates an array of double size public void growSize() { //declares a temp[] array int temp[] = null; if (count == sizeofarray) { //initialize a double size array of array temp = new int[sizeofarray * 2]; { for (int i = 0; i <sizeofarray; i++) { copies all the elements of old array temp[i]="array[i];" } sizeofarray="sizeofarray" * 2; method removes unused space public void shrinksize() declares a temp[] int if (count> 0) { //creates an array of the size equal to the count i.e. number of elements the array have temp = new int[count]; for (int i = 0; i <count; i++) { copies all the elements of old array temp[i]="array[i];" } sizeofarray="count;" creating a function that removes last for public void removeelement() if (count> 0) { array[count - 1] = 0; count--; } } //creating a function that delets an element from the specified index public void removeElementAt(int index) { if (count > 0) { for (int i = index; i <count 7 - 1; i++) { shifting all the elements to left from specified index array[i]="array[i" + 1]; } array[count 1]="0;" count--; public static void main(string[] args) dynamicarrayexample2 da="new" dynamicarrayexample2(); adding array da.addelement(12); da.addelement(22); da.addelement(35); da.addelement(47); da.addelement(85); da.addelement(26); da.addelement(70); da.addelement(81); da.addelement(96); da.addelement(54); system.out.println('elements of array:'); iterate over for accessing (int i="0;" < da.sizeofarray; system.out.print(da.array[i] ' '); system.out.println(); determines and prints size number system.out.println('size array: da.sizeofarray); system.out.println('no. in da.count); invoking method delete last element da.removeelement(); after deleting system.out.print(' elements element: system.out.print('no. da.count+' '); that deletes an da.removeelementat(7); at 7: pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/02/dynamic-array-java-7.webp" alt="Dynamic Array in Java"> <hr></count></count;></sizeofarray;></pre></da.sizeofarray;></sizeofarray;></pre></array.length>
Implementirajmo operacije u Java programu o kojima smo raspravljali gore.
DynamicArrayExample1.java
public class DynamicArrayExample1 { private int array[]; private int count; private int sizeofarray; //creating a constructor of the class that initializes the values public DynamicArrayExample1() { array = new int[1]; count = 0; sizeofarray = 1; } //creating a function that appends an element at the end of the array public void addElement(int a) { //compares if the number of elements is equal to the size of the array or not if (count == sizeofarray) { //invoking the growSize() method that creates an array of double size growSize(); } //appens an element at the end of the array array[count] = a; count++; } //function that creates an array of double size public void growSize() { //declares a temp[] array int temp[] = null; if (count == sizeofarray) { //initialize a double size array of array temp = new int[sizeofarray * 2]; { for (int i = 0; i <sizeofarray; i++) { copies all the elements of old array temp[i]="array[i];" } sizeofarray="sizeofarray" * 2; creating a function that deletes an element at specified index public void addelementat(int index, int a) compare size with number if not equal grows (count="=" sizeofarray) invoking growsize() method growsize(); for (int i="count" - 1;>= index; i--) { //shifting all the elements to the left from the specified index array[i + 1] = array[i]; } //inserts an element at the specified index array[index] = a; count++; } public static void main(String[] args) { DynamicArrayExample1 da = new DynamicArrayExample1(); //adding elements to the array da.addElement(12); da.addElement(22); da.addElement(35); da.addElement(47); da.addElement(85); da.addElement(26); da.addElement(70); da.addElement(81); da.addElement(96); da.addElement(54); System.out.println('Elements of the array:'); //iterate over the array for accessing the elements for (int i = 0; i <da.sizeofarray; 5 99 i++) { system.out.print(da.array[i] + \' \'); } system.out.println(); determines and prints the size number of elements array system.out.println(\'size array: da.sizeofarray); system.out.println(\'no. in da.count); invoking method to add an element at specified index da.addelementat(5, 99); where is be system.out.println(\' elements after adding 5:\'); iterate over for accessing (int i="0;" < da.sizeofarray; pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/02/dynamic-array-java-6.webp" alt="Dynamic Array in Java"> <p>Let's shrink the array, delete the last element, and a specified element from the array.</p> <p> <strong>DynamicArrayExample2.java</strong> </p> <pre> public class DynamicArrayExample2 { private int array[]; private int count; private int sizeofarray; //creating a constructor of the class that initializes the values public DynamicArrayExample2() { array = new int[1]; count = 0; sizeofarray = 1; } //creating a function that appends an element at the end of the array public void addElement(int a) { //compares if the number of elements is equal to the size of the array or not if (count == sizeofarray) { //invoking the growSize() method that creates an array of double size growSize(); } //appens an element at the end of the array array[count] = a; count++; } //function that creates an array of double size public void growSize() { //declares a temp[] array int temp[] = null; if (count == sizeofarray) { //initialize a double size array of array temp = new int[sizeofarray * 2]; { for (int i = 0; i <sizeofarray; i++) { copies all the elements of old array temp[i]="array[i];" } sizeofarray="sizeofarray" * 2; method removes unused space public void shrinksize() declares a temp[] int if (count> 0) { //creates an array of the size equal to the count i.e. number of elements the array have temp = new int[count]; for (int i = 0; i <count; i++) { copies all the elements of old array temp[i]="array[i];" } sizeofarray="count;" creating a function that removes last for public void removeelement() if (count> 0) { array[count - 1] = 0; count--; } } //creating a function that delets an element from the specified index public void removeElementAt(int index) { if (count > 0) { for (int i = index; i <count 7 - 1; i++) { shifting all the elements to left from specified index array[i]="array[i" + 1]; } array[count 1]="0;" count--; public static void main(string[] args) dynamicarrayexample2 da="new" dynamicarrayexample2(); adding array da.addelement(12); da.addelement(22); da.addelement(35); da.addelement(47); da.addelement(85); da.addelement(26); da.addelement(70); da.addelement(81); da.addelement(96); da.addelement(54); system.out.println(\'elements of array:\'); iterate over for accessing (int i="0;" < da.sizeofarray; system.out.print(da.array[i] \' \'); system.out.println(); determines and prints size number system.out.println(\'size array: da.sizeofarray); system.out.println(\'no. in da.count); invoking method delete last element da.removeelement(); after deleting system.out.print(\' elements element: system.out.print(\'no. da.count+\' \'); that deletes an da.removeelementat(7); at 7: pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/02/dynamic-array-java-7.webp" alt="Dynamic Array in Java"> <hr></count></count;></sizeofarray;></pre></da.sizeofarray;></sizeofarray;>