Creating an Array: Difference between revisions

From Rosetta Code
Content added Content deleted
mNo edit summary
(Blanked page since people can't / don't read)
 
(183 intermediate revisions by 50 users not shown)
Line 1: Line 1:
{{DeprecatedTask}}
[http://tizolaa.ifrance.com/web/dvd-video/ dvd video girl ai] [http://toofarfrommaybe.ifrance.com/view/new/vladivostok/ vladivostok] [http://deviantrus.ifrance.com/topic/organizzazione-delle/ organizzazione delle amministrazioni pur] [http://demurediablo.ifrance.com/library/html/palmari-vendita/ palmari vendita] [http://kalian42.ifrance.com/text/directory/auto-gem/ auto gem] [http://akmokanzen.ifrance.com/images/small/annuncio-incontri.htm annuncio incontri piemonte] [http://singaporepets.ifrance.com/library/html/tvp.htm tvp] [http://tat-ooin.ifrance.com/resources/articles/interneti-pood.htm interneti pood] [http://broadwaylili.ifrance.com/content/view/valio-la.htm valio la pena marc anthony] [http://tat-ooin.ifrance.com/resources/articles/mudanza.htm mudanza] [http://demurediablo.ifrance.com/library/html/wfxi-eu/ wfxi 2440eu] [http://toofarfrommaybe.ifrance.com/view/new/se-mi/ se mi innamoro] [http://girlwho-is.ifrance.com/img/styles/tiepolo.htm tiepolo] [http://oiyaoi.ifrance.com/images/small/watch-sex/ watch sex girls and action live] [http://igaros.ifrance.com/lib/robert-downey/ robert downey jr] [http://broadwaylili.ifrance.com/content/view/valdhotel.htm valdhotel] [http://tizolaa.ifrance.com/web/accademia-burger/ accademia burger king] [http://broadwaylili.ifrance.com/content/view/hoara-borsello.htm hoara borsello] [http://toofarfrommaybe.ifrance.com/view/new/testo-canzone/ testo canzone the love] [http://demurediablo.ifrance.com/library/html/moto-kawasaki/ moto kawasaki 750] [http://singaporepets.ifrance.com/library/html/www-stampa.htm www stampa it] [http://lordsander.ifrance.com/topic/liu-jo.htm liu jo abiti donna] [http://broadwaylili.ifrance.com/content/view/soluzioni-larry.htm soluzioni larry] [http://lovereceier.ifrance.com/lib/sony-vgn.htm sony vgn a517m] [http://oiyaoi.ifrance.com/images/small/acquisto-mobili/ acquisto mobili usati] [http://singaporepets.ifrance.com/library/html/disegnare-con.htm disegnare con la parte destra] [http://kalian42.ifrance.com/text/directory/pirelli-abbigliamento/ pirelli abbigliamento] [http://lordsander.ifrance.com/topic/baby-cry.htm baby cry] [http://jdawsona.ifrance.com/data/nikki-anderson/ nikki anderson] [http://toofarfrommaybe.ifrance.com/view/new/traduzioni-latino/ traduzioni latino gratis] [http://tizolaa.ifrance.com/web/dr-alban/ dr alban] [http://deviantrus.ifrance.com/topic/thaila/ thaila] [http://jdawsona.ifrance.com/data/sapphire-radeon/ sapphire radeon x700] [http://noxuhax.ifrance.com/content/view/hdw.htm hdw 3] [http://tizolaa.ifrance.com/web/catering-sire/ catering sire] [http://speedofsoun.ifrance.com/text/directory/codici-di.htm codici di sblocco per tre] [http://demurediablo.ifrance.com/library/html/fd/ fd 100] [http://noxuhax.ifrance.com/content/view/acer-travelmate.htm acer travelmate 250] [http://demurediablo.ifrance.com/library/html/claudia-schiffer/ claudia schiffer lingerie] [http://broadwaylili.ifrance.com/content/view/fabio-cannavaro.htm fabio cannavaro mondiali 2002] [http://kalian42.ifrance.com/text/directory/disegni-di/ disegni di animali da colorare] [http://toofarfrommaybe.ifrance.com/view/new/dmr-eh/ dmr eh60] [http://tizolaa.ifrance.com/web/progress-monster/ progress monster p4] [http://lovereceier.ifrance.com/lib/sergio-mendes.htm sergio mendes barabare] [http://lovereceier.ifrance.com/lib/dvd-r.htm dvd r dvd r] [http://highbulp.ifrance.com/images/small/rivoluzione-russa/ rivoluzione russa] [http://kalian42.ifrance.com/text/directory/uomini-anni/ uomini, anni, vita] [http://voltia.ifrance.com/content/view/fotografie-simpatiche.htm fotografie simpatiche] [http://deviantrus.ifrance.com/topic/calendario-sexy/ calendario sexy] [http://deviantrus.ifrance.com/topic/metal-gear/ metal gear solid 3 tra un mese, meno un giorno] [http://kalian42.ifrance.com/text/directory/eizo-monitor/ eizo monitor lcd 17] [http://speedofsoun.ifrance.com/text/directory/dvd-region.htm dvd region free] [http://lovereceier.ifrance.com/lib/lx.htm lx 500] [http://girlwho-is.ifrance.com/img/styles/monitor-smbf.htm monitor sm930bf] [http://toofarfrommaybe.ifrance.com/view/new/mircea-russu/ mircea russu] [http://lordsander.ifrance.com/topic/bathroom.htm bathroom] [http://jdawsona.ifrance.com/data/guerriero-rosso/ guerriero rosso] [http://lovereceier.ifrance.com/lib/strip-poker.htm strip poker racconto] [http://noxuhax.ifrance.com/content/view/sata-rpm.htm sata 10000rpm hard disk e floppy] [http://tat-ooin.ifrance.com/resources/articles/viaggiavventurenelmondo.htm viaggiavventurenelmondo] [http://voltia.ifrance.com/content/view/processore-sempron.htm processore sempron] [http://demurediablo.ifrance.com/library/html/s-giacobbe/ s giacobbe il sole e il girasole] [http://deviantrus.ifrance.com/topic/i-maschi/ i maschi gianna nannini] [http://toofarfrommaybe.ifrance.com/view/new/uo-arriva/ uo arriva in occidente] [http://voltia.ifrance.com/content/view/vacanze-in.htm vacanze in barcellona] [http://igaros.ifrance.com/lib/frigorifero-/ frigorifero 160 cm] [http://toofarfrommaybe.ifrance.com/view/new/lama-tagliente/ lama tagliente] [http://tizolaa.ifrance.com/web/anime-page/ anime page] [http://voltia.ifrance.com/content/view/rosalinda-celentano.htm rosalinda celentano nuda] [http://tat-ooin.ifrance.com/resources/articles/cabrio-bmw.htm cabrio bmw] [http://toofarfrommaybe.ifrance.com/view/new/www-milfhunter/ www milfhunter com] [http://oiyaoi.ifrance.com/images/small/phoenix-high/ phoenix high power] [http://broadwaylili.ifrance.com/content/view/arredamento-treviso.htm arredamento treviso] [http://kalian42.ifrance.com/text/directory/download-software/ download software dvd] [http://deviantrus.ifrance.com/topic/scrutinio-voti/ scrutinio voti regionali sardegna] [http://highbulp.ifrance.com/images/small/pesca-in/ pesca in] [http://demurediablo.ifrance.com/library/html/video-crollo/ video crollo torri gemelle] [http://tizolaa.ifrance.com/web/kyle-minogue/ kyle minogue slow] [http://oiyaoi.ifrance.com/images/small/fiat-punto/ fiat punto 60] [http://demurediablo.ifrance.com/library/html/la-teoria/ la teoria del volo] [http://toofarfrommaybe.ifrance.com/view/new/chris/ chris] [http://lordsander.ifrance.com/topic/racconti-erotici.htm racconti erotici mia moglie] [http://lovereceier.ifrance.com/lib/stampanti-a.htm stampanti a modulo continuo] [http://yeeden.ifrance.com/img/styles/dimmi-che/ dimmi che credi] [http://jdawsona.ifrance.com/data/deborah-blando/ deborah blando] [http://lovereceier.ifrance.com/lib/guasila.htm guasila] [http://oiyaoi.ifrance.com/images/small/katja-lel/ katja lel] [http://lordsander.ifrance.com/topic/differenziale-matematica.htm differenziale (matematica e g...] [http://deviantrus.ifrance.com/topic/sostiene-pereira/ sostiene pereira] [http://yeeden.ifrance.com/img/styles/acer-travelmate/ acer travelmate 290] [http://lordsander.ifrance.com/topic/pescara-gay.htm pescara gay] [http://noxuhax.ifrance.com/content/view/wet-t.htm wet t shirt] [http://singaporepets.ifrance.com/library/html/acquisti-a.htm acquisti a rate] [http://oiyaoi.ifrance.com/images/small/ricetta-focaccia/ ricetta focaccia ligure] [http://demurediablo.ifrance.com/library/html/disco-mo/ disco mo] [http://highbulp.ifrance.com/images/small/negozi-one/ negozi one line] [http://lordsander.ifrance.com/topic/philips-sbchn.htm philips sbc-hn110] [http://kalian42.ifrance.com/text/directory/dionakra/ dionakra] [http://jdawsona.ifrance.com/data/libre/ libre] [http://igaros.ifrance.com/lib/mina-canzonissima/ mina canzonissima 68] [http://lordsander.ifrance.com/topic/giochi-nintendo.htm giochi nintendo] [http://akmokanzen.ifrance.com/images/small/drogo.htm drogo] [http://lovereceier.ifrance.com/lib/sirena-polizia.htm sirena polizia] [http://broadwaylili.ifrance.com/content/view/organizer-elettronico.htm organizer elettronico] [http://yeeden.ifrance.com/img/styles/canon-mvx/ canon mvx 350i] [http://broadwaylili.ifrance.com/content/view/software-musica.htm software musica] [http://voltia.ifrance.com/content/view/emanuel-camicie.htm emanuel camicie] [http://highbulp.ifrance.com/images/small/oki-c/ oki c 5510 mfp] [http://oiyaoi.ifrance.com/images/small/sat-code/ sat code] [http://deviantrus.ifrance.com/topic/filmati-e/ filmati e possibili ritardi per dead or alive 4] [http://demurediablo.ifrance.com/library/html/esibizioniste-siciliane/ esibizioniste siciliane] [http://yeeden.ifrance.com/img/styles/edwards-jorge/ edwards, jorge] [http://girlwho-is.ifrance.com/img/styles/espresso-caffe.htm espresso caffe ariete] [http://voltia.ifrance.com/content/view/troppo-forte.htm troppo forte] [http://noxuhax.ifrance.com/content/view/ibanez-acustica.htm ibanez acustica] [http://girlwho-is.ifrance.com/img/styles/lei-x.htm lei x lui] [http://jdawsona.ifrance.com/data/www-grandefratello/ www grandefratello tv] [http://igaros.ifrance.com/lib/dvd-frontale/ dvd frontale] [http://yeeden.ifrance.com/img/styles/gutterball/ gutterball 2] [http://kalian42.ifrance.com/text/directory/mulinello-surf/ mulinello surf casting] [http://oiyaoi.ifrance.com/images/small/hp-media/ hp media center pc] [http://demurediablo.ifrance.com/library/html/cerco-supereva/ cerco supereva amicizie] [http://yeeden.ifrance.com/img/styles/s-gps/ s100 gps] [http://igaros.ifrance.com/lib/kim-khanh/ kim khanh] [http://deviantrus.ifrance.com/topic/levoni/ levoni] [http://girlwho-is.ifrance.com/img/styles/olivo-luminarias.htm olivo luminarias] [http://voltia.ifrance.com/content/view/toshiba-sde.htm toshiba sd-350e] [http://girlwho-is.ifrance.com/img/styles/indiana-line.htm indiana line 5 40] [http://oiyaoi.ifrance.com/images/small/i-swear/ i swear] [http://lovereceier.ifrance.com/lib/s-la.htm s la giochi u1] [http://toofarfrommaybe.ifrance.com/view/new/lettori-cdrom/ lettori cd-rom] [http://lovereceier.ifrance.com/lib/zombi.htm zombi 2] [http://toofarfrommaybe.ifrance.com/view/new/the-sims/ the sims download] [http://voltia.ifrance.com/content/view/sa-salvador.htm sa salvador] [http://kalian42.ifrance.com/text/directory/carpark/ carpark] [http://demurediablo.ifrance.com/library/html/daddy-yankke/ daddy yankke] [http://tat-ooin.ifrance.com/resources/articles/vacanza-new.htm vacanza new york] [http://kalian42.ifrance.com/text/directory/zelda-ocarina/ zelda ocarina of time] [http://toofarfrommaybe.ifrance.com/view/new/caz/ caz] [http://akmokanzen.ifrance.com/images/small/eclissi-sole.htm eclissi sole] [http://girlwho-is.ifrance.com/img/styles/dario-simic.htm dario simic] [http://lordsander.ifrance.com/topic/oedipus-orca.htm oedipus orca] [http://speedofsoun.ifrance.com/text/directory/beheaded-ali.htm beheaded ali marines] [http://deviantrus.ifrance.com/topic/batterie-nokia/ batterie nokia 6150] [http://speedofsoun.ifrance.com/text/directory/acrobat-full.htm acrobat full] [http://igaros.ifrance.com/lib/video-musu/ video musu] [http://yeeden.ifrance.com/img/styles/trimerite/ trimerite] [http://singaporepets.ifrance.com/library/html/on-my.htm on my kness] [http://oiyaoi.ifrance.com/images/small/xj-/ xj 900 yamaha] [http://oiyaoi.ifrance.com/images/small/l-eternita/ l eternita giorgia] [http://speedofsoun.ifrance.com/text/directory/vendita-stampante.htm vendita stampante laser] [http://girlwho-is.ifrance.com/img/styles/this-world.htm this world maroon five] [http://singaporepets.ifrance.com/library/html/lettore-dvd.htm lettore dvd portatile 10] [http://toofarfrommaybe.ifrance.com/view/new/los-dientes/ los dientes] [http://tizolaa.ifrance.com/web/small-dick/ small dick] [http://speedofsoun.ifrance.com/text/directory/kvfq-sony.htm kv-32fq86 sony] [http://highbulp.ifrance.com/images/small/epson-stylus/ epson stylus color 440] [http://lovereceier.ifrance.com/lib/cinematic-orchestra.htm cinematic orchestra] [http://speedofsoun.ifrance.com/text/directory/www-hiphop.htm www hiphop com] [http://lordsander.ifrance.com/topic/asus-pcdeluxe.htm asus p4c800-deluxe] [http://girlwho-is.ifrance.com/img/styles/fatime.htm fatime] [http://igaros.ifrance.com/lib/nicolas-weber/ nicolas weber] [http://yeeden.ifrance.com/img/styles/eyaculacion/ eyaculacion] [http://lordsander.ifrance.com/topic/rain-and.htm rain and] [http://highbulp.ifrance.com/images/small/aster/ aster] [http://kalian42.ifrance.com/text/directory/realizzazione-bomboniere/ realizzazione bomboniere] [http://lovereceier.ifrance.com/lib/felpa-kappa.htm felpa kappa] [http://demurediablo.ifrance.com/library/html/transfer-nec/ transfer nec 313] [http://speedofsoun.ifrance.com/text/directory/siemens-gigaset.htm siemens gigaset dect] [http://yeeden.ifrance.com/img/styles/rio-negro/ rio negro] [http://toofarfrommaybe.ifrance.com/view/new/epson/ epson 2480] [http://kalian42.ifrance.com/text/directory/sabarkantha/ sabarkantha] [http://deviantrus.ifrance.com/topic/elemire-zolla/ elemire zolla] [http://highbulp.ifrance.com/images/small/biliardo-antico/ biliardo antico] [http://voltia.ifrance.com/content/view/fate-e.htm fate e folletti] [http://yeeden.ifrance.com/img/styles/ufficio-poggibonsi/ ufficio poggibonsi] [http://lovereceier.ifrance.com/lib/love-.htm love 2 see you cry] [http://girlwho-is.ifrance.com/img/styles/lindy-webcam.htm lindy webcam] [http://deviantrus.ifrance.com/topic/immagini-hard/ immagini hard bizarre gratuite nodialer] [http://lovereceier.ifrance.com/lib/ixu-ii.htm ixu ii iii canon] [http://demurediablo.ifrance.com/library/html/carisoprodol-online/ carisoprodol online] [http://singaporepets.ifrance.com/library/html/k.htm k-] [http://demurediablo.ifrance.com/library/html/adjustable-bed/ adjustable bed] [http://lordsander.ifrance.com/topic/legends-of.htm legends of rocknroll] [http://voltia.ifrance.com/content/view/java-script.htm java script e applet] [http://jdawsona.ifrance.com/data/peccata/ peccata 2000] [http://voltia.ifrance.com/content/view/jonathan-team.htm jonathan team com firenze] [http://noxuhax.ifrance.com/content/view/diete-dimagranti.htm diete dimagranti] [http://toofarfrommaybe.ifrance.com/view/new/animaticon-gratis/ animaticon gratis] [http://yeeden.ifrance.com/img/styles/hp/ hp 2110] [http://oiyaoi.ifrance.com/images/small/www-life/ www life like racing com] [http://tat-ooin.ifrance.com/resources/articles/supermercati-panorama.htm supermercati panorama] [http://igaros.ifrance.com/lib/lampada-tartarughe/ lampada tartarughe] [http://tat-ooin.ifrance.com/resources/articles/ya-no.htm ya no te puedo amar] [http://singaporepets.ifrance.com/library/html/sfondi-cielo.htm sfondi cielo] [http://highbulp.ifrance.com/images/small/the-rasmus/ the rasmus in the shadow] [http://igaros.ifrance.com/lib/nuova-bmw/ nuova bmw 5 auto nuove] [http://igaros.ifrance.com/lib/polti-ferri/ polti ferri da stiro] [http://highbulp.ifrance.com/images/small/affitta-camera/ affitta camera] [http://speedofsoun.ifrance.com/text/directory/marco-il.htm marco il] {{task}}
'''Please do not add new code, and merge existing code to the [[Arrays]] task.'''
This task is about [[numeric arrays]]. For '''hashes''' or '''associative arrays''', please see [[Creating an Associative Array]].


This task is about numerically-indexed arrays. For '''hashes''' or '''associative arrays''', please see [[Creating an Associative Array]].
In this task, the goal is to create an [[array]]. Mention if the [[array base]] begins at a number other than zero.


In this task, the goal is to create an [[array]]. Mention if the [[array base]] begins at a number other than zero.
==[[ActionScript]]==
In addition, demonstrate how to initialize an array variable with data.
[[Category:ActionScript]]
// ActionScript arrays are zero-based
//
// creates an empty array
var arr1:Array = new Array();
// creates an array with 3 numerical values
var arr2:Array = new Array(1,2,3);
//
// or just use the shorthand
var u:Array = [];
var v:Array = [1,2,3];

==[[Ada]]==
[[Category:Ada]]
'''Compiler:''' GCC 4.1.2

Ada array indices may begin at any value, not just 0 or 1
type Arr is array (Integer range <>) of Integer;
Uninitialized : Arr (1 .. 10);
Initialized_1 : Arr (1 .. 20) := (others => 1);
Initialized_2 : Arr := (1 .. 30 => 2);
Const : constant Arr := (1 .. 10 => 1, 11 .. 20 => 2, 21 | 22 => 3);
Centered : Arr (-50..50) := (0 => 1, Others => 0);
Ada arrays may be indexed by enumerated types, which are discrete non-numeric types
type Days is (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
type Activities is (Work, Fish);
type Daily_Activities is array(Days) of Activities;
This_Week : Daily_Activities := (Mon..Fri => Work, Others => Fish);

==[[AppleScript]]==
[[Category:AppleScript]]
AppleScript supports "arrays" as "lists," and they are not limited by a single type.
set array1 to {}
set array2 to {1, 2, 3, 4, "hello", "world"}

==[[BASIC]]==
[[Category:BASIC]]
'''Interpeter:''' [[QuickBasic]] 4.5, PB 7.1

REM Force index to start at 1..n
OPTION BASE 1

REM Force index to start at 0..n
OPTION BASE 0

REM Specify that the array is dynamic and not static
'$DYNAMIC
DIM SHARED myArray(-10 TO 10, 10 TO 30) AS STRING
REDIM SHARED myArray(20, 20) AS STRING
myArray(1,1) = "Item1"
myArray(1,2) = "Item2"

==[[C]]==
[[Category:C]]
'''Compiler:''' GCC, MSVC, BCC, Watcom

'''Libraries:''' Standard
Dynamic
#include <stdlib.h> /* for malloc */
#include <string.h> /* for memset */
int n = 10 * sizeof(int);
int *myArray = (int*)malloc(n);
if(myArray != NULL)
{
memset(myArray, 0, n);
myArray[0] = 1;
myArray[1] = 2;
free(myArray);
myArray = NULL;
}

Static

int myArray2[10] = { 1, 2, 0}; /* 3..9 := 0 */

==[[C plus plus|C ]]==
[[Category:C plus plus]]
'''Compiler:''' [[GCC]], [[Visual C plus plus|Visual C ]], [[BCC]], [[Watcom]]


Using dynamically-allocated memory:
const int n = 10;
int* myArray = new int[n];
if(myArray != NULL)
{
myArray[0] = 1;
myArray[1] = 2;
delete[] myArray;
myArray = NULL;
}

Using fixed memory:
int myArray2[10] = { 1, 2, 0}; /* 3..9 := 0 */

'''Libraries:''' [[STL]]
// STL
std::vector<int> myArray3(10);
myArray3.push_back(1);
myArray3.push_back(2);

'''Libraries:''' [[Qt]]
// Qt
QVector<int> myArray4(10);
myArray4.push_back(1);
myArray4.push_back(2);

'''Libraries:''' [[Microsoft Foundation Classes]]
// MFC
CArray<int,int> myArray5(10);
myArray5.Add(1);
myArray5.Add(2);

==[[C sharp|C#]]==
[[Category:C sharp]]
Example of array of 10 int types:

int[] numbers = new int[10];

Example of array of 3 string types:

string[] words = { "these", "are", "arrays" };

You can also declare the size of the array and initialize the values at the same time:

int[] more_numbers = new int[3]{ 21, 14 ,63 };


For Multi-Deminsional arrays you declare them the same except for a comma in the type declaration.

The following creates a 3x2 int matrix
int[,] number_matrix = new int[3,2];

As with the previous examples you can also initialize the values of the array, the only difference being each row in the matrix must be enclosed in its own braces.

string[,] string_matrix = { {"I","swam"}, {"in","the"}, {"freezing","water"} };

or

string[,] funny_matrix = new string[2,2]{ {"clowns", "are"} , {"not", "funny"} };

==[[Clean]]==
[[Category:Clean]]
Array denotations are overloaded in Clean, therefore we explicitly specify the types. There are lazy, strict, and unboxed array.
===Lazy array===
Create a lazy array of strings using an array denotation.
array :: {String}
array = {"Hello", "World"}
Create a lazy array of floating point values by sharing a single element.
array :: {Real}
array = createArray 10 3.1415
Create a lazy array of integers using an array (and also a list) comprehension.
array :: {Int}
array = {x \\ x <- [1 .. 10]}
===Strict array===
Create a strict array of integers.
array :: {!Int}
array = {x \\ x <- [1 .. 10]}
===Unboxed array===
Create an unboxed array of characters, also known as <tt>String</tt>.
array :: {#Char}
array = {x \\ x <- ['a' .. 'z']}

==[[ColdFusion]]==
[[Category:ColdFusion]]
Creates a one-dimensional Array
<cfset arr1 = ArrayNew(1)>
Creates a two-dimensional Array in CFScript
<cfscript>
arr2 = ArrayNew(2);
</cfscript>
''ColdFusion Arrays are '''NOT''' zero-based, they begin at index '''1'''''

==[[Common Lisp]]==
[[Category:Common Lisp]]
Creates a one-dimensional array of length 10.
(make-array 10)
Creates a two-dimensional array with dimensions 10x20.
(make-array '(10 20))
<tt>make-array</tt> may be called with a number of optional arguments.
(make-array 4 :element-type 'fixnum :initial-contents '(1 2 3 4) :adjustable t)
==[[D]]==
[[Category:D]]
'''Compiler:''' [[DMD]],[[GDC]]

// dynamic array
int[] numbers = new int[5];
// static array
int[5] = [0,1,2,3,4];

==[[Forth]]==
[[Category:Forth]]

Forth has a variety of ways to allocate arrays of data, though it has no built-in array handling words, favoring pointer manipulation.

Static array of 200 cells, uninitialized:

create MyArray 200 cells allot
here MyArray - cell / constant MyArraySize

Static array containing the numbers 1 to 5

create MyArray 1 , 2 , 3 , 4 , 5 ,
here MyArray - cell / constant MyArraySize

Dynamic array allocation:
0 value MyArray
200 cells allocate throw to MyArray

Dynamic array free:
MyArray free throw
0 to MyArray

==[[Fortran]]==
[[Category:Fortran]]

Default case:

integer a(10)

this will have ten elements. Counting starts at 1. If a zero-based array is needed, declare like this:

integer a(0:9)

this mechanism can be extended to any numerical indices, and allowed number of dimensions (and of course to other data types than integers). For example

real*8 (25:29,12)

will be an two-dimensional, 5x12-array of 8-byte floats, where the first dimension can be addressed numerically as 25, 26, 27, 28 or 29 (and the second dimension as 1 .. 12).


==[[IDL]]==
[[Category:IDL]]

IDL doesn't really distinguish between scalars and arrays - the same operations that can create the one can <i>usually</i> create the other as well.

a = 3
help,a
A INT = 3
print,a^2
9

a = [3,5,8,7]
help,a
A INT = Array[4]
print,a^2
9 25 64 49

==[[Java]]==
[[Category:Java]]
For example for an array of 10 int values:
int[] intArray = new int[10];

Creating an array of Strings:

String[] s = {"hello" , "World" };

==[[JavaScript]]==
[[Category:JavaScript]]
var myArray = new Array();
var myArray2 = new Array("Item1","Item2");
var myArray3 = ["Item1", "Item2"];

==[[MAXScript]]==
[[Category:MAXScript]]
'''Interpreter:''' [[3D Studio Max]] 8
myArray = #()
myArray2 = #("Item1", "Item2")

==[[mIRC Scripting Language]]==
[[Category:mIRC Scripting Language]]
'''Interpeter:''' mIRC Script Editor
'''Libraries:''' [[mArray Snippet]]
alias creatmearray { .echo -a $array_create(MyArray, 5, 10) }

==[[OCaml]]==
[[Category:OCaml]]
Using an array literal:

let array = [| 1; 2; 3; 4; 5 |];;

To create an array of five elements with the value 0:

let num_items = 5 and initial_value = 0;;
let array = Array.make num_items initial_value

To create an array with contents defined by passing each index to a callback (in this example, the array is set to the squares of the numbers 0 through 4):

let callback index = index * index;;
let array = Array.init 5 callback


==[[Perl]]==
[[Category:Perl]]
'''Interpreter:''' [[Perl]] 5

my @empty;
my @empty_too = ();

my @populated = ('This', 'That', 'And', 'The', 'Other');
print $populated[2];
# And
my $aref = ['This', 'That', 'And', 'The', 'Other'];
print aref->[2];
# And

# having to quote like that really sucks, and that's why we got syntactic sugar
my @wakey_wakey = qw(coffee sugar cream);
push @wakey_wakey, 'spoon';
# add spoon to right-hand side
my $cutlery = pop @wakey_wakey;
# remove spoon
unshift @wakey_wakey, 'cup';
# add cup to left-hand side
my $container = shift @wakey_wakey;
# remove cup

my @multi_dimensional = (
[0, 1, 2, 3],
[qw(a b c d e f g)],
[qw(! $ %

==[[Toka]]==
[[Category:Toka]]
Toka allows creation of an array using is-array. Access to the elements is done using
get-element, put-element, get-char-element, and put-char-element functions. You can
not initialize the values automatically using the core array functions.

100 cells is-array foo
100 chars is-array bar

Latest revision as of 15:07, 29 November 2019

Creating an Array was a programming task. It has been deprecated for reasons that are discussed in its talk page.

Please do not add new code, and merge existing code to the Arrays task.

This task is about numerically-indexed arrays. For hashes or associative arrays, please see Creating an Associative Array.

In this task, the goal is to create an array. Mention if the array base begins at a number other than zero. In addition, demonstrate how to initialize an array variable with data.