Recent changes Random page
GAMING
Technology
 
Gaming
Entertainment
Science Fiction
Biggest wikis
Hobbies
Music
See more...

Arrays

From Programmer's Wiki

Jump to: navigation, search
 

Arrays are the most basic type of list available in most programming languages. Arrays occupy a contiguous area of storage and can be traversed randomly. Arrays are created to hold a certain number of elements and they can be accessed by supplying the index of the element in the array. For loops are particularly useful for accessing arrays.

[edit] PHP

/* define array using auto-generated keys */
$data = array('apple', 'orange');

/* show values */
print_r($data);
#Array
#(
#    [0] => apple
#    [1] => orange
#)


/* define array with user-defined keys */
$data = array('a'=>'apple', 'o'=>'orange');

/* show values */
print_r($data);
#Array
#(
#    [a] => apple
#    [o] => orange
#)

[edit] Java

// define a class named array
class array {
    // define main method of this java code
    public static void main(String[] args){
    
        // declare an array named intArray for integer type values
        int[] intArray = {14,15,16,17,18};
        for (int i = 0; i < intArray.length; i++) {
            System.out.println((i+1) + " Element of array : " + intArray[i]);
        }
    }
}

[edit] C++

#include <cstdlib>
#include <iostream>
#include <vector>
#include <string>
int main(int argc, char* argv[] ) {
    // declare an array named intArray for integer type values on the stack
    int iStackArray[] = {14,15,16,17,18};
    for(int i=0; i<5; i++) {
        std::cout << (i+1) << " Element of array : " << iStackArray[i] << std::endl;
    }
    // declare an empty array of char with a size of 15 on the heap
    // In c and c++ arrays are pointers to contiguous memory locations
    // and is not abstracted away from the programmer.
    char* cHeapArray = new char[15];

    // declate an empty array of string with a size variable size using STL.
    std::vector<std::string> sVectorArray;

    // if the cHeapArray is not needed anymore, free it by calling delete[] (not delete)!!!
    delete[] cHeapArray;

    return( EXIT_SUCCESS );
}


Rate this article:
Share this article: