Matrice frastagliata - Jagged array
In informatica , un array irregolare , noto anche come array frastagliato , è un array di array i cui array membri possono essere di lunghezze diverse, producendo righe di bordi frastagliati quando visualizzati come output. Al contrario, gli array bidimensionali sono sempre rettangolari, quindi gli array frastagliati non devono essere confusi con gli array multidimensionali , ma il primo è spesso usato per emulare il secondo.
Array di array in linguaggi come Java, PHP, Python (liste multidimensionali), Ruby, C#.Net, Visual Basic.NET , Perl, JavaScript, Objective-C, Swift e Atlas Autocode sono implementati come vettori Iliffe .
Esempi
In C# e Java è possibile creare array frastagliati con il seguente codice:
int[][]c;
c = new int[2][]; // creates 2 rows
c[0] = new int[5]; // 5 columns for row 0
c[1] = new int[3]; // create 3 columns for row 1
In C e C++ , è possibile creare un array frastagliato (sullo stack) utilizzando il seguente codice:
int jagged_row0[] = {0,1};
int jagged_row1[] = {1,2,3};
int *jagged[] = { jagged_row0, jagged_row1 };
In C/C++, possono essere creati anche array frastagliati (sull'heap) con un array di puntatori:
int *jagged[5];
jagged[0] = malloc(sizeof(int) * 10);
jagged[1] = malloc(sizeof(int) * 3);
In C++/CLI , l'array frastagliato può essere creato con il codice:
using namespace System;
int main()
{
array<array<double> ^> ^ Arrayname = gcnew array <array<double> ^> (4); // array contains 4
//elements
return 0;
}
In Python , gli array frastagliati non sono nativi ma è possibile utilizzare le comprensioni di elenco per creare un elenco multidimensionale che supporti qualsiasi matrice dimensionale:
multi_list_3d = [[[] for i in range(3)] for i in range(3)]
# Produces: [[[], [], []], [[], [], []], [[], [], []]]
multi_list_5d = [[[] for i in range(5)] for i in range(5)]
# Produces: [[[], [], [], [], []], [[], [], [], [], []], [[], [], [], [], []], [[], [], [], [], []], [[], [], [], [], []]]
Guarda anche
Riferimenti
