Escolar Documentos
Profissional Documentos
Cultura Documentos
int[][] jaggedArray;
jaggedArray = new int[2][];
jaggedArray[0] = new int[5];
jaggedArray[1] = new int[3];
Here is how we declare, allocate and initialize an array of arrays (a jagged array
whose elements are arrays of integer values):
int[][] myJaggedArray = {
new int[] {5, 7, 2},
new int[] {10, 20, 40},
new int[] {3, 25}
};
Memory Allocation
The figure below depicts how the now declared jagged array
myJaggedArray is
myJaggedArray[0][2] = 45;
The elements of the jagged array can be one-dimensional and multi-dimensional
arrays. Here is an example for jagged array of two-dimensional arrays:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
...
To have a Pascals triangle with a given height, for example 12, we allocate a jagged
will have a default value than 0 by allocation. Then we loop through the rows and
from
row we
row+1.
for loop
through the columns on the current row and the following Pascal definitions for
values in the triangle: we add the value of the current cell of the current row
(triangle[row][col]) to the cell below (triangle[row+1][col]) and to
the
cell
below
on
the
right
(triangle
PascalTriangle.cs
class PascalTriangle
{
static void Main()
{
const int HEIGHT = 12;
// Allocate the array in a triangle form
long[][] triangle = new long[HEIGHT + 1][];
for (int row = 0; row < HEIGHT; row++)
{
triangle[row] = new long[row + 1];
}
// Calculate the Pascal's triangle
triangle[0][0] = 1;
for (int row = 0; row < HEIGHT - 1; row++)
{
for (int col = 0; col <= row; col++)
{
triangle[row + 1][col] += triangle[row][col];
triangle[row + 1][col + 1] += triangle[row][col];
}
}
// Print the Pascal's triangle
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1
Array.Resiz
e allocates a
new array. It
then copies
existing
element
values to the
new array.
This logic is
needed when
an array's
size is
inadequate.
We show
how Resize
reallocates
and copies
elements.
Array.Copy
Based on: .NET 4.5
Example. Th
is first
example
uses
Array.Resize
to replace a
large array
with a
smaller one.
This is useful
if we have a
large array
of data, and
want to only
keep the first
number of
bytes or
elements.
Here:Arr
ay.Resize
changes
an array
with four
elements
to one
with two
elements.
C# program that uses Array.Resize
using System;
class Program
{
static void Main()
{
// Initialize array for example.
char[] array = new char[4];
array[0] = 'p';
array[1] = 'e';
array[2] = 'r';
array[3] = 'l';
// Display the array.
for (int i = 0; i < array.Length; i++)
{
Console.Write(array[i]);
}
Console.WriteLine();
// Resize the array from 4 to 2
elements.
Array.Resize(ref array, 2);
// Display the array that has been
resized.
for (int i = 0; i < array.Length; i++)
{
Console.Write(array[i]);
}
Console.WriteLine();
}
}
Output
(Array was resized to 2 elements.)
perl
pe
Internals. I
nternally, the
single call to
Array.Resize
above runs
through an
algorithm
that
determines
that the
array needs
to be
smaller. So it
copies the
referenced
array to a
new array,
and then
changes the
reference.
So:Array.
Resize is
a
misnomer
. It does
not resize
the array.
It
replaces
the array
with a
new one
of a
different
size.
Example
2. Next, we
replace an
array with a
larger one
with
Array.Resize.
This can be
useful for
certain data
structures,
such as
those that
must
accommodat
e more data
but have
minimal
memory
footprint.
Note:I
have
used this
in
implemen
ting hash
table
buckets.
Array.Resi
ze is
helpful in
optimizati
on tasks.
Cauti
on:If
we
omit
the
Array.
Resize
call,
we will
get an
Index
OutOf
Range
Except
ion.
This
except
ion
should
be
avoide
d.
IndexOutOfRangeException
Program 2 that uses Array.Resize: C#
using System;
class Program
{
static void Main()
{
// Initialize an array with 5 elements.
char[] arr = new char[5];
arr[0] = 'p';
arr[1] = 'y';
arr[2] = 't';
arr[3] = 'h';
arr[4] = 'o';