Collections: Difference between revisions

Content deleted Content added
m →‎{{header|REXX}}: deleted a superflous blank line. -- ~~~~
Line 134:
 
=={{header|C}}==
The onlyOne thing in C language proper that can be said asto be a collection is array type:.
An array has a length known at compile time.
<lang c>int ar[10];
<lang c>#define cSize( a ) ( sizeof(a)/sizeof(a[0]) ) /* a.size() */
ar[0] = 1;
int ar[10]; /* Collection<Integer> ar = new Collection<Integer>(10); */
ar[1] = 2;</lang>Arbitrarily complex data structures can be constructed, normally via language fetures <code>struct</code> and pointers. They are everywhere, but not provided by the C language itself per se.
ar[0] = 1; /* ar.set(0, 1); */
ar[01] = 12;
 
int* p; /* Iterator<Integer> p; Integer pValue; */
for (p=ar; /* for( p = ar.itereator(), pValue=p.next(); */
p<(ar+cSize(ar)); /* p.hasNext(); */
p++) { /* pValue=p.next() ) { */
printf("%d\n",*p); /* System.out.println(pValue); */
} /* } */</lang>
Please note that c built-in pointer-arithmatic support which helps this logic. An integer may be 4 bytes, and a char 1 byte: the plus operator (+) is overloaded to multiply a incement by 4 for integer pointers and by 1 for char pointers (etc).
 
Another construct which can be seen as a collection is a malloced array. A malloced array's size is not known at compile time.
<lang c>int ar[10];
int* ar; /* Collection<Integer> ar; */
int arSize;
arSize = (rand () % 6) + 1;
ar = calloc(arSize, sizeof(int) ); /* ar = new ArrayList<Integer>(arSize); */
ar[0] = 1; /* ar.set(0, 1); */
 
int* p; /* Iterator<Integer> p; Integer pValue; */
for (p=ar; /* p=ar.itereator(); for( pValue=p.next(); */
p<(ar+arSize); /* p.hasNext(); */
p++) { /* pValue=p.next() ) { */
printf("%d\n",*p); /* System.out.println(pValue); */
} /* } */</lang>
 
A string is another C language construct (when looked at with its standard libraries) that behaves like a collection.
A C language string is an array of char, and it's size may or may not be known at compile time, however a c string is terminated with a ASCII NUL (which may be stated as a constant, '\0' or ((char)0) in the C language). The String standard library "class" has many "methods", however instead of being called String.method(), they are usually called strmethod().
 
ar[1] = 2;</lang>Arbitrarily complex data structures can be constructed, normally via language fetures <code>struct</code> and pointers. They are everywhere, but not provided by the C language itself per se.
 
=={{header|C++}}==