Coding Bootcamp: Linear data structures

Learning objectives

Linear data structures

Linear data structures (examples)

Arrays

Introduction to the String class

Concatenating Strings

StringBuffer

StringBuilder

Immutable vs mutable operations

/* Simple String example */
String s = "";
int N = 100000;
for(int i=0; i<N; i++) {
    s += String.valueOf(i);
}

/* StringBuilder example */
StringBuilder builder = new StringBuilder();
for(int i=0; i<N; i++) {
    builder.append(i);
}
s = builder.toString();

/* StringBuffer example */
StringBuffer buff = new StringBuffer();
for(int i=0; i<N; i++) {
    buff.append(i);
}
s = buff.toString();

Immutable vs Mutable operations (2)

Available String Constructors

Strings as arrays

Strings as arrays (2)

// our test String phrase
String message = "Winter is coming...";

// transform a String to a char[] and print it
char[] charArray = message.toCharArray();
for(int i=0; i<charArray.length; i++)
    System.out.print(charArray[i] + ", ");

// getting the first word of the phrase
String firstWord = message.substring(0, message.indexOf(' '));
System.out.println("First word: " + firstWord);

// checking if our phrase contains the String "sun"
boolean match = message.contains("sun");
System.out.println("Contains 'sun': " + match);

// getting the last word of the phrase
String lastWord = message.substring(message.lastIndexOf(' ') + 1, message.length());
System.out.println("Last word: " + lastWord);

The output of the aforementioned 4 prints is the following:

[W, i, n, t, e, r,  , i, s,  , c, o, m, i, n, g, ., ., .]
First word: Winter
Contains 'sun': false
Last word: coming...

Java Collections Framework

Java Collections Framework

Java Collections Framework


The List data structure

The ArrayList

The ArrayList (Code example)

import java.util.ArrayList;

public class ArrayListDemo {

    public static void main(String[] args) {
        // ArrayList declaration and initialization
        ArrayList<Integer> myList = new ArrayList<>();
        
        // add elements at the end of the list
        myList.add(1);
        myList.add(2);
        myList.add(4);
        System.out.println(myList);
        
        // add an element in a specific position
        myList.add(2, 3);
        System.out.println(myList);
        
        // check if the list contains a number
        System.out.println("List contains 2? " + myList.contains(2) + "\n"
                            + "List contains 0? " + myList.contains(0));
        
        // ensures that the internal array will have 1000 elements capacity
        myList.ensureCapacity(1000);
        
        // remove all elements from the list
        myList.clear();
        System.out.println(myList);
    }

}

with output

    [1, 2, 4]
    [1, 2, 3, 4]
    List contains 2? true
    List contains 0? false
    []

Vector vs ArrayList

The LinkedList Java class

The Linked-list data structure


The Linked-list data structure


The Linked-list data structure


CircleList, a linked list example

class Circle {
    /* Linked-list nodes should have a reference of their own
    type showing the next element in the list */
    private Circle next;

    Circle(){ this.next = null; }
    public void setNext (Circle c) { this.next = c; }
    public Circle getNext () { return this.next; }

    /*
    * Here follows the code of the original Circle class 
    * as presented in the Creating Classes session
    */
}

public class CircleList {
    private Circle first_element;
    
    CircleList() { this.first_element = null; }
    
    public void addElement(Circle c) {
        if(this.first_element == null) {
            this.first_element = c; 
        } else {
            Circle current_circle = this.first_element;
            while (current_circle.getNext() != null) {
                current_circle = current_circle.getNext();
            }
            current_circle.setNext(c);
        }
    }

}

The Stack data structure

Stack visualisation


Stack (Code example)

import java.util.Stack;

public class StackDemo {

    public static void main(String[] args) {
        // Stack declarations and initialization
        Stack<Integer> stack = new Stack<>();
        
        // add elements at the top of the stack
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        System.out.println(stack);
        
        // check the first element
        int top = stack.peek();
        System.out.println("top element: " + top);
        
        // remove and hold the first element
        int first = stack.pop();
        System.out.println("popped element: " + first + "\n" + stack);
        
        // check the first element
        top = stack.peek();
        System.out.println("top element: " + top);
    }

}

The Queue data structure

The Queue data structure


The Queue interface (continued)

Adding Synchronization feature

References

Exercise 1 (Strings and Arrays)

CaesarsCipher is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet link.

Exercise 1 (continued)

Exercise 2 - Collections

public class ReverseStack {

    public static Stack<Integer> reverse(Stack<Integer> initial) {
        // fill-in your code here
    }
    
    public static void main(String[] args) {
        Stack<Integer> myStack = new Stack<>();
        myStack.addAll(Arrays.asList(new Integer[]{0,1,2,3,4,5,6,7,8,9,10}));
        System.out.println("initial stack: " + myStack);
        System.out.println("reversed stack: " + reverse(myStack));
    }
}

The output is

    initial stack: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    reversed stack: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

Exercise 3 - Collections

Exercise 3 (continued)

Exercise 3 (continued)

Exercise 3 (continued)

Exercise 3 (continued)

Exercise 3 (continued)

Exercise 3 (continued)

Exercise 3 (continued)

Exercise 3 (continued)

Exercise 3 (continued)

Exercise 4 (Strings, Arrays)

Exercise 5 (Arrays)

Exercise 3b

Exercise 3b (continued)

Exercise 3b (continued)


Creative Commons Licence
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.