break Statementcontinue Statementswitch Statementbreak in a switch StatementHomework 8 is due this Sunday at 11:59 PM.
You will find the assignment here.
If you have a problem or a question, make a post on the Class Discussion Area.
ArrayList<String> list = new ArrayList<>();
import statement
at the top of your code
import java.util.ArrayList;
import java.util.ArrayList;
public class ArrayListString {
public static void main (String[] args){
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < args.length; i++){
list.add(args[i]);
}
System.out.println(list);
}
}
$ java ArrayListString foo bar blecth
[foo, bar, blecth]
import java.util.ArrayList;
public class ArrayListString2 {
public static void main (String[] args){
ArrayList<String> list = new ArrayList<>();
list.add("one");
list.add("two");
list.add("three");
list.add("four");
list.add("five");
System.out.println("The length of the list is " + );
}
}
$ java ArrayListString2
The length of the list is 5
for loop to move through
the values
import java.util.ArrayList;
public class ArrayListString3 {
public static void main (String[] args){
ArrayList<String> list = new ArrayList<>();
list.add("one");
list.add("two");
list.add("three");
list.add("four");
list.add("five");
for (int i = 0; i < list.size(); i++){
System.out.println(list.get(i));
}
}
}
$ java ArrayListString3
one
two
three
four
five
int or doubleArrayList<Integer> list = new ArrayList<>();
import java.util.*;
import java.io.*;
public class ArrayListIntegerFile {
public static void main (String[] args)
throws FileNotFoundException {
ArrayList<Integer> numbers = new ArrayList<>();
File file = new File(args[0]);
Scanner input = new Scanner(file);
while (input.hasNextInt()){
numbers.add(input.nextInt());
}
System.out.println(numbers.size());
}
}
$ java ArrayListIntegerFile numbers.txt
30
Contribute to society and to human well-being, acknowledging that all people are stakeholders in computing
| Method | Description |
|---|---|
| add(value) | Adds a value to the end of the list |
| add(index, value) | Adds a value at a given position |
| clear() | Removes all entries from the list |
| get(index) | Returns the value at a given position |
| remove(index) | Remove the element at a given position |
| set(index, value) | Changes the value at a give position |
| size() | Returns the number of elements in the list |
import java.util.ArrayList;
public class ListAdd {
public static void main (String[] args){
ArrayList<String> list = new ArrayList<>();
System.out.println(list.size());
list.add("one");
System.out.println(list.size());
list.add("two");
System.out.println(list.size());
System.out.println(list.size());
System.out.println(list);
}
}
$ java ListAdd
0
1
2
3
[one, two, three]
import java.util.ArrayList;
public class ListAddIndex {
public static void main (String[] args){
ArrayList<String> list = new ArrayList<>();
list.add("one");
list.add("two");
list.add("three");
System.out.println(list);
list.add(1, "one and a half");
System.out.println(list);
}
}
$ java ListAddIndex
[one, two, three]
[one, one and a half, two, three]
import java.util.ArrayList;
public class ListClear {
public static void main (String[] args){
ArrayList<String> list = new ArrayList<>();
list.add("one");
list.add("two");
list.add("three");
System.out.println(list);
list.clear();
System.out.println(list);
}
}
$ java ListClear
[one, two, three]
[]
import java.util.ArrayList;
public class ListGet {
public static void main (String[] args){
ArrayList<String> list = new ArrayList<>();
list.add("one");
list.add("two");
list.add("three");
System.out.println(list);
System.out.println("The value at index 1: " + list.get(1));
}
}
$ java ListGet
[one, two, three]
The value at index 1: two
import java.util.ArrayList;
public class ListRemove {
public static void main (String[] args){
ArrayList<String> list = new ArrayList<>();
list.add("one");
list.add("two");
list.add("three");
System.out.println(list);
list.remove(1);
System.out.println(list);
}
}
$ java ListRemove
[one, two, three]
[one, three]
import java.util.ArrayList;
public class ListSet {
public static void main (String[] args){
ArrayList<String> list = new ArrayList<>();
list.add("one");
list.add("two");
list.add("three");
System.out.println(list);
list.set(1, "2");
System.out.println(list);
}
}
$ java ListSet
[one, two, three]
[one, 2, three]
import java.util.ArrayList;
public class ListSize {
public static void main (String[] args){
ArrayList<String> list = new ArrayList<>();
list.add("one");
System.out.println("Elements: " + list.size());
list.add("two");
System.out.println("Elements: " + list.size());
list.add("three");
System.out.println("Elements: " + list.size());
}
}
$ java ListSize
Elements: 1
Elements: 2
Elements: 3
| Method | Description |
|---|---|
| contains(value) | Returns true if the value can be found in the list |
| indexOf(value) | Returns the index of the first occurence of a value in the list or -1 if the value cannot be found |
| lastIndexOf(value) | Returns the index of the first occurence of a value in the list or -1 if the value cannot be found |
import java.util.ArrayList;
public class ListContains {
public static void main (String[] args){
ArrayListt<String> list = new ArrayList<>();
list.add("one");
list.add("two");
list.add("three");
System.out.println("\"two\" is in list: " + list.contains("two"));
System.out.println("\"four\" is in list: " + list.contains("four"));
}
}
$ java ListContains
"two" is in list: true
"four" is in list: false
import java.util.ArrayList;
public class ListIndexOf {
public static void main (String[] args){
ArrayList<String> teams = new ArrayList<>();
teams.add("Red Sox");
teams.add("Pats");
teams.add("Celtics");
teams.add("Bruins");
teams.add("Red Sox");
System.out.println(teams);
System.out.println("Index of \"Red Sox\": " + teams.indexOf("Red Sox"));
System.out.println("Index of \"Yankees\": " + teams.indexOf("Yankees"));
}
}
java ListIndexOf
[Red Sox, Pats, Celtics, Bruins, Red Sox]
Index of "Red Sox": 0
Index of "Yankees": -1
import java.util.ArrayList;
public class ListLastIndexOf {
public static void main (String[] args){
ArrayList<String> teams = new ArrayList<>();
teams.add("Red Sox");
teams.add("Pats");
teams.add("Celtics");
teams.add("Bruins");
teams.add("Red Sox");
System.out.println(teams);
System.out.println("Last index of \"Red Sox\": " + teams.lastIndexOf("Red Sox"));
System.out.println("Last index of \"Yankees\": " + teams.lastIndexOf("Yankees"));
}
}
$ java ListLastIndexOf
[Red Sox, Pats, Celtics, Bruins, Red Sox]
Last index of "Red Sox": 4
Last index of "Yankees": -1
import java.io.*;
import java.util.*;
public class FileToList {
public static void main (String[] args)
throws FileNotFoundException {
ArrayList<String> list = create_list_from_file(args[0]);
System.out.println(list);
}
public static ArrayList>String> create_list_from_file(String filename)
throws FileNotFoundException {
File file = new File(filename);
ArrayList>String> list = new ArrayList<>();
Scanner input = new Scanner(file);
while (input.hasNext()){
list.add(input.next());
}
return list;
}
}
$ java FileToList.java fruit.txt
[grapes, pears, oranges, cranberries, apples, melons, blueberries]
import java.io.*;
import java.util.*;
public class FileToList2 {
public static void main (String[] args)
throws FileNotFoundException {
ArrayList<Integer> list = create_list_from_file(args[0]);
System.out.println(list);
}
public static ArrayList<Integer> create_list_from_file(String filename)
throws FileNotFoundException {
File file = new File(filename);
ArrayListArrayList<Integer> list = new ArrayList<>();
Scanner input = new Scanner(file);
while (input.hasNextInt()){
list.add(input.());
}
return list;
}
}
$ java FileToList2.java numbers
[46, 19, 35, 43, 35, 33, 42, 6, 25, 6, 19, 31, 1, 23, 14, 18, 24, 24, 48, 13, 36, 12, 15, 40, 14, 27, 28, 44, 30, 50]
for loop it tells the number of times the loop
will run
while and do/while loops
it gives the condition that will make the loop stop
continuebreakbreak Statementbreak statement
public class Break {
public static void main (String[] args){
for (int i = 1; i <= 10; i++){
if (i == 5){
break;
} else {
System.out.println(i);
}
}
}
}
$ java Break.
1
2
3
4
continue Statementcontinue statement
public class Continue {
public static void main (String[] args){
for (int i = 1; i <= 10; i++){
if (i % 2 == 0){
continue;
} else {
System.out.println(i);
}
}
}
}
$ java Continue
1
3
5
7
9
switch Statement
public class ItCourseTitle {
public static void main (String[] args){
String course_number = args[0];
String course_title;
if (course_number.equals("110")){
course_title = "Information Technology Problem Solving";
} else if (course_number.equals("111")){
course_title = "Managerial Statistics";
} else if (course_number.equals("114")){
course_title = "Introduction To Java";
} else if (course_number.equals("116")){
course_title = "Introduction to Scripting";
} else if (course_number.equals("117")){
course_title = "Intermediate Scripting";
} else {
course_title = "Unknown";
}
System.out.println("IT " + course_number + " - " + course_title);
}
}
$ java ItCourseTitle 114
IT 114 - Introduction To Java
$ java ItCourseTitle.java 119
IT 119 - Unknown
switch statement
switch(EXPRESSION) {
case VALUE_1:
STATEMENT;
STATEMENT;
...
break;
case y:
STATEMENT;
STATEMENT;
break;
...
default:
STATEMENT;
STATEMENT;
}
switch statement
public class ItCourseTitle2 {
public static void main (String[] args){
String course_number = args[0];
String course_title;
switch (course_number){
case "110":
course_title = "Information Technology Problem Solving";
break;
case "111":
course_title = "Managerial Statistics";
break;
case "114":
course_title = "Introduction To Java";
break;
case "116":
course_title = "Introduction to Scripting";
break;
case "117":
course_title = "Intermediate Scripting";
break;
default:
course_title = "Unknown";
}
System.out.println("IT " + course_number + " - " + course_title);
}
}
$ java ItCourseTitle2 114
IT 114 - Introduction To Java
break in a switch Statementbreak in the code for each case
you will get very strange results
public class ItCourseTitle3 {
public static void main (String[] args){
String course_number = args[0];
String course_title;
switch (course_number){
case "110":
course_title = "Information Technology Problem Solving";
System.out.println("IT " + course_number + " - " + course_title);
case "111":
course_title = "Managerial Statistics";
System.out.println("IT " + course_number + " - " + course_title);
case "114":
course_title = "Introduction To Java";
System.out.println("IT " + course_number + " - " + course_title);
case "116":
course_title = "Introduction to Scripting";
System.out.println("IT " + course_number + " - " + course_title);
case "117":
course_title = "Intermediate Scripting";
System.out.println("IT " + course_number + " - " + course_title);
default:
course_title = "Unknown";
}
}
}
$ java ItCourseTitle3 114
IT 114 - Introduction To Java
IT 114 - Introduction to Scripting
IT 114 - Intermediate Scripting