Chapter 6: Arrays


Chapter 6: Arrays and ArrayLists

Highlights review:

  • In Java, arrays are objects
  • Arrays can be create with the “new” operator: int[] height = new int[11]
  • Arrays can also be created in using this format: int[] scores = {76, 87, 99}
  • The index operator [ ] performs automatic bounds checking
  • Beware of the off-by-one errors in a program
  • Either syntax int[] grades or int grades[] is correct

NOTE: The following programs and the pdf are stored in edmodo’s folder. Get familiar with all these programs and their algorithms to further develop assignments.

BasicArray
Primes

Printing an array of integers in reverse order:

ReverseOrder

Letter counting

LetterCount

Non-alphabetic characters: 14

Array length: 8
The first few prime numbers are:
2  3  5  7  11  13  17  19  

Helpful notes for testing purposes:
Instead of entering manually input, you could put the data in a file like input.data. Take a look at an easy way to read data from a file:

import java.util.Scanner;
public class ScannerInTerminal
{
    public static void main(String [] args)
    {
        Scanner sc = new Scanner(System.in);
        String fromTerm = sc.next();
        System.out.println(fromTerm);
    }
}

Then you can run it in Terminal
mrseliaTerminal%: java ScannerInTerminal < input.txt 3.14159…
Classwork:

Programming Projects 6.1, 6.2, 6.4 and 6.5


Homework:

Short Answers 6.1, 6.2 and 6.3
Read pages 298 through 311