logo

Statiskais masīvs Java valodā

Java valodā, masīvs ir vissvarīgākā datu struktūra, kas satur viena veida elementus. Tas saglabā elementus blakus esošās atmiņas sadalījumā. Ir divu veidu masīvs t.i. statiskais masīvs un dinamiskais masīvs. Šajā sadaļā mēs koncentrēsimies tikai uz statiskais masīvs Java .

ir modeļu piemēri

Statiskais masīvs

Masīvu, kas tiek deklarēts ar statisko atslēgvārdu, sauc par statisku masīvu. Tā kompilēšanas laikā piešķir atmiņu, kuras lielums ir fiksēts. Mēs nevaram mainīt statisko masīvu.

Ja mēs vēlamies, lai masīva lielums būtu balstīts uz lietotāja ievadīto informāciju, mēs nevaram izmantot statiskus masīvus. Šādā gadījumā dinamiskie masīvi ļauj norādīt masīva lielumu izpildes laikā.

Statiskā masīva piemērs

Piemēram, int arr[10] izveido masīvu ar izmēru 10. Tas nozīmē, ka varam ievietot tikai 10 elementus; mēs nevaram pievienot 11. elementu, jo masīva lielums ir fiksēts.

 int arr[] = { 1, 3, 4 }; // static integer array int* arr = new int[3]; // dynamic integer array 

Statiskā masīva priekšrocības

  • Tam ir efektīvs izpildes laiks.
  • Statiskās piešķiršanas kalpošanas laiks ir viss programmas darbības laiks.

Statiskā masīva trūkumi

  • Ja tiek deklarēta vairāk statiskās datu vietas, nekā nepieciešams, tiek iztērēta vieta.
  • Gadījumā, ja tiek deklarēta mazāk statiskās vietas nekā nepieciešams, izpildes laikā kļūst neiespējami paplašināt šo fiksēto izmēru.

Statiskā masīva deklarēšana

Sintakse statiskā masīva deklarēšanai ir:

 []={,,.....}; 

Piemēram:

attēlu izlīdzināšana css
 String[] suit = new String[] { 'Japan', 'India', 'Austria', 'Dubai' }; 

Mēs varam arī deklarēt un inicializēt statisko masīvu šādi:

 String[] suit = { 'Japan', 'India', 'Austria', 'Dubai' }; 

Statisko masīvu var arī deklarēt kā sarakstu. Piemēram:

 List suit = Arrays.asList( 'Japan', 'India', 'Austria', 'Dubai' ); 

Statiskā masīva Java programma

StaticArrayExample.java

 public class StaticArrayExample { private static String[] array; static { array = new String[2]; array[0] = &apos;Welcome to&apos;; array[1] = &apos;Javatpoint&apos;; } public static void main(String args[]) { for(int i = 0; i <array.length; i++) { system.out.print(array[i] + ' '); } < pre> <p> <strong>Output:</strong> </p> <pre> Welcome to Javatpoint </pre> <p>Let&apos;s see another Java program.</p> <p> <strong>StaticArrayExample.java</strong> </p> <pre> public class StaticArrayExample2 { //creates a static array of integer type static Integer[] integerArray; static { integerArray = new Integer[] { new Integer(1), new Integer(2), new Integer(3), new Integer(4), new Integer(5)}; } public static void main(String args[]) { //loop iterate over static array for (int i = 0; i <integerarray.length; i++) { prints array elements system.out.println(integerarray[i]); } < pre> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 5 </pre> <h2>Difference Between Static Array and Dynamic Array</h2> <p>The following table describes the key differences between static array and dynamic array.</p> <table class="table"> <tr> <th>Static Array</th> <th>Dynamic Array</th> </tr> <tr> <td>Static arrays are allocated memory at compile time.</td> <td>Dynamic array is located at run-time.</td> </tr> <tr> <td>The size of static array is fixed.</td> <td>The size of dynamic array is fixed. </td> </tr> <tr> <td>It is located in stack memory space.</td> <td>It is located in heap memory space.</td> </tr> <tr> <td>int array[10]; //array of size 10</td> <td>int* array = new int[10];</td> </tr> </table> <hr></integerarray.length;></pre></array.length;>

Apskatīsim citu Java programmu.

StaticArrayExample.java

 public class StaticArrayExample2 { //creates a static array of integer type static Integer[] integerArray; static { integerArray = new Integer[] { new Integer(1), new Integer(2), new Integer(3), new Integer(4), new Integer(5)}; } public static void main(String args[]) { //loop iterate over static array for (int i = 0; i <integerarray.length; i++) { prints array elements system.out.println(integerarray[i]); } < pre> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 5 </pre> <h2>Difference Between Static Array and Dynamic Array</h2> <p>The following table describes the key differences between static array and dynamic array.</p> <table class="table"> <tr> <th>Static Array</th> <th>Dynamic Array</th> </tr> <tr> <td>Static arrays are allocated memory at compile time.</td> <td>Dynamic array is located at run-time.</td> </tr> <tr> <td>The size of static array is fixed.</td> <td>The size of dynamic array is fixed. </td> </tr> <tr> <td>It is located in stack memory space.</td> <td>It is located in heap memory space.</td> </tr> <tr> <td>int array[10]; //array of size 10</td> <td>int* array = new int[10];</td> </tr> </table> <hr></integerarray.length;>

Atšķirība starp statisko masīvu un dinamisko masīvu

Šajā tabulā ir aprakstītas galvenās atšķirības starp statisko masīvu un dinamisko masīvu.

gudra datorvaloda
Statiskais masīvs Dinamiskais masīvs
Statiskajiem masīviem tiek piešķirta atmiņa kompilēšanas laikā. Dinamiskais masīvs atrodas izpildes laikā.
Statiskā masīva lielums ir fiksēts. Dinamiskā masīva lielums ir fiksēts.
Tas atrodas steka atmiņas vietā. Tas atrodas kaudzes atmiņas vietā.
int masīvs[10]; //10. izmēra masīvs int* masīvs = jauns int[10];