logo

Tablice TypeScriptu

Tablica to jednorodny zbiór elementów podobnego typu, które mają sąsiadującą lokalizację w pamięci.

Tablica jest typem danych zdefiniowanym przez użytkownika.

Tablica to rodzaj struktury danych, w której przechowujemy elementy o podobnym typie danych. W tablicy możemy przechowywać tylko ustalony zestaw elementów. Możemy go również używać jako przedmiotu.

Tablica jest pamięcią opartą na indeksach, gdzie pierwszy element jest przechowywany pod indeksem 0. Poniższa struktura pomaga zrozumieć strukturę tablicy.

Tablice TypeScriptu

Charakterystyka tablicy

  1. Tablica przechowuje elementy, które mają ten sam typ danych.
  2. Elementy tablicy przechowywane w sąsiadujących lokalizacjach pamięci.
  3. Przechowywanie elementów tablicy 2-D jest wiersz po wierszu w ciągłej lokalizacji pamięci.
  4. Nazwa tablicy reprezentuje adres elementu początkowego.
  5. Rozmiar tablicy należy zainicjować w momencie deklaracji.
  6. Rozmiar tablicy powinien być wyrażeniem stałym, a nie zmienną.
  7. Możemy pobrać elementy tablicy, określając odpowiednią wartość indeksu elementu.

Korzyść

Optymalizacja kodu: Tablica pomaga zoptymalizować kod, co zwiększa szybkość i wydajność programu. Pozwala nam to efektywniej pobierać lub sortować dane tablicowe.

Losowy dostęp: Zapewnia możliwość dostępu do dowolnych danych tablicy w stałym czasie (niezależnie od jej położenia i rozmiaru). W ten sposób możemy bezpośrednio uzyskać dowolne dane z tablicy znajdującej się w dowolnej pozycji indeksu.

Niekorzyść

Limit rozmiaru: Tablica pozwala nam przechowywać tylko ustaloną liczbę elementów. Po zadeklarowaniu tablicy nie możemy zmienić jej rozmiaru. Jeśli więc chcemy wstawić więcej elementów niż zadeklarowano, nie jest to możliwe.

Deklaracja tablicy

Podobnie jak JavaScript, TypeScript obsługuje również tablice. Istnieją dwa sposoby zadeklarowania tablicy:

1. Używanie nawiasów kwadratowych.

 let array_name[:datatype] = [val1,val2,valn..] 

Przykład:

 let fruits: string[] = ['Apple', 'Orange', 'Banana']; 

2. Używanie ogólnego typu tablicy.

jak wyłączyć tryb programisty w Androidzie
 let array_name: Array = [val1,val2,valn..] 

Przykład:

 let fruits: Array = ['Apple', 'Orange', 'Banana']; 

Typy tablicy w TypeScript

Istnieją dwa typy tablicy:

  1. Tablica jednowymiarowa
  2. Tablica wielowymiarowa
Tablice TypeScriptu

Tablica jednowymiarowa

Tablica jednowymiarowa to rodzaj tablicy liniowej, która zawiera tylko jeden wiersz do przechowywania danych. Zawiera pojedynczy zestaw nawiasów kwadratowych („[]”). Dostęp do jego elementów możemy uzyskać za pomocą indeksu wiersza lub kolumny.

Składnia

 let array_name[:datatype]; 

Inicjalizacja

 array_name = [val1,val2,valn..] 

Przykład

 let arr:number[]; arr = [1, 2, 3, 4] console.log('Array[0]: ' +arr[0]); console.log('Array[1]: ' +arr[1]); 

Wyjście:

 Array[0]: 1 Array[1]: 2 

Tablica wielowymiarowa

Tablica wielowymiarowa to tablica zawierająca jedną lub więcej tablic. W tablicy wielowymiarowej dane są przechowywane w indeksie opartym na wierszach i kolumnach (znanym również jako forma macierzowa). Tablica dwuwymiarowa (tablica 2-D) jest najprostszą formą tablicy wielowymiarowej.

Tablice TypeScriptu

Składnia

 let arr_name:datatype[][] = [ [a1,a2,a3], [b1,b2,b3] ]; 

