martes, 29 de octubre de 2013

Arreglos de Arreglos?

agged Arrays.
Un arreglo de arreglos es un arreglo cuyos elementos son arreglos. Los elementos de un arreglo de arreglos pueden ser de diferentes dimensiones y tamaños.

Declaración y creación. En el manejo de un Jagged Array debe usarse un par de corchetes para cada dimensión:

<tipo> [ ] [ ] <identificador> = new <tipo>[ n ] [ ] ;

o

<tipo> [ ] [ ] <identificador> ;
.
.
.
<identificador> = new <tipo>[ n ] [ ] ;

Ejemplo:

int [ ] [ ] arregloIrregular = new int [ 3 ] [ ] ;

o

int [ ] [ ] arregloIrregular ;
arregloIrregular = new int [ 5 ] [ ] ;

Aquí se ha creado un arreglo unidimensional y se han declarado sus 5 elementos que son arreglos unidimensionales de números enteros.

Antes de poder utilizar el arregloIrregular, deben crearse sus elementos, por ejemplo:

arregloIrregular [0] = new int[3] ;
arregloIrregular [1] = new int[1] ;
arregloIrregular [2] = new int[5] ;
arregloIrregular [3] = new int[4] ;
arregloIrregular [4] = new int[2] ;

Esto es diferente al arreglo bidimensional:

int [ , ] arregloRegular = new int [5,5] ;

Inicialización.
Pueden inicializarse los elementos de un arreglo al mismo tiempo de su creación, como en el siguiente ejemplo:

arregloIrregular[0] = new int[ ] { 11, 33, 25 } ;
arregloIrregular[1] = new int[ ] { 26 } ;
arregloIrregular[2] = new int[ ] { 26, 5 , 34 , 23 , 78 } ;
arregloIrregular[3] = new int[ ] { 16, 67 , 34 , 21 } ;
arregloIrregular[4] = new int[ ] { 62, 85 } ;

O inicializarlos desde la creación, por ejemplo:

int [ ] [ ] arregloIrregular = new int [ ] [ ]
{
new int[ ] { 11, 33, 25 } ,
new int[ ] { 26 } ,
new int[ ] { 26, 5 , 34 , 23 , 78 } ,
new int[ ] { 16, 67 , 34 , 21 } ,
new int[ ] { 62, 85 }
} ;

o

int [ ] [ ] arregloIrregular = {
new int[ ] { 11, 33, 25 } ,
new int[ ] { 26 } ,
new int[ ] { 26, 5 , 34 , 23 , 78 } ,
new int[ ] { 16, 67 , 34 , 21 } ,
new int[ ] { 62, 85 }
} ;

Acceso.
Se puede acceder a los elementos individuales de un jagged array utilizando un subíndice dentro de cada par de corchetes.

elemento = arregloIrregular[ 2 ] [ 4 ] ; // elemento es igual a 78.
arregloIrregular[ 3 ] [ 1 ] = 23 ; // Sustituye el 67 por 23.

Mezcla de arreglos.
Es posible combinar jagged array y multidimensionales, cada uno con diferentes dimensiones.

int [ ] [ , ] mezcla = new int [  ] [ , ]

Ejemplo de un programa que manipula jagged arrays, llamado Arreglos2.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Arreglos2
{     class Program     {         static void Main(string[] args)         {             //jagged array de 5 x N donde N es un arreglo de distintos tamaños             int[][] arregloIrregular;             arregloIrregular = new int[5][];             arregloIrregular [0] = new int[3] ;             arregloIrregular [1] = new int[1] ;             arregloIrregular [2] = new int[5] ;             arregloIrregular [3] = new int[4] ;             arregloIrregular [4] = new int[2] ;             for (int i = 0; i < arregloIrregular.Length; i++)             {                 for (int j = 0; j < arregloIrregular[i].Length; j++)                 {                     System.Console.Write("Valor de Arreglo en posicion[" + i + "," + j + "]:");                     arregloIrregular[i][j] = System.Convert.ToInt32(System.Console.ReadLine());                 }             }             System.Console.WriteLine();             System.Console.WriteLine();             for (int i = 0; i < arregloIrregular.Length; i++)             {                 for (int j = 0; j < arregloIrregular[i].Length; j++)                 {                     System.Console.WriteLine("Valor de Arreglo en posicion[" + i + "," + j + "]:"  + arregloIrregular[i][j]);                 }             }             Console.ReadLine();         }     }
}

No hay comentarios:

Publicar un comentario