Swift: An array of string

This entry is part 6 of 41 in the series Swift

Defining an array of string in Swift and output the array.

import UIKit


var someArray: [String] = ["Alex", "Brian", "Dave"]

print(someArray[0])
print(someArray[1])

swift

C++: Array

This entry is part 41 of 61 in the series C++

Now that we have Netbeans for C++ properly installed, we start off with a simple example on Array.

An array declaration has the form Name[D] where Name is the name being defined and D is the dimension of the array.

The dimension specifies the number of elements and must be greater than zero.  And the dimension must be known at compile time.

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char** argv) {
    
    double height[] = {170, 172, 185};
    
    cout << height[0] << endl;
    
    cout << height[2];

    return 0;
}

In line 8, we are naming the array as height and it’s dimension is 3.  The first array value is accessible by height[0] as an array index will always start from 0.

array

Java: An array of objects

This entry is part 48 of 54 in the series Learn Java

Sometimes, we might want to create an array of objects.  This is done by creating an array of instance.

Here we create an array class Rock and using an instance called r, we initialize the object using the constructor.

package javaapplication42;

public class Music {

    static public class Rock {

        int TopTen = 0;

        public Rock(int t) {
            this.TopTen = t;
        }

        public void count() {
            System.out.println(TopTen);
        }
    }
    
    public static void main(String[] args) {

        Rock[] r = new Rock[3];
       r[0] = new Rock(10); 
       r[0].count();
       
       r[1] = new Rock(2); 
       r[1].count();

       r[2] = new Rock(5); 
       r[2].count();

    }
}

arrayobject