Inicjalizacja

 let arr_name:datatype[initial_array_index][referenced_array_index] = [ [val1,val2,val 3], [v1,v2,v3]]; 

Przykład

 var mArray:number[][] = [[1,2,3],[5,6,7]] ; console.log(mArray[0][0]); console.log(mArray[0][1]); console.log(mArray[0][2]); console.log(); console.log(mArray[1][0]); console.log(mArray[1][1]); console.log(mArray[1][2]); 

Wyjście:

 1 2 3 5 6 7 

Obiekt tablicowy

Obiekty Array pozwalają nam przechowywać wiele wartości w jednej zmiennej. Tablicę możemy utworzyć za pomocą obiektu Array. Konstruktor Array służy do przekazywania następujących argumentów podczas tworzenia tablicy.

  • Wartość liczbowa reprezentująca rozmiar tablicy lub
  • Lista wartości oddzielonych przecinkami.

Składnia

 let arr_name:datatype[] = new Array(values); 

Przykład

 //array by using the Array object. let arr:string[] = new Array(&apos;JavaTpoint&apos;,&apos;2200&apos;,&apos;Java&apos;,&apos;Abhishek&apos;); for(var i = 0;i <arr.length;i++) { console.log(arr[i]); } < pre> <p> <strong>Output:</strong> </p> <pre> JavaTpoint 2200 Java Abhishek </pre> <h3>Array Traversal by using a for...in loop</h3> <p> <strong>Example</strong> </p> <pre> let i:any; let arr:string[] = [&apos;JavaTpoint&apos;, &apos;2300&apos;, &apos;Java&apos;, &apos;Abhishek&apos;]; for(i in arr) { console.log(arr[i]) } </pre> <p> <strong>Output:</strong> </p> <pre> JavaTpoint 2300 Java Abhishek </pre> <h3>Passing Arrays to Functions</h3> <p>We can pass arrays to functions by specifying the array name without an index.</p> <p> <strong>Example</strong> </p> <pre> let arr:string[] = new Array(&apos;JavaTpoint&apos;, &apos;2300&apos;, &apos;Java&apos;, &apos;Abhishek&apos;); //Passing arrays in function function display(arr_values:string[]) { for(let i = 0;i <arr_values.length;i++) { console.log(arr[i]); } calling arrays in function display(arr); < pre> <p> <strong>Output:</strong> </p> <pre> JavaTpoint 2300 Java Abhishek </pre> <hr> <h2>TypeScript Spread operator</h2> <p>The spread operator is used to initialize arrays and objects from another array or object. We can also use it for object de-structuring. It is a part of the ES 6 version.</p> <p> <strong>Example</strong> </p> <pre> let arr1 = [ 1, 2, 3]; let arr2 = [ 4, 5, 6]; //Create new array from existing array let copyArray = [...arr1]; console.log(&apos;CopiedArray: &apos; +copyArray); //Create new array from existing array with more elements let newArray = [...arr1, 7, 8]; console.log(&apos;NewArray: &apos; +newArray); //Create array by merging two arrays let mergedArray = [...arr1, ...arr2]; console.log(&apos;MergedArray: &apos; +mergedArray); </pre> <p> <strong>Output:</strong> </p> <pre> CopiedArray: 1,2,3 NewArray: 1,2,3,7,8 MergedArray: 1,2,3,4,5,6 </pre> <hr> <h2>Array Methods</h2> <p>The list of array methods with their description is given below.</p> <table class="table"> <tr> <th>SN</th> <th>Method</th> <th>Description</th> </tr> <tr> <td>1.</td> <td>concat()</td> <td>It is used to joins two arrays and returns the combined result.</td> </tr> <tr> <td>2.</td> <td>copyWithin()</td> <td>It copies a sequence of an element within the array.</td> </tr> <tr> <td>3.</td> <td>every()</td> <td>It returns true if every element in the array satisfies the provided testing function.</td> </tr> <tr> <td>4.</td> <td>fill()</td> <td>It fills an array with a static value from the specified start to end index.</td> </tr> <tr> <td>5.</td> <td>indexOf()</td> <td>It returns the index of the matching element in the array, otherwise -1.</td> </tr> <tr> <td>6.</td> <td>includes()</td> <td>It is used to check whether the array contains a certain element or not.</td> </tr> <tr> <td>7.</td> <td>Join()</td> <td>It is used to joins all elements of an array into a string.</td> </tr> <tr> <td>8.</td> <td>lastIndexOf()</td> <td>It returns the last index of an element in the array.</td> </tr> <tr> <td>9.</td> <td>Pop()</td> <td>It is used to removes the last elements of the array.</td> </tr> <tr> <td>10.</td> <td>Push()</td> <td>It is used to add new elements to the array.</td> </tr> <tr> <td>11.</td> <td>reverse()</td> <td>It is used to reverse the order of an element in the array.</td> </tr> <tr> <td>12.</td> <td>Shift()</td> <td>It is used to removes and returns the first element of an array.</td> </tr> <tr> <td>13.</td> <td>slice()</td> <td>It returns the section fo an array in the new array.</td> </tr> <tr> <td>14.</td> <td>sort()</td> <td>It is used to sort the elements of an array.</td> </tr> <tr> <td>15.</td> <td>splice()</td> <td>It is used to add or remove the elements from an array.</td> </tr> <tr> <td>16.</td> <td>toString()</td> <td>It returns the string representation of an array.</td> </tr> <tr> <td>17.</td> <td>unshift()</td> <td>It is used to add one or more elements to the beginning of an array.</td> </tr> </table></arr_values.length;i++)></pre></arr.length;i++)>

