// This file stores some of the examples we coded in DrJava in class. import java.util.*; // for Arrays.toString public class DrJavaExamples { public static void main(String[] args) { // prints array elements int[] list = {}; System.out.print(list[0]); for (int i = 1; i < list.length; i++) { System.out.print(", " + list[i]); } System.out.println(); // vote-counting example (see below for another version) // string stores voters' votes // (R)epublican, (D)emocrat, (I)ndependent String votes = "RDRDRRIDRRRDDDDIRRRDRRRDIDIDDRDDRRDRDIDD"; int[] counts = new int[3]; // R -> 0, D -> 1, I -> 2 for (int i = 0; i < votes.length(); i++) { char c = votes.charAt(i); if (c == 'R') { counts[0]++; } else if (c == 'D') { counts[1]++; } else { // c == 'I' counts[2]++; } } System.out.println(Arrays.toString(counts)); // [17, 18, 5] } } /* Another version of the vote-counting loop that doesn't need if/else: // 012 String parties = "RDI"; for (int i = 0; i < votes.length(); i++) { char c = votes.charAt(i); // c == 'I' int index = parties.indexOf(c); // index == 2 counts[index]++; } */