Previous Lecture Lecture 4 Next Lecture

Lecture 4, Wed 04/10

User Input, Random, ArrayLists

Command Line Arguments

Example

$ java Lecture 10 20
public class Lecture {

    public static void main(String args[]) {
        System.out.println(args[0]); // 10
        System.out.println(args[1]); // 20
        System.out.println(args[0] + args[1]); // 1020
        System.out.println(Integer.parseInt(args[0]) + 
        Integer.parseInt(args[1])); // 30
    }
}

Console Input

Example

// Lecture.java
import java.util.Scanner; // import Scanner object

Scanner s = new Scanner(System.in);
String t = s.nextLine();
System.out.println(t);
s.close(); // important so resources aren’t wasted.
Scanner s = new Scanner(System.in);
System.out.print("Enter your name: ");
String first = s.next(); // returns a value up to a newline or space
String last = s.next();

System.out.println(first);
System.out.println(last);

s.close();

Random Number Generation

Example

// Lecture.java

import java.util.Random;

// in main
Random generator = new Random();
int randomInt = generator.nextInt(4); // 0 -> 3
double randomDouble = generator.nextDouble(); // 0 -> 0.9999999...
System.out.println(randomInt); 
System.out.println(randomDouble);
int x = generator.nextInt(3) – 1; // returns [-1, 0, 1]
int y = generator.nextInt(6) + 5; // returns [5, 6, 7, 8, 9, 10]

Arrays

Example

// Lecture.java

// in main

// declare array
int[] array;

// assign array of size 100 ints (i.e. 100 * 4 bytes)
array = new int[100]; // initializes ints to 0 

// initialize elements
array[0] = 10;
array[1] = 20;

// extract elements
System.out.println(array[1]); // prints 20

// initialization shortcut
int[] array2 = {10, 20, 30}; // creates an array of size 3 ints

System.out.println(array2[2]); // prints 30
System.out.println(array[4]); // returns 0
System.out.println(array2[4]); // ERROR, ArrayIndexOutOfBounds

Example

int[] array3 = array2.clone();
if (array3 == array2) {
    System.out.println("array3 == array2");
} else {
    System.out.println("array3 != array2"); // Two separate arrays
}

// loop through array (print out values).
for (int i = 0; i < array3.length; i++) {
    System.out.println(array3[i]);
} // 10, 20, 30

System.out.println("--");

// Note the "for-each" loop syntax
for (int x : array3) {
    System.out.println(x);
}

ArrayLists

import java.util.ArrayList;

// constructs an ArrayList object containing Integer objects
// initially, the list is empty.
ArrayList<Integer> x = new ArrayList<Integer>();

Example: Adding to an ArrayList

ArrayList<Integer> x = new ArrayList<Integer>();

x.add(5);
x.add(10);
x.add(15);
x.add(20);

for (int i = 0; i < x.size(); i++) {
	System.out.println(x.get(i));
}

Example: Setting items in an ArrayList

Example: Removing from an ArrayList

Some more examples using ArrayLists

// Sums up the integers in the ArrayList
public static int sum(ArrayList<Integer> x) {
    int sum = 0;
    for (int i = 0; i < x.size(); i++) {
        sum += x.get(i);
    }
    return sum;
}

// Swaps two values in the arrayList based on index
public static void swap(ArrayList<Integer> x, int i, int j) {
    int temp = x.get(i);
    x.set(i, x.get(j));
    x.set(j, temp);
}
	
// Returns the position of the first occurence of the value.
public static int getFirstPosition(ArrayList<Integer> x, int value) {
    int i = 0;
    while (i < x.size()) {
        if (x.get(i) == value)
            return i;
        else
            i++;
    }
    return -1;
}

// ---
// in main
ArrayList<Integer> x = new ArrayList<Integer>();

x.add(5);
x.add(10);
x.add(15);
x.add(20);

System.out.println(sum(x)); // 50
swap(x, 0, 3);
printArrayList(x); // 20, 10, 15, 5
System.out.println(getFirstPosition(x, 15)); // 2

ArrayLists containing ArrayLists

// Lecture.java

// in main
ArrayList<ArrayList<Integer>> listOfLists = new 
ArrayList<ArrayList<Integer>>();

ArrayList<Integer> arrayInt1 = new ArrayList<Integer>();
arrayInt1.add(100);
arrayInt1.add(200);
arrayInt1.add(300);	

ArrayList<Integer> arrayInt2 = new ArrayList<Integer>();
arrayInt2.add(-100);
arrayInt2.add(-200);	
listOfLists.add(arrayInt1);
listOfLists.add(arrayInt2);

// Print all ints in the listOfLists
for (int i = 0; i < listOfLists.size(); i++) {
    ArrayList<Integer> intList = listOfLists.get(i);
    for (int j = 0; j < intList.size(); j++) {
        System.out.println(intList.get(j));
    }
}

Stack Overflow

Example: Factorial

public static int factorial(int n) {
    // No base case
//  if (n == 1) {
//      return 1;
//  }
    return n * factorial(n - 1);
}
public static int factorial(int n) {
String s;
    Runtime runtime = Runtime.getRuntime();
    while (true) {
        s = new String();
        System.out.println("Used Memory:" + (runtime.totalMemory() - runtime.freeMemory()) / (1024 * 1024)); // measured in bytes
    }
}