Przechodzenie po tablicy za pomocą pętli for...in

Przykład

 let i:any; let arr:string[] = [&apos;JavaTpoint&apos;, &apos;2300&apos;, &apos;Java&apos;, &apos;Abhishek&apos;]; for(i in arr) { console.log(arr[i]) } 

Wyjście:

 JavaTpoint 2300 Java Abhishek 

Przekazywanie tablic do funkcji

Możemy przekazywać tablice do funkcji, podając nazwę tablicy bez indeksu.

Przykład

wady bankowości internetowej
 let arr:string[] = new Array(&apos;JavaTpoint&apos;, &apos;2300&apos;, &apos;Java&apos;, &apos;Abhishek&apos;); //Passing arrays in function function display(arr_values:string[]) { for(let i = 0;i <arr_values.length;i++) { console.log(arr[i]); } calling arrays in function display(arr); < pre> <p> <strong>Output:</strong> </p> <pre> JavaTpoint 2300 Java Abhishek </pre> <hr> <h2>TypeScript Spread operator</h2> <p>The spread operator is used to initialize arrays and objects from another array or object. We can also use it for object de-structuring. It is a part of the ES 6 version.</p> <p> <strong>Example</strong> </p> <pre> let arr1 = [ 1, 2, 3]; let arr2 = [ 4, 5, 6]; //Create new array from existing array let copyArray = [...arr1]; console.log(&apos;CopiedArray: &apos; +copyArray); //Create new array from existing array with more elements let newArray = [...arr1, 7, 8]; console.log(&apos;NewArray: &apos; +newArray); //Create array by merging two arrays let mergedArray = [...arr1, ...arr2]; console.log(&apos;MergedArray: &apos; +mergedArray); </pre> <p> <strong>Output:</strong> </p> <pre> CopiedArray: 1,2,3 NewArray: 1,2,3,7,8 MergedArray: 1,2,3,4,5,6 </pre> <hr> <h2>Array Methods</h2> <p>The list of array methods with their description is given below.</p> <table class="table"> <tr> <th>SN</th> <th>Method</th> <th>Description</th> </tr> <tr> <td>1.</td> <td>concat()</td> <td>It is used to joins two arrays and returns the combined result.</td> </tr> <tr> <td>2.</td> <td>copyWithin()</td> <td>It copies a sequence of an element within the array.</td> </tr> <tr> <td>3.</td> <td>every()</td> <td>It returns true if every element in the array satisfies the provided testing function.</td> </tr> <tr> <td>4.</td> <td>fill()</td> <td>It fills an array with a static value from the specified start to end index.</td> </tr> <tr> <td>5.</td> <td>indexOf()</td> <td>It returns the index of the matching element in the array, otherwise -1.</td> </tr> <tr> <td>6.</td> <td>includes()</td> <td>It is used to check whether the array contains a certain element or not.</td> </tr> <tr> <td>7.</td> <td>Join()</td> <td>It is used to joins all elements of an array into a string.</td> </tr> <tr> <td>8.</td> <td>lastIndexOf()</td> <td>It returns the last index of an element in the array.</td> </tr> <tr> <td>9.</td> <td>Pop()</td> <td>It is used to removes the last elements of the array.</td> </tr> <tr> <td>10.</td> <td>Push()</td> <td>It is used to add new elements to the array.</td> </tr> <tr> <td>11.</td> <td>reverse()</td> <td>It is used to reverse the order of an element in the array.</td> </tr> <tr> <td>12.</td> <td>Shift()</td> <td>It is used to removes and returns the first element of an array.</td> </tr> <tr> <td>13.</td> <td>slice()</td> <td>It returns the section fo an array in the new array.</td> </tr> <tr> <td>14.</td> <td>sort()</td> <td>It is used to sort the elements of an array.</td> </tr> <tr> <td>15.</td> <td>splice()</td> <td>It is used to add or remove the elements from an array.</td> </tr> <tr> <td>16.</td> <td>toString()</td> <td>It returns the string representation of an array.</td> </tr> <tr> <td>17.</td> <td>unshift()</td> <td>It is used to add one or more elements to the beginning of an array.</td> </tr> </table></arr_values.length;i++)>

Operator TypeScript Spread

Operator rozprzestrzeniania służy do inicjowania tablic i obiektów z innej tablicy lub obiektu. Możemy go również wykorzystać do destrukturyzacji obiektów. Jest częścią wersji ES 6.

Przykład

 let arr1 = [ 1, 2, 3]; let arr2 = [ 4, 5, 6]; //Create new array from existing array let copyArray = [...arr1]; console.log(&apos;CopiedArray: &apos; +copyArray); //Create new array from existing array with more elements let newArray = [...arr1, 7, 8]; console.log(&apos;NewArray: &apos; +newArray); //Create array by merging two arrays let mergedArray = [...arr1, ...arr2]; console.log(&apos;MergedArray: &apos; +mergedArray); 

Wyjście:

 CopiedArray: 1,2,3 NewArray: 1,2,3,7,8 MergedArray: 1,2,3,4,5,6 

Metody tablicowe

Poniżej znajduje się lista metod tablicowych wraz z ich opisem.

SN metoda Opis
1. połączyć() Służy do łączenia dwóch tablic i zwraca połączony wynik.
2. kopiujWin() Kopiuje sekwencję elementu w tablicy.
3. każdy() Zwraca wartość true, jeśli każdy element tablicy spełnia podaną funkcję testującą.
4. wypełnić() Wypełnia tablicę wartością statyczną z określonego indeksu od początku do końca.
5. indeks() Zwraca indeks pasującego elementu w tablicy, w przeciwnym razie -1.
6. zawiera() Służy do sprawdzania, czy tablica zawiera określony element, czy nie.
7. Dołączyć() Służy do łączenia wszystkich elementów tablicy w ciąg znaków.
8. ostatniIndeksOf() Zwraca ostatni indeks elementu w tablicy.
9. Muzyka pop() Służy do usuwania ostatnich elementów tablicy.
10. Naciskać() Służy do dodawania nowych elementów do tablicy.
jedenaście. odwracać() Służy do odwrócenia kolejności elementów w tablicy.
12. Zmiana() Służy do usuwania i zwracania pierwszego elementu tablicy.
13. plasterek() Zwraca sekcję tablicy w nowej tablicy.
14. sortować() Służy do sortowania elementów tablicy.
piętnaście. splatać() Służy do dodawania lub usuwania elementów z tablicy.
16. doString() Zwraca ciąg reprezentujący tablicę.
17. cofnij przesunięcie() Służy do dodania jednego lub większej liczby elementów na początku tablicy.