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)
 
(171 intermediate revisions by 50 users not shown)
Line 1: Line 1:
{{DeprecatedTask}}
[http://cannotspell.ifrance.com/description/lib/bachata-milano/ bachata milano] [http://fishing-gro.ifrance.com/html/cloruro-calcio/ cloruro calcio] [http://me4tateljnitsa.ifrance.com/data/trasporto-solido/ trasporto solido] [http://bagrec.ifrance.com/lib/amilo-m.htm amilo m fujitsusiemens portatili m3438g] [http://orbzine.ifrance.com/images/small/frigorifero-.htm frigorifero 320 litri] [http://lauashbu.ifrance.com/lib/indennita-accompagnamento/ indennita accompagnamento] [http://zonkismusik.ifrance.com/img/styles/kit-dvd.htm kit dvd] [http://gmkamm.ifrance.com/view/new/gestione-investimento.htm gestione investimento] [http://la-seras.ifrance.com/library/html/vanessa-midori.htm vanessa midori] [http://rielethuil.ifrance.com/description/lib/gallagher-rory.htm gallagher rory] [http://last-shadow.ifrance.com/text/directory/pigeonite/ pigeonite] [http://la-seras.ifrance.com/library/html/cent-anni.htm cent anni di solitudine] [http://zonkismusik.ifrance.com/img/styles/xcel.htm xcel2000] [http://rielethuil.ifrance.com/description/lib/radio-lettore.htm radio lettore dvd] [http://lauashbu.ifrance.com/lib/loghi-cellulari/ loghi cellulari] [http://lauashbu.ifrance.com/lib/quartine-italia/ quartine italia] [http://last-shadow.ifrance.com/text/directory/stampante-a/ stampante a3 epson] [http://pegasik.ifrance.com/html/cassacco/ cassacco] [http://lady-mosquito.ifrance.com/web/pieve-a/ pieve a nievole] [http://bagrec.ifrance.com/lib/il-paradiso.htm il paradiso dei barbari] [http://uo-ination.ifrance.com/lib/marmore/ marmore] [http://heyclassylady.ifrance.com/html/stampante-duplex.htm stampante duplex hp laser] [http://rielethuil.ifrance.com/description/lib/download-java.htm download java] [http://butanexlove.ifrance.com/lib/sgs.htm sgs 57] [http://me4tateljnitsa.ifrance.com/data/gabriella-passion/ gabriella passion] [http://ryandrablya.ifrance.com/lib/concessionarie-auto.htm concessionarie auto bmw sicilia] [http://last-shadow.ifrance.com/text/directory/trans-ama/ trans ama donna] [http://last-shadow.ifrance.com/text/directory/tenebre-foto/ tenebre foto] [http://butanexlove.ifrance.com/lib/corolla-verso.htm corolla verso] [http://gmkamm.ifrance.com/view/new/le-ponte.htm le ponte di bassano] [http://gmkamm.ifrance.com/view/new/nadiad.htm nadiad] [http://uo-ination.ifrance.com/lib/rock-mexicano/ rock mexicano] [http://fishing-gro.ifrance.com/html/gianni-nazzaro/ gianni nazzaro] [http://gmkamm.ifrance.com/view/new/rullatrice.htm rullatrice] [http://orbzine.ifrance.com/images/small/sl-colour.htm sl150 colour siemens] [http://rielethuil.ifrance.com/description/lib/belle-nere.htm belle nere gratis] [http://fishing-gro.ifrance.com/html/fragole/ fragole] [http://last-shadow.ifrance.com/text/directory/www-altroconsumo/ www altroconsumo it] [http://cannotspell.ifrance.com/description/lib/pliko-p/ pliko p3 perego modular] [http://pegasik.ifrance.com/html/baglioni-mai/ baglioni mai piu come te] [http://heyclassylady.ifrance.com/html/verde.htm verde] [http://epireial-h.ifrance.com/topic/crazy-little.htm crazy little thing called love] [http://bagrec.ifrance.com/lib/whytemelville-george.htm whytemelville george] [http://uo-ination.ifrance.com/lib/potenzialita-di/ potenzialita di lavoro in sicilia] [http://la-seras.ifrance.com/library/html/via-kt.htm via k8t800 pro] [http://cannotspell.ifrance.com/description/lib/the-sims/ the sims no cd] [http://last-shadow.ifrance.com/text/directory/porcellini-disney/ porcellini disney] [http://last-shadow.ifrance.com/text/directory/radio-trieste/ radio trieste] [http://sherryfraser03.ifrance.com/web/energia-trasporti/ energia trasporti] [http://pegasik.ifrance.com/html/il-mio/ il mio portatile] [http://la-seras.ifrance.com/library/html/darley-george.htm darley george] [http://me4tateljnitsa.ifrance.com/data/la-squadra/ la squadra stagione 1 episodio 15] [http://sherryfraser03.ifrance.com/web/branzino-al/ branzino al forno] [http://pegasik.ifrance.com/html/elthon-john/ elthon john your songs] [http://orbzine.ifrance.com/images/small/otogi-xbox.htm otogi xbox] [http://me4tateljnitsa.ifrance.com/data/i-due/ i due volti del novecento] [http://stasiia.ifrance.com/img/styles/kettler-porta/ kettler porta bilanciere] [http://stasiia.ifrance.com/img/styles/bilancia-pesa/ bilancia pesa neonati] [http://ryandrablya.ifrance.com/lib/weber.htm weber] [http://pegasik.ifrance.com/html/hub-video/ hub video] [http://ryandrablya.ifrance.com/lib/www-ed.htm www ed lascuola it] [http://lady-mosquito.ifrance.com/web/strane-voci/ strane voci] [http://zonkismusik.ifrance.com/img/styles/zainetti.htm zainetti] [http://stasiia.ifrance.com/img/styles/accessorio-ciclomotore/ accessorio ciclomotore] [http://epireial-h.ifrance.com/topic/zidane.htm zidane] [http://heyclassylady.ifrance.com/html/pen-drivemp.htm pen drivemp3 1 gb] [http://heyclassylady.ifrance.com/html/provincie-piemonte.htm provincie piemonte] [http://bagrec.ifrance.com/lib/porcelline.htm porcelline] [http://lauashbu.ifrance.com/lib/driver-per/ driver per xp] [http://ryandrablya.ifrance.com/lib/viaggio-con.htm viaggio con te] [http://lauashbu.ifrance.com/lib/rimedi-per/ rimedi per costola incrinata] [http://me4tateljnitsa.ifrance.com/data/agordo/ agordo] [http://butanexlove.ifrance.com/lib/lavoro-part.htm lavoro part time salerno e provincia] [http://lady-mosquito.ifrance.com/web/ministero-dell/ ministero dell interno carriera prefetti] [http://sherryfraser03.ifrance.com/web/cowlist/ cowlist] [http://cannotspell.ifrance.com/description/lib/bmw-/ bmw 1200 classic] [http://bagrec.ifrance.com/lib/rosanna-casale.htm rosanna casale] [http://orbzine.ifrance.com/images/small/boss-profumo.htm boss profumo] [http://lady-mosquito.ifrance.com/web/conway/ conway] [http://cannotspell.ifrance.com/description/lib/fotos-da/ fotos da mama] [http://zonkismusik.ifrance.com/img/styles/code-kioske.htm code kioske] [http://lauashbu.ifrance.com/lib/voli-romania/ voli romania] [http://rielethuil.ifrance.com/description/lib/ciclocomputer-vdo.htm ciclocomputer vdo] [http://sherryfraser03.ifrance.com/web/ricevitore-gps/ ricevitore gps nokia 6600] [http://uo-ination.ifrance.com/lib/www-network/ www network explorer com] [http://epireial-h.ifrance.com/topic/palmira-colombia.htm palmira (colombia)] [http://gmkamm.ifrance.com/view/new/consiglieri-eletti.htm consiglieri eletti provincia di cosenza] [http://gmkamm.ifrance.com/view/new/trucchi-per.htm trucchi per gta 3] [http://bagrec.ifrance.com/lib/masterizzatore-dvd.htm masterizzatore dvd samsung] [http://uo-ination.ifrance.com/lib/collegio-geometri/ collegio geometri] [http://stasiia.ifrance.com/img/styles/everybody-s/ everybody s chancing] [http://lady-mosquito.ifrance.com/web/fred-kannone/ fred kannone] [http://butanexlove.ifrance.com/lib/f-m.htm f m] [http://epireial-h.ifrance.com/topic/buoni-cattivi.htm buoni cattivi] [http://orbzine.ifrance.com/images/small/fiesta.htm fiesta] [http://butanexlove.ifrance.com/lib/picchar.htm picchar] [http://epireial-h.ifrance.com/topic/intonaci.htm intonaci] [http://lady-mosquito.ifrance.com/web/sicilian-clan/ sicilian clan] [http://epireial-h.ifrance.com/topic/canzoni-di.htm canzoni di natale della melevisione] [http://heyclassylady.ifrance.com/html/www-com.htm www3 com english] [http://bagrec.ifrance.com/lib/divani-e.htm divani e poltrona] [http://ryandrablya.ifrance.com/lib/scanner-epson.htm scanner epson 1680] [http://zonkismusik.ifrance.com/img/styles/acoustic-alchemy.htm acoustic alchemy live from st lucia jazz 10th anniversary] [http://uo-ination.ifrance.com/lib/carburanti/ carburanti] [http://stasiia.ifrance.com/img/styles/isola-bali/ isola bali shopping] [http://me4tateljnitsa.ifrance.com/data/gli-indiavolati/ gli indiavolati] [http://rielethuil.ifrance.com/description/lib/facolta-di.htm facolta di architettura] [http://heyclassylady.ifrance.com/html/art-cool.htm art cool condizionatori fissi] [http://cannotspell.ifrance.com/description/lib/www-iranianmovies/ www iranianmovies com] [http://la-seras.ifrance.com/library/html/una-mezza.htm una mezza dozzina di rose] [http://fishing-gro.ifrance.com/html/valentina-campani/ valentina campani] [http://lady-mosquito.ifrance.com/web/logitech-webcam/ logitech webcam quickcam express] [http://pegasik.ifrance.com/html/garanzia-kia/ garanzia kia] [http://pegasik.ifrance.com/html/sniffem/ sniffem] [http://epireial-h.ifrance.com/topic/san-giovanni.htm san giovanni rotondo hotel] [http://rielethuil.ifrance.com/description/lib/delibera-n.htm delibera n 942 23 2 95 giunta regionale] [http://lauashbu.ifrance.com/lib/autoradio-con/ autoradio con dvd tv] [http://stasiia.ifrance.com/img/styles/disney-resort/ disney resort paris] [http://lady-mosquito.ifrance.com/web/hard-disk/ hard disk 2 5 da 40 gb] [http://uo-ination.ifrance.com/lib/casio-exilim/ casio exilim ex s500] [http://orbzine.ifrance.com/images/small/il-sistema.htm il sistema brit] [http://lady-mosquito.ifrance.com/web/dimensione-suono/ dimensione suono hi fi] [http://lauashbu.ifrance.com/lib/chrysler-stratus/ chrysler stratus 20 lx] [http://cannotspell.ifrance.com/description/lib/ufficio-volterra/ ufficio volterra] [http://pegasik.ifrance.com/html/foto-porno/ foto porno teen] [http://uo-ination.ifrance.com/lib/un-numero/ un numero di telefono] [http://stasiia.ifrance.com/img/styles/toner-c/ toner c1900] [http://heyclassylady.ifrance.com/html/vacanze-romane.htm vacanze romane film] [http://heyclassylady.ifrance.com/html/concorso-vfp.htm concorso vfp4 esito] [http://zonkismusik.ifrance.com/img/styles/varese-express.htm varese express como] [http://lauashbu.ifrance.com/lib/hp-business/ hp business 2600] [http://last-shadow.ifrance.com/text/directory/vendita-dvd/ vendita dvd foggia] [http://last-shadow.ifrance.com/text/directory/paolo-belli/ paolo belli] [http://uo-ination.ifrance.com/lib/noleggio-veicolo/ noleggio veicolo] [http://pegasik.ifrance.com/html/driver-audio/ driver audio] [http://butanexlove.ifrance.com/lib/c.htm c330] [http://orbzine.ifrance.com/images/small/incontri-ravvicinati.htm incontri ravvicinati] [http://fishing-gro.ifrance.com/html/chatear-con/ chatear con amigas] [http://orbzine.ifrance.com/images/small/kingdom-heart.htm kingdom heart] [http://me4tateljnitsa.ifrance.com/data/piega-tubi/ piega tubi milano] [http://last-shadow.ifrance.com/text/directory/frigorifero-americano/ frigorifero americano] [http://fishing-gro.ifrance.com/html/toshiba-qosmio/ toshiba qosmio g10] [http://pegasik.ifrance.com/html/c/ c666] [http://stasiia.ifrance.com/img/styles/roberto-cavalli/ roberto cavalli perizoma] [http://fishing-gro.ifrance.com/html/royal-balled/ royal balled] [http://ryandrablya.ifrance.com/lib/calcietto-d.htm calcietto 3d] [http://cannotspell.ifrance.com/description/lib/la-seconda/ la seconda volta] [http://cannotspell.ifrance.com/description/lib/oakley-montoya/ oakley montoya] [http://ryandrablya.ifrance.com/lib/scarpe-di.htm scarpe di napoli] [http://zonkismusik.ifrance.com/img/styles/screensaver-film.htm screensaver film] [http://bagrec.ifrance.com/lib/forno-gas.htm forno gas nardi 90] [http://stasiia.ifrance.com/img/styles/volta/ volta] [http://uo-ination.ifrance.com/lib/forno-ad/ forno ad incasso smeg] [http://last-shadow.ifrance.com/text/directory/hwicce/ hwicce] [http://epireial-h.ifrance.com/topic/logitech-z.htm logitech z5400] [http://me4tateljnitsa.ifrance.com/data/lago-di/ lago di viverone] [http://bagrec.ifrance.com/lib/sesso-video.htm sesso video pregnant] [http://bagrec.ifrance.com/lib/unire.htm unire] [http://ryandrablya.ifrance.com/lib/zero-destiny.htm zero destiny] [http://orbzine.ifrance.com/images/small/codici-sblocco.htm codici sblocco motorola a835] [http://stasiia.ifrance.com/img/styles/sapphire-xpress/ sapphire xpress 200] [http://zonkismusik.ifrance.com/img/styles/inuyacha.htm inuyacha] [http://butanexlove.ifrance.com/lib/il-canto.htm il canto del mare] [http://butanexlove.ifrance.com/lib/gold-srike.htm gold srike] [http://uo-ination.ifrance.com/lib/tanti-anni/ tanti anni fa] [http://lady-mosquito.ifrance.com/web/donne-cercano/ donne cercano uomini per incontri] [http://stasiia.ifrance.com/img/styles/speaker-inspire/ speaker inspire t7900] [http://uo-ination.ifrance.com/lib/ricette-light/ ricette light] [http://lauashbu.ifrance.com/lib/lettera-scuse/ lettera scuse amministrative] [http://bagrec.ifrance.com/lib/mustek-d.htm mustek d35] [http://zonkismusik.ifrance.com/img/styles/il-bar.htm il bar] [http://sherryfraser03.ifrance.com/web/football-sexy/ football sexy] [http://lady-mosquito.ifrance.com/web/citroen-saxo/ citroen saxo 14 vts] [http://last-shadow.ifrance.com/text/directory/nordenham/ nordenham] [http://gmkamm.ifrance.com/view/new/bigger-disk.htm bigger disk] [http://bagrec.ifrance.com/lib/frankie-ruiz.htm frankie ruiz] [http://epireial-h.ifrance.com/topic/campioni-canale.htm campioni canale5] [http://la-seras.ifrance.com/library/html/la-factoria.htm la factoria] [http://me4tateljnitsa.ifrance.com/data/il-colpo/ il colpo era perfetto ma] [http://bagrec.ifrance.com/lib/masterizzatore-dvd.htm masterizzatore dvd dual layer sony] [http://lauashbu.ifrance.com/lib/pantaloncini-kick/ pantaloncini kick boxing] [http://orbzine.ifrance.com/images/small/batteria-originale.htm batteria originale per nokia 3310] [http://zonkismusik.ifrance.com/img/styles/musica-kukident.htm musica kukident] [http://ryandrablya.ifrance.com/lib/volo-low.htm volo low cost ibiza] [http://heyclassylady.ifrance.com/html/bmw-xi.htm bmw 325xi] [http://stasiia.ifrance.com/img/styles/geordi/ geordi] [http://orbzine.ifrance.com/images/small/kevin-spacey.htm kevin spacey collection] [http://butanexlove.ifrance.com/lib/tenda-geo.htm tenda geo 3] [http://sherryfraser03.ifrance.com/web/offerte-viaggi/ offerte viaggi] [http://lady-mosquito.ifrance.com/web/cabosse/ cabosse] [http://epireial-h.ifrance.com/topic/extreme-creatine.htm extreme creatine] [http://last-shadow.ifrance.com/text/directory/ranma-/ ranma1 2 y musica] [http://zonkismusik.ifrance.com/img/styles/unique-bag.htm unique bag] [http://lauashbu.ifrance.com/lib/memoria-convenzionale/ memoria convenzionale] [http://lauashbu.ifrance.com/lib/patrick-ray/ patrick ray pugliese] [http://stasiia.ifrance.com/img/styles/la-ragazza/ la ragazza di piazza s pietro] [http://lady-mosquito.ifrance.com/web/comune-di/ comune di piazza armerina] [http://ryandrablya.ifrance.com/lib/ellemieke-vermolen.htm ellemieke vermolen] [http://zonkismusik.ifrance.com/img/styles/comparse-vita.htm comparse vita e bella] [http://heyclassylady.ifrance.com/html/amd-sempron.htm amd sempron 64 3000] {{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.