Object Oriented Programming

Arrays and Strings

Arrays and Strings

Table of Contents

Arrays

int[] nums;
int nums[];
<type>[] var = new <type>[<size>];
<type>[] var = new <type>[]{element1, element2, ..., elementn};
<type>[] var = new <type>[<size>];
<type>[] var2 = var;

Array Methods and Tools

int[] nums = new int[10]
int num = nums[0];          // array indexing
int x = intArray[10];       // gives out of bounds error: doesn't cause program to crash
int length = nums.length;   // array length is public instance variable (NB not usually available)
import java.util.Arrays;
...
System.out.println(Arrays.toString(nums));      // converting to a string
int[] nums = Arrays.copyOf(nums, nums.length);  // create distinct copy of an array
Arrays.sort(nums)                               // in-place sort
Arrays.equal(nums, nums2);                      // equality: same length + holds same values
int[] intArray = new int[5];
intArray = new int[intArray.length + 3];

Array Iteration

for (<type> var : <iterable>) {
    // code block
}

Multi-dimensional arrays

int[][] nums = new int[100][10]; // array with 100 rows and 10 columns, each cell initialised to 0
int[][] nums = new int[10][];
for (int i = 0; i < nums.length; i++) {
    nums[i] = new int[<length of subarray>];
}
import java.util.Arrays;

public class Program {
    public static void main(String args[]) {
        final int NUM_ROWS = 5;
        final int MAX_COLS = NUM_ROWS;
        
        int[][] nums = new int[NUM_ROWS][]; // <- declaration of uninitialised 2D array

        for (int i = 0; i < nums.length; i++) {
	    nums[i] = new int[NUM_ROWS - i];
	}

        for (int i = 0; i < NUM_ROWS; i++) {
	    System.out.println(Arrays.toString(nums[i]));
        }
    }
import java.lang.Math;
// ...
public static double[] computeDoublePowers(int n) {
    double[] nums = new double[n];
    for (int i = 0; i < n; i++) {
        arr[0] = Math.pow(2, i);
    }
    return nums;
}
public class IrregularArray {
    public static void main(String[] args) {
        final int HEIGHT = 5;
        final int MAX_WIDTH = HEIGHT;
        int[][] triangleArray = new int[HEIGHT][];
        for (int i = 0; i < HEIGHT; i++) {
            triangleArray[i] = new int[MAX_WIDTH - i];
            for (int j = 0; j < MAX_WIDTH - i; j++) {
                triangleArray[i][j] = j+i+ 1;
            }
        }
    }
}

Arrays of Objects

// CircleArray.java
class CircleArray {
    Circle[] circleArray = new Circle[3];
    // create circle objects, store in array
    for (int i = 0; i < circleArray.length; i++) {
        circleArray[i] = new Circle(i, i, i+2);
    }
}

Strings

Basic string operations

String s = "Hello";
s.length();             // returns length of s (5)
s.toUpperCase();        // returns "HELLO"
s.toLowerCase();        // returns "hello"
s.split(" ");           // split by space character

e.g. What does this output?

String s = "Hello World";
s.toUpperCase();    // "HELLO WORLD"
s.replace("e", "i");// "Hillo World"
s.substring(0,2);   // "He"
s += " FIVE";       // s = "Hello World FIVE"
System.out.println(s);  // "Hello World FIVE"

Substrings

String substr = "el";
s.contains(substr);         // indicates if substr found in s
s.indexOf(substr);          // indicates index of first instance of substring; else -1
s.substring(arg1, arg2);    // slice of string with indices [arg1, arg2 - 1]

String concatenation

System.out.println("1 + 1 = " + 1 + 1);
// "1 + 1 = 11"
System.out.println("1 + 1 = " + (1 + 1));
// "1 + 1 = 2"

String equality and references

public class Program {
    public static void main(String[] args) {
        // 1. Two string literals
        System.out.println("Hello" == "Hello"); // true

        // 2. One literal, one variable
        String s0 = "Hello";
        System.out.println(s0 == "Hello");      // true

        // 3. Two variables, given the same literal value
        String s1 = "Hello";
        String s2 = "Hello";
        System.out.println(s1 == s2);           // true

        // 4. Two variables, with one creating a new "object"
        String s3 = "Hello";
        String s4 = new String("Hello");
        System.out.println(s3 == s4);           // false
    }
}

String Modification


Edit this